Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: (Binary Tree) Family tree - help with my addChild method

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    UK
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default (Binary Tree) Family tree - help with my addChild method

    unable to delete post
    Last edited by Pip_Squeak; April 2nd, 2014 at 09:27 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: (Binary Tree) Family tree - help with my addChild method

    I suspect your if statement bodies are not what you're thinking. Enclose the entire body of each if statement, and see if that helps. Like this:
    if ( condition )
    {
        // statement 1
     
        // statement 2
     
        // etc . . .
    }

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    UK
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (Binary Tree) Family tree - help with my addChild method

    Hi, thanks for your reply.
    I'm still not any further forward i think I've just been staring at it too long

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: (Binary Tree) Family tree - help with my addChild method

    Post your updated code and describe the new results. Show an actual run, if possible, and describe how the actual outcome varies from what you expect or desire.

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: (Binary Tree) Family tree - help with my addChild method

    Focus on this block of code:
            if (next.sibling!=null)
                    next=next.sibling;
                    next.sibling=member;        
                    if (current.name.compareToIgnoreCase(next.name) == 0)
                        match = true;
                        System.out.println("Name already exists");

    The indentation used here is most likely misleading of the intention of the if blocks. The above, when braces are used consistently, is equivalent to
            if (next.sibling!=null) {
                next=next.sibling;
            }
            next.sibling=member;
            if (current.name.compareToIgnoreCase(next.name) == 0) {
                match = true;
            }
            System.out.println("Name already exists");

    Is this the logic that is intended here? E.g., next.sibling=member; is run regardless whether (next.sibling!=null) evaluates to true or false?

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Location
    UK
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (Binary Tree) Family tree - help with my addChild method

    Thanks for your reply.
    Yes, i believe so - it's just so i can have multiple siblings, rather than limit the structure to two kids (child & one sibling)

Similar Threads

  1. Binary Tree ( Family Tree)
    By tommyacton in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 23rd, 2013, 10:40 PM
  2. Binary Search Tree inorder tree traversal
    By Maukkv in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 26th, 2013, 05:28 PM
  3. Binary Search Tree Lookup Method
    By Sbonett in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 15th, 2012, 07:43 AM
  4. a recorsive method to get level of a certin leaf on a binary tree???
    By oshikoren in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 11:27 AM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM

Tags for this Thread