Well I'll go straight to the point...
I'm trying to recursively move through a binary search tree. I'm not really sure what is wrong with my algorithm (and I'm also pretty new to recursion) but this is what I have:
public boolean contains(BTreeNode<T> node)
{
return containsItem(root, node);
}
private boolean containsItem(BTreeNode<T> current, BTreeNode<T> target)
{
boolean found = false;
if(current != null) {
if(current.equals(target))
{
found = true;
}
else{
if(current.getLeft() != null)
{
containsItem(current.getLeft(), target);
}
if(current.getRight() != null)
{
containsItem(current.getRight(), target);
}
}
}
else{
throw new EmptyCollectionException();
}
return found;
}
When i enter
System.out.println(BST.contains(new BTreeNode(45)));
it returns false (45 IS an item in a Binary search tree called BST)
What am I doing wrong? Is there something wrong with my base case? Please give me some insight.