title: How do I print a binary tree “as a” tree?
body: <p>I made a program that inserts values, sorts them using binary search, and outputs the sorted values. However, I also would like to use the function PrintTree() to print the tree. I can't think of any way to implement a functioning code in that function.
  Here is my code up until now.</p>
  <pre><code>#ifndef b_h
  #define b_h
  
  #include &lt;iostream&gt;
  
  class BSTNode
  {
  public:
      int Key;
      BSTNode * Left;
      BSTNode * Right;
      BSTNode * Parent;
      int Height;
  };
  
  class BST
  {
  private:
      BSTNode * root;
      BSTNode * insertKey(BSTNode * node, int newKey);
      void PrintTreeInOrder(BSTNode * node);
      BSTNode * hasKey(BSTNode * node, int searchKey);
   
  
  
  
  public:
      BST();
      ~BST();
      void insertKey(int newKey);
      void PrintTreeInOrder();
      bool hasKey(int searchKey);
       void TreePrint();
  
  };
  
  #endif 
  </code></pre>
  <p>The header file above.</p>
  <pre><code>#include &quot;b.h&quot;
  
  BST::BST() : root(NULL)
  {
  }
  
  
  BSTNode * BST::insertKey(BSTNode * node, int newKey)
  {
     
      if(node == NULL)
      {
          node = new BSTNode;
          node-&gt;Key = newKey;
          node-&gt;Left = NULL;
          node-&gt;Right = NULL;
          node-&gt;Parent = NULL;
      }
    
      else if(node-&gt;Key &lt; newKey)
      {
          node-&gt;Right = insertKey(node-&gt;Right, newKey);
          node-&gt;Right-&gt;Parent = node;
      }
    
      else
      {
          node-&gt;Left = insertKey(node-&gt;Left, newKey);
          node-&gt;Left-&gt;Parent = node;
      }
  
      return node;
  }
  
  void BST::insertKey(int newKey)
  {
      root = insertKey(root, newKey);
  }
  
  void BST::PrintTreeInOrder(BSTNode * node)
  {
      if(node == NULL)
          return;
  
     
      PrintTreeInOrder(node-&gt;Left);
      std::cout &lt;&lt; node-&gt;Key &lt;&lt; &quot; &quot;;
  
      PrintTreeInOrder(node-&gt;Right);
  }
  
  void BST::PrintTreeInOrder()
  {
      PrintTreeInOrder(root);
      std::cout &lt;&lt; std::endl;
  }
  
  BSTNode * BST::hasKey(BSTNode * node, int key)
  {
    
      if (node == NULL)
          return NULL;
  
      else if(node-&gt;Key == key)
          return node;
     
      else if(node-&gt;Key &lt; key)
          return hasKey(node-&gt;Right, key);
     
      else
          return hasKey(node-&gt;Left, key);
  }
  
  bool BST::hasKey(int searchKey)
  {
     
      BSTNode * result = hasKey(root, searchKey);
  
      return result == NULL ?
          false :
          true;
  }
  </code></pre>
  <p>The cpp file above.</p>
  <pre><code>
  #include &lt;cstddef&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;limits&gt;
  #include &quot;b.h&quot;
  
  using namespace std;
  
  int main()
  {
      cout &lt;&lt; &quot;Binary Search Tree&quot; &lt;&lt; endl;
  
      BST * tree = new BST;
  
      cout &lt;&lt; &quot;Enter the numbers to be stored (end with a letter): &quot;;
  
      int value;
      int i = 0;
      vector &lt;int&gt; keys;
      
       
  cin &gt;&gt; value;
      while (!cin.fail()){
      
      keys.push_back(value);
      cin &gt;&gt; value;
      i++;
     }
         cin.clear(); // reset failbit
      cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
     
  
      for(const int&amp; key : keys){
          tree-&gt;insertKey(key);}
  
      int number;
      cout &lt;&lt; &quot;Which number do you want to look up? &quot; &lt;&lt;endl;
      cin &gt;&gt; number;
     
      bool b = tree-&gt;hasKey(number);
      if(b){
          cout &lt;&lt; number &lt;&lt; &quot; is in the tree: yes&quot;;}
      else{
          cout &lt;&lt; number &lt;&lt; &quot; is in the tree: no&quot;;
      cout &lt;&lt; endl;}
      
      cout &lt;&lt; &quot;The numbers in sorted order: &quot;;
      tree-&gt;PrintTreeInOrder();
  
       tree-&gt;TreePrint();
     
  
      return 0;
  }
  </code></pre>
  <p>And the main.</p>
  <p>This program works and it sorts the vector using binary search and has a search-for-key function, however, I want to print the tree in the function PrintTree(); as follows for example;</p>
  <pre><code>       8
      3    10
   1    6     14
      4  7   13
  </code></pre>
  <p>Can anyone help?</p>