Java泛型测试类

时间:2013-02-21 22:19:51

标签: java generics binary-search-tree

我是泛型的新手,我必须使用泛型实现二叉搜索树。我这样做但现在我想知道我如何测试我写的代码?我只是创建另一个类并开始使用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;
}
}

我试着按照你的说法,这是我的测试课     公共课BSTTest         {         public void testInsert()         {         int height;         BST myTree =新BST();         myTree.insert(1);         }

} 

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

1 个答案:

答案 0 :(得分:0)

是的,创建一个名为BSTTest的类,并创建测试BST中每个公共方法的方法。

如果使用JUnit,则可以使用注释和标准命名约定

public class BSTTest {
    @Test
    public void testInsert() {
        BST<String> bst = new BST<String>();
        String s = "hello";
        bst.insert(s);
        AssertTrue("I should get back what I put in!", bst.find(s));
    }

    @Test
    public void testDelete() {
        // etc...
    }

}

然后,您可以在Java IDE(例如IntelliJ IDEA)中运行此“单元测试”,或者,如果您已经设置,则通过maven:mvn test运行。

另外,我认为你的find()方法可以返回布尔值?

祝你好运!