Modify class Tree of Fig. 21.15 to include a recursive outputTree method to display a binary tree
Question:
Modify class Tree of Fig. 21.15 to include a recursive outputTree method to display a binary tree object. The method should output the tree row by row, with the top of the tree at the left of the screen and the bottom of the tree toward the right. Each row is output vertically.
For example, the binary tree illustrated in Fig. 21.18 is output as shown in Fig. 21.23.
Fig. 21.23
The rightmost leaf node appears at the output’s top in the rightmost column and the root node appears at the output’s left. Each column starts five spaces to the right of the preceding column. Method outputTree should receive an argument totalSpaces representing the number of spaces preceding the value to be output. (This variable should start at zero so that the root node is output at the left of the screen.) The method uses a modified inorder traversal to output the tree—it starts at the rightmost node in the tree and works back to the left. The algorithm is in Fig. 21.24.
Fig. 21.24
Fig. 21.15
Fig. 21.18
Step by Step Answer:
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates