Ex20_25 Printing Trees (P.821)

  1. Write a recursive member function outputTree to display a binary tree object on the screen.
  2. The function 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.
  3. Each row is output vertically. For example, the binary tree illustrated in Fig. 20.24 is output as shown in Fig. 20.25.
                   99
              97
                   92
         83
                   72
              71
                   69
    49
                   44
              40
                   32
         28
                   19
              18
                   11  
  4. Note that 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.
  5. Each column of output starts five spaces to the right of the previous column.
  6. Function outputTree should receive an argument pos representing the position where the value to be output. This variable should start at zero, so the root node is output at the left of the screen.
  7. The function uses a modified inorder traversal to output the tree -- it recursively processes the right subtree, print the value of the current node, and recursively processes the left subtree.
  8. The algorithm is as follows: