Cannot properly go through a tree recursively

Posted 11 December 2010 - 05:14 PM

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:

Replies To: Cannot properly go through a tree recursively

Re: Cannot properly go through a tree recursively

Posted 11 December 2010 - 05:24 PM

Your main problem is that you are ignoring the recursive calls and what they return. To be honest, it might be easier to just get rid of your boolean and use return statements instead. So if the current Node has the value, return true. Else if the recursive call to the left returns true, then return true. Same for the recursive call on the right. Finally, return false because you haven't returned true.

Re: Cannot properly go through a tree recursively

Posted 11 December 2010 - 07:15 PM

macosxnerd101, on 11 December 2010 - 04:24 PM, said:

Your main problem is that you are ignoring the recursive calls and what they return. To be honest, it might be easier to just get rid of your boolean and use return statements instead. So if the current Node has the value, return true. Else if the recursive call to the left returns true, then return true. Same for the recursive call on the right. Finally, return false because you haven't returned true.

I'm not sure I understand 100% what you're saying here. But from what I understood I used to write this code:

Re: Cannot properly go through a tree recursively

Posted 11 December 2010 - 07:20 PM

You should in your first contains(BTreeNode<T>) method check to see if your Collection is empty before invoking the overloaded contains() method so only the root Node will be checked for the Exception, not other childless Nodes. Otherwise, your revised method looks good.

Re: Cannot properly go through a tree recursively

Posted 11 December 2010 - 07:42 PM

macosxnerd101, on 11 December 2010 - 06:20 PM, said:

You should in your first contains(BTreeNode<T>) method check to see if your Collection is empty before invoking the overloaded contains() method so only the root Node will be checked for the Exception, not other childless Nodes. Otherwise, your revised method looks good.

Dogstopper, on 11 December 2010 - 06:20 PM, said:

Well, I think you are throwing your exception incorrectly. If you have a tree like this:

1
/\
2 3
/ \ \
4 5 6

When you get to 4, 5, or 6, and those don't equal the target, it calls this:

else if (containsItem(current.getLeft(), target))

But, as you can see, 4 has null as a left element, which means that current == null and throws an exception as you have set up here:

if (current == null){ throw new EmptyCollectionException(); }

You need to not have that exception be thrown unless the ROOT node is null.

that clarified things up alot for me. I got rid of the exception but now it returns null instead of true or false. This is what I changed my code to: