title: How to display binary search tree in console properly?
body: <p>I'm trying to display a BST in console. That's my code (it's a modified version of code found here: <a href="https://stackoverflow.com/questions/13186941/printing-level-order-binary-search-tree-formatting">Printing Level Order Binary Search Tree Formatting</a>):</p>
  
  <pre><code>string printLevel(node *root, int level, string gap) {
    if (!root) {
      return gap + "-" + gap;
    }
    if (level==1) {
      stringstream out;
      out&lt;&lt;root-&gt;value;
      return gap + out.str() + gap;
    } else if (level&gt;1) {
      string leftStr = printLevel(root-&gt;left, level-1, gap);
      string rightStr = printLevel(root-&gt;right, level-1, gap);
      return leftStr + " " + rightStr;
    } else return "";
  }
  
  void printLevelOrder (node* root, int depth) {
    for (int i=1; i&lt;=depth; i++) {
      string gap="";
      for (int j=0; j&lt;pow(2,depth-i)-1; j++) {
        gap+=" ";
      }
      string levelNodes = printLevel(root, i, gap);
      cout&lt;&lt;levelNodes&lt;&lt;endl;
    }
  }
  </code></pre>
  
  <p>For example result should be like that:</p>
  
  <pre><code>       4
     1       6
   -   2   5   - 
  - - - 3 - - - -
  </code></pre>
  
  <p>But instead it is:</p>
  
  <pre><code>       4       
     1       6
   -   2   5   -
  - - 3 - - -
  </code></pre>
  
  <p>If I undestand correctly, the recursion stops when the program makes it to the empty leaf and therefore there are lacking " - " on the lower levels in the result. But how do I know how much of those I should draw on the lower levels? How to make this work?</p>