Search code examples
javabinary-treepretty-print

Coding a basic pretty printer for trees in Java


I need help in understanding how to code a pretty printer for any given binary tree.

I know the first step is to do preorder to get all of the nodes.

I know that during the preorder traversal, that's where all of the prettyness will be implemented.

Just not sure how to get started. I am given a preorder traversal function that works but i'm not sure where to start modifying it or if I should just make my own function.

No code, just asking for ideas on how others would go about it.

And maybe later code if I get desperate :P

To be specific, It should look like this:

example


Solution

  • If you know the depth of the tree, i.e. the number of levels you can calculate the maximum number of nodes in the last level (which is n2 for n=number of levels - 1, 1 element if you have the root only). If you further know the width of the elements (e.g. if you have at most 2-digit numbers each element would have width 2) you can calculate the width of the last level: ((number of elements - 1) * (element width + spacing) + element width).

    Actually the above paragraph doesn't matter. All you need is the depth of the tree, i.e. the maximum level that is to be displayed. However, if the tree is sparse, i.e. not all elements of the last level and maybe above are present, you'll need to get the position of the node you're rendering in order to adapt the indent/spacing for that case accordingly.

    In your pre-order iteration you can then calculate the indentation and spacing between the elements at each level. The formula for the indent would be: 2(max level - level) - 1 and for the spacing: 2(max level - level + 1) - 1 (level is 1-based)

    Example:

           1
       2       3
     4   5   6   7
    8 9 A B C D E F
    

    In that tree, the number of levels is 4. The spacing at the last level is 1 whereas the indent is 0. You'll get the following values:

    • level 1:
      • indent = 7: (2(4-1) - 1 = 23 - 1 = 8 - 1 = 7)
      • first level, so spacing doesn't matter
    • level 2:
      • indent = 3: (2(4-2) - 1 = 22 - 1 = 4 - 1 = 3)
      • spacing = 7 (2(4-1) - 1 = 23 - 1 = 8 - 1 = 7)
    • level 3:
      • indent = 1: (2(4-3) - 1 = 21 - 1 = 2 - 1 = 1)
      • spacing = 3 (2(4-2) - 1 = 22 - 1 = 4 - 1 = 3)
    • level 4:
      • indent = 0: (2(4-4) - 1 = 20 - 1 = 1 - 1 = 0)
      • spacing = 1: (2(4-3) - 1 = 21 - 1 = 2 - 1 = 1)

    Note that at the last level you'll always have spacing 1 * element width. Thus for a maximum element width of 3 (e.g. 3-digit numbers) you'd have a spacing of 3 at the last level, in order to get some pretty alignment of the upper levels.

    Edit: For your pretty print, you'd just have to calculate the indent as width element * level where level would be zero-based. Then if the node is not a leaf, draw it with a opening paranthesis in front and a closing paranthesis after drawing the children, if it is a leaf just draw it and if the leaf is missing, draw double paranthis.

    Thus you'd get something like this:

    public void printSubtree( int indent, node ) {
      for( int i = 0; i < indent; ++i) {
        System.out.print(" ");
      }
    
      if( inner node) {
        System.out.println("(" + value);     
        printSubtree(indent + elem width, left child); //this is a recursive call, alternatively use the indent formula above if you don't use recursion
        printSubtree(indent + elem width, right child);
    
        //we have a new line so print the indent again
        for( int i = 0; i < indent; ++i) {
          System.out.print(" ");
        }
        System.out.println(")"); 
      } else if( not empty) {
        System.out.println(value);
      } else { //empty/non existing node
        System.out.println("()");
      }
    }