Results 1 to 9 of 9
Thread: Recursion returning boolean
- 11-30-2011, 10:45 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
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.
All help appreciated!Java 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; }
- 11-30-2011, 10:53 PM #2
Member
- Join Date
- Nov 2011
- Location
- Budapest, Hungary
- Posts
- 5
- Rep Power
- 0
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?
- 11-30-2011, 11:02 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
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.
- 11-30-2011, 11:03 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
Re: Recursion returning boolean
and by the way child is the node i am searching for so no i dont mean child.offspring.iterator()
- 11-30-2011, 11:19 PM #5
Member
- Join Date
- Nov 2011
- Location
- Budapest, Hungary
- Posts
- 5
- Rep Power
- 0
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.
- 11-30-2011, 11:22 PM #6
Member
- Join Date
- Nov 2011
- Location
- Budapest, Hungary
- Posts
- 5
- Rep Power
- 0
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
- 11-30-2011, 11:30 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
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
- 11-30-2011, 11:32 PM #8
Member
- Join Date
- Nov 2011
- Location
- Budapest, Hungary
- Posts
- 5
- Rep Power
- 0
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;
}
- 11-30-2011, 11:48 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
Returning a value instead of false for a boolean
By NewToJava_99 in forum New To JavaReplies: 2Last Post: 11-23-2011, 11:33 PM -
Boolean help
By FadedAura in forum New To JavaReplies: 2Last Post: 11-20-2011, 01:56 PM -
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 12:01 AM -
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05-05-2011, 09:04 AM -
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks