Chapter 17: Problem 25
\((\) Printing Trees) Write a recursive method outputTree to display a binary tree object on the screen. 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 of the screen. Each row is output vertically. For example, the binary tree illustrated in Fig. 17.20 is output as shown in Fig. 17.21 The rightmost leaf node appears at the top of the output in the rightmost column and the root node appears at the left of the output. Each column starts five spaces to the right of the preceding column. Method outputTree should receive an argument total Spaces 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 as follows: While the reference to the current node is not null, perform the following: Recursively call outputTree with the right subtree of the current node and totalSpaces +5 Use a for statement to count from 1 to totalSpaces and output spaces. Output the value in the current node. Set the reference to the current node to refer to the left subtree of the current node. Increment totalSpaces by 5
Short Answer
Step by step solution
Key Concepts
These are the key concepts you need to understand to accurately answer the question.