Generi c предупреждение о безопасности типа - PullRequest
0 голосов
/ 28 мая 2020

Я получаю предупреждение о безопасности типов повсюду в программе, и я почти уверен, что сделал что-то не так с использованием дженериков. Предупреждение отображается в основном коде, в котором задействован Node. Пожалуйста, укажите те части, где мой синтаксис, возможно, не самый идеальный, и порекомендуйте внести изменения в мой код. Ценю любую помощь!

Работает удаление класса in Node!

public class BinaryTree<T extends Comparable<T>> {

private class Node<T>{

    private T data;
    private Node left;
    private Node right;

    // left and right child do not have to nessary exist
    public Node ( T data) {
        this.data = data;
        this.left = null;
        this.right = null; 
    }}

    private Node<T>root;
    private int count = 0;

    public void add( T data) {
        if ( isEmpty()) {
            root = new Node(data);
            count++;
        }
        else {
            insert(data, root);
            count++;
        }
    }

    public boolean isEmpty() {  
        return root == null;
    }

    public T getRoot()  {
        if ( root.data == null) {
            System.out.println("Root is empty");
            return null;
        }
        else {
        return  root.data;
    }}

    /*
     * Checking if the data is larger or lesser than the parent's data
     * If the data is smaller than the parent's data, node.left is created 
     * If the data is bigger than the parent's data, node.right is created
     */
    private void insert( T data, Node<T>node) {

        /*
         * If 1st obj is less than the 2nd obj return a neg
         * if 1st obj is more than the 2nd obj return a pos
         * if equal return 0
         */
        int compare = data.compareTo(node.data);
        if ( compare < 1 ){
            if (node.left == null ) {
                node.left = new Node(data);

            }

        // make node.left if it is filled   
            else {
                insert(data, node.left);
            }
        }

        else {
            if ( node.right == null) {
                node.right = new Node(data);
            }
            else {
                insert( data, node.right);
            }
        }
    }

    public int getSize() {
        return count;
    }
...