0

Im new to generics and i have to implement a binary search tree using generics. I did that but now im wondering how do i test the code that i wrote? Do i just make another class and start using the methods of the bst?

any help would be appreciated. below is my code just to clarify.

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)
{
}

}

and my node class

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;
}
}

Im trying to follow what you said, this is my test class public class BSTTest { public void testInsert() { int height; BST myTree = new BST(); myTree.insert(1); }

} 

but when i compile i get the error of unexpected type, it says if found an int but requires a reference on the line of BST myTree = new BST(); what does that mean?

3
  • you can do what you suggested or use unit testing. Commented Feb 21, 2013 at 22:21
  • BST can't preserve its invariants as long as Node's setters are public. Commented Feb 21, 2013 at 22:32
  • for best results use E extends Comparable<? super E> Commented Feb 22, 2013 at 3:08

1 Answer 1

1

Yes, make a class called BSTTest and create methods to test each of the public methods in BST.

If you use JUnit, you can use annotations and a standard naming convention

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...
    }

}

Then, you can run this 'Unit Test' in your java IDE (such as IntelliJ IDEA) or, if you have it set up, via maven: mvn test.

Also, I think your find() method could return boolean?

good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Im trying to follow what you said, this is my test classpublic class BSTTest { public void testInsert() { int height; BST<int> myTree = new BST<int>(); myTree.insert(1); } } but when i compile i get the error of unexpected type, it says if found an int but requires a reference on the line of BST<int> myTree = new BST<int>(); what does that mean?
try BST<Integer> myTree = new BST<Integer>();... how does that go?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.