I have a big problem with these nonlinear thinking Binary trees, and expecially when one is required to display the contents of a proper BinaryTree for ones Easter Assignment Does anyone have a good starting point. The only way of doing this i think is recursivley, but when you get down into either left or right subtrees, the process becomes very confusing! Should i be considering the heights\depth of the nodes in each subtree? Any help would be smashing.
I am not sure if your code will work. For instance, and I assume, you start with the root and the root's parent is null. In that case the "if" condition returns false and you fall into the else clause. In the else clause you call start.getParent().getRight(). We already know start.getParent() returns null, so wouldn't you get a NullPointerException?
Learning trees & lists & such I found it very helpful to draw the tree on paper and imagine that I'm walking around on it. When you're standing on the anchor node, what do you want to display first? Next? How do you get there? When you hit the end of a branch, how do you get back?
The good news is the solution is simpler than you think. As a hint, it will have fewer if tests than you do now.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi