Results 1 to 9 of 9
  1. #1
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

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

    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;
            }
    All help appreciated!

  2. #2
    Squiffy is offline Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    5
    Rep Power
    0

    Default 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?

  3. #3
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

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

  4. #4
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default Re: Recursion returning boolean

    and by the way child is the node i am searching for so no i dont mean child.offspring.iterator()

  5. #5
    Squiffy is offline Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    5
    Rep Power
    0

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

  6. #6
    Squiffy is offline Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    5
    Rep Power
    0

    Default 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

  7. #7
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default 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

  8. #8
    Squiffy is offline Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    5
    Rep Power
    0

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

  9. #9
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default Re: Recursion returning boolean

    Brilliant thats perfect thanks alot!!!!! :)

Similar Threads

  1. Returning a value instead of false for a boolean
    By NewToJava_99 in forum New To Java
    Replies: 2
    Last Post: 11-24-2011, 12:33 AM
  2. Boolean help
    By FadedAura in forum New To Java
    Replies: 2
    Last Post: 11-20-2011, 02:56 PM
  3. Replies: 3
    Last Post: 09-13-2011, 01:01 AM
  4. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 10:04 AM
  5. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •