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

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

49 83 28 97 71 40 18 99 92 72 69 44 32 19 11

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

I 3 4 56 While the reference to the current node is not null, perform the following: Recursively call output

Fig. 21.15I // Fig. 21.15: Tree.java 2 // TreeNode and Tree class declarations for a binary search tree. 3 package

Fig. 21.18

18 11 19 28 40 32 44 49 71 69 72 83 97 92 99

49 83 28 97 71 40 18 99 92 72 69 44 32 19 11

Step by Step Solution

3.43 Rating (150 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To modify the Tree class to include a recursive outputTree method as described in the question you c... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!