Please delete this thread!
Printable View
Please delete this thread!
Perhaps it is empty. Where did you put any data into mirror?Quote:
Every time I execute the program nothing prints out for the mirror tree.
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.
I haven't read that closely, but doesn't the no-arg createMirrorImage() do an inplace mirroring?
Yes that method should do the mirroring but then it returns the root node back to where????
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.Code:TreeItem mirror = new TreeItem(null, null, null, null);
mirror = r;
mirror.right = createMirrorImage(r.left);
mirror.left = createMirrorImage(r.right);
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: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,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;
}
Jos
Please delete
Sorry to repeat myself, but where (on what line) do you put any data into mirror?
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.
So add several String/Integer pairs.
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
Did you write the Tree class. It has an add method. Try using it.