Java Reference
In-Depth Information
152 /**
153
Prints this node and all of its descendants
154
in sorted order.
155 */
156
public void
printNodes()
157 {
158
if
(left !=
null
)
159 left.printNodes();
160 System.out.println(data +
Ð Ñ
);
161
if
(right !=
null
)
162 right.printNodes();
163 }
164
165
public
Comparable data;
166
public
Node left;
167
public
Node right;
168 }
169 }
730
731
S
ELF
C
HECK
9.
What is the difference between a tree, a binary tree, and a balanced
binary tree?
10.
Give an example of a string that, when inserted into the tree of
Figure
10
, becomes a right child of
Romeo
.
16.6 Tree Traversal
Now that the data are inserted in the tree, what can you do with them? It turns out to
be surprisingly simple to print all elements in sorted order. You know that all data in
the left subtree of any node must come before the node and before all data in the right
subtree. That is, the following algorithm will print the elements in sorted order:
1. Print the left subtree.
2. Print the data.
3. Print the right subtree.
Let's try this out with the tree in
Figure 10
. The algorithm tells us to