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

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

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  answer-question

Java How To Program Late Objects Version

ISBN: 9780136123712

8th Edition

Authors: Paul Deitel, Deitel & Associates

Question Posted: