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

    Default Mirror of binary tree

    Please delete this thread!
    Last edited by nve5009; 07-29-2011 at 06:41 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Every time I execute the program nothing prints out for the mirror tree.
    Perhaps it is empty. Where did you put any data into mirror?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    The code does not compile as the makeMirrorImage method does not exist. Even if you called the correct method it returns a TreeItem object which you ignore. This does not magically make your mirror variable refer to a Tree that is a mirror image of the original.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I haven't read that closely, but doesn't the no-arg createMirrorImage() do an inplace mirroring?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Yes that method should do the mirroring but then it returns the root node back to where????

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It may be that the OP intends to create a new TreeItem in which case you're right: it needs to be returned. But I thought it more likely that the mirroring was "inplace" and the root node didn't have to be returned because it was still the root. The OP will have to clarify what he is doing.

    Either way there's a nice bug in the code that is supposed to do the mirroring:

    Java Code:
    TreeItem mirror = new TreeItem(null, null, null, null);
    mirror = r;
    mirror.right =  createMirrorImage(r.left);
    mirror.left =  createMirrorImage(r.right);
    since mirror and r are the same thing, the entire r.right will disappear. mirror had better been named smoke.
    Last edited by pbrockway2; 07-28-2011 at 09:29 AM.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    The op has to decide whether or not he wants to modify the original tree to create the mirror of the tree. If surgery is allowed (and thus modification of the original tree) the solution is easy:

    Java Code:
    Node mirror(Node root) {
       if (root != null) {
          Node tmp= mirror(root.left);
          root.left= mirror(root.right);
          root.right= tmp;
       }
       return root;
    }
    This little method swaps all left/right pointers in the entire tree; if modification is not allowed (and a copy of the tree has to be delivered), the method is similar:

    Java Code:
    Node mirror(Node root) {
       if (root != null) {
          Node tmp= copyOf(root); // copy of a single node
          tmp.left= mirror(root.right);
          tmp.right= mirror(root.left);
          root= tmp;
       }
       return root;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    nve5009 is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Please delete
    Last edited by nve5009; 07-29-2011 at 06:42 AM.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Sorry to repeat myself, but where (on what line) do you put any data into mirror?

  10. #10
    nve5009 is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    I've acknowledged that this is the problem in my last post. I have no idea how I can put any data into mirror. I've tried to use the add method, but it doesn't work because it expects a pair of generic data which in this case is String and Integer.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    So add several String/Integer pairs.

  12. #12
    nve5009 is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Lol I give up none of this makes any sense. I'm just wasting my time. I was able to make the countLeaves and countNodes methods so easily but I'm getting nowhere with this. Thanks everyone for your help

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Did you write the Tree class. It has an add method. Try using it.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nve5009 View Post
    Lol I give up none of this makes any sense. I'm just wasting my time. I was able to make the countLeaves and countNodes methods so easily but I'm getting nowhere with this. Thanks everyone for your help
    Is my reply #7 written with invisible ink again?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. binary tree
    By Dedo in forum Advanced Java
    Replies: 15
    Last Post: 05-26-2011, 09:11 PM
  2. binary tree
    By ryamz in forum New To Java
    Replies: 2
    Last Post: 08-12-2010, 02:45 AM
  3. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  4. Binary Tree
    By MuslimCoder in forum New To Java
    Replies: 8
    Last Post: 11-19-2009, 05:57 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
  •