Results 1 to 14 of 14
Thread: Mirror of binary tree
- 07-28-2011, 06:42 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
- 07-28-2011, 07:39 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Perhaps it is empty. Where did you put any data into mirror?Every time I execute the program nothing prints out for the mirror tree.
- 07-28-2011, 07:56 AM #3
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.
- 07-28-2011, 08:13 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I haven't read that closely, but doesn't the no-arg createMirrorImage() do an inplace mirroring?
- 07-28-2011, 08:20 AM #5
Yes that method should do the mirroring but then it returns the root node back to where????
- 07-28-2011, 09:25 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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:
since mirror and r are the same thing, the entire r.right will disappear. mirror had better been named smoke.Java Code:TreeItem mirror = new TreeItem(null, null, null, null); mirror = r; mirror.right = createMirrorImage(r.left); mirror.left = createMirrorImage(r.right);
Last edited by pbrockway2; 07-28-2011 at 09:29 AM.
- 07-28-2011, 10:17 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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:
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= mirror(root.left); root.left= mirror(root.right); root.right= tmp; } return root; }
kind regards,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; }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-29-2011, 01:17 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
Please delete
Last edited by nve5009; 07-29-2011 at 06:42 AM.
- 07-29-2011, 02:45 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Sorry to repeat myself, but where (on what line) do you put any data into mirror?
- 07-29-2011, 04:27 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
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.
- 07-29-2011, 04:39 AM #11
So add several String/Integer pairs.
- 07-29-2011, 05:05 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
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
- 07-29-2011, 05:10 AM #13
Did you write the Tree class. It has an add method. Try using it.
- 07-29-2011, 09:13 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
binary tree
By Dedo in forum Advanced JavaReplies: 15Last Post: 05-26-2011, 09:11 PM -
binary tree
By ryamz in forum New To JavaReplies: 2Last Post: 08-12-2010, 02:45 AM -
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 07:40 AM -
Binary Tree
By MuslimCoder in forum New To JavaReplies: 8Last Post: 11-19-2009, 05:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks