Java 通用类测试

标签 java generics binary-search-tree

我正在尝试测试我编写的 Java 泛型类,这是我的测试

public class BSTTest
{
    public void testInsert()
    {
        int height;
        BST<int> myTree = new BST<int>();
        myTree.insert(1);
    }

} 

但是当我编译时我得到了意外类型的错误,它说如果找到一个 int 但需要在 BST myTree = new BST(); 行上的引用这是什么意思?

下面是我的二叉搜索树和节点类供引用

public class BST<E extends Comparable<E>>
{
    public Node<E> root;


    public BST()
    {
        root = null;
    }
    //insert delete find height
    public void find(E s, Node<E> n)
    {
        //empty tree, root is null
        if(n == null)
        {
            System.out.println("Item not present.");
        }
        //n is the node where s is, return n
        else if(n.getData().equals(s))
        {
            System.out.println("Item present");
        }
        //s is greater than n, look for s on the right subtree
        else if(s.compareTo(n.getData()) > 0)
        {
            find(s, n.getRight());
        }
        //s is less than n, look for s on the left subtree
        else
        {
            find(s, n.getLeft());
        }
    }

    public int height() 
    {
        int count;
        return count = height(root); 
    }

    private int height(Node<E> n)
    {
        int ct = 0;
        if(n == null)
        {

        }

        else
        {

            int left = height(n.getLeft());

            int right = height(n.getRight());

            ct = Math.max(left, right) + 1;
        }
        return ct;
    } 

    public void insert(E s) 
    {
        root = insert(s, root);
    } 

    private Node<E> insert(E s, Node<E> T)
    {
        //easiest case, empty tree, create new tree
        if(T == null)
        {
            T = new Node<E>(s,null,null);
        }
        //easiest case, found s
        else if(s.compareTo(T.getData()) == 0)
        {
            System.out.println("Item already present.");
        }
        //s is greater than T, insert on right subtree
        else if(s.compareTo(T.getData()) > 0)
        {
            T.setRight(insert(s, T.getRight()));
        }
        //s is less than T, insert on left subtree
        else
        {
            T.setLeft(insert(s,T.getLeft()));
        }
        return T;
    }

    public void delete(E d)
    {
    }

}

和我的节点类

public class Node<E> 
    {
       private E data;
    private Node<E> left;
    private Node<E> right;
    private Node<E> parent;

       public  Node(E d, Node<E> r, Node<E> l) 
    {
      data = d;

        left = l;
        right = r; 
       }
       public void setData(E d) 
    {
      data = d;
       }
    public E getData()
    {
        return data;
    }
       public Node<E> getRight() 
    {
      return right;
       }
    public void  setRight(Node<E> nd)
    {
        right = nd;
    }
       public Node<E> getLeft()
    {
        return left;
    }
    public void  setLeft(Node<E> nd)
    {
        left = nd;
    }
    public Node<E> getParent()
    {
        return parent;
    }
    public void  setParent(Node<E> nd)
    {
        parent = nd;
    }
}

最佳答案

你能试试 Integer 而不是 int 吗?

关于Java 通用类测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15083424/

相关文章:

Ruby 二进制搜索代码未返回预期结果

java - 是否可以在 android studio 中下载更多样式的小部件或者如何设计自己的小部件?

java - 模拟具有通用(?扩展集合)返回类型的方法时遇到问题

java - 摆脱 Unchecked 覆盖 : return type requires unchecked conversion

swift - 二叉搜索树中节点的后继 - Swift

c - 二叉搜索树疯狂

java - 向 ArrayList<String> 添加单个反斜杠

java - 在 Eclipse 中进行远程 Java 调试之前运行命令

java - 如何在小程序中设置Http Proxy

c# - 如何在开放泛型类型中定义构造函数?