# Recursion returning boolean

• 11-30-2011, 11:45 PM
Billywizz
Recursion returning boolean
Hi i have a method that traverses through a tree and compares if a given parameter is in the tree and returns true if it is and false if not. The method works fine and traverses through every element in the tree and compares every element but i am having trouble where to put the return true or false statements as it is recursion it seems to not return true immediatly when one compares. Please have a look at my code and see if you can spot where i have gone wrong. Thanks.

Code:

``` public boolean isDescendent(Object child) {             Node b;             Iterator it = this.offspring.iterator();             while(it.hasNext()){                     b = (Node) it.next();                     b.isDescendant(child);                     if(b.equals(child)){                         return true;                     }             }             return false;         }```
All help appreciated!
• 11-30-2011, 11:53 PM
Squiffy
Re: Recursion returning boolean
I don't really understand what you want your code to do exactly, but some aspects of it look a bit dodgy to me. For instance, did you really mean to call this.offspring.iterator() every time? You are going through the exact same loop with every recursion.

Did you mean child.offspring.iterator() or something like that?
• 12-01-2011, 12:02 AM
Billywizz
Re: Recursion returning boolean
yes i do mean to do that the method works fine dont worry about it i just need to know where to put the return statements or how to return true or false.
• 12-01-2011, 12:03 AM
Billywizz
Re: Recursion returning boolean
and by the way child is the node i am searching for so no i dont mean child.offspring.iterator()
• 12-01-2011, 12:19 AM
Squiffy
Re: Recursion returning boolean
I get you. Well, maybe you need to do your check before you recurse. Try putting if(b.equals ....) ABOVE b.isDescendant().

If that doesn't work, you can always post more code.
• 12-01-2011, 12:22 AM
Squiffy
Re: Recursion returning boolean
Hmmmm, I think you also need to check the return value of isDescendant, otherwise only the first level can return true or false.

If you can post a complete app, maybe I can help you .... otherwise I'll shut up and quit sounding like an idiot :D
• 12-01-2011, 12:30 AM
Billywizz
Re: Recursion returning boolean
Thanks Squiff unfortunatly that didnt work that is the whole method so... any other ideas? When i run it through the debugger it gets to the statement return true; statement goes over it but then continues through the method i want it to return true immediatly as soon as it reads return true is there anyway of doing this?

Thanks
• 12-01-2011, 12:32 AM
Squiffy
Re: Recursion returning boolean
I THINK what you want is something like the following, but wouldn't like to stake my house on it (if I had a house):

b = (Node) it.next();
if(b.equals(child)){
return true;
}

if(b.isDescendant(child) == true) {
return true;
}
• 12-01-2011, 12:48 AM
Billywizz
Re: Recursion returning boolean
Brilliant thats perfect thanks alot!!!!! :)