title: C++ print out a binary search tree
body: <p>Got nothing better to do this Christmas holiday, so I decided to try out making a binary search tree. I'm stuck with the print function. How should the logic behind it work? Since the tree is already inserting it in a somewhat sorted order, and I want to print the tree from smallest values to the biggest.</p>
  
  <p>So I need to travel to the furthest left branch of the tree to print the first value. Right, so after that how do I remember the way back up, do I need to save the previous node? A search in wikipedia gave me an solution which they used stack. And other solutions I couldn't quite understand how they've made it, so I'm asking here instead hoping someone can enlight me.</p>
  
  <p>I also wonder my insert function is OK. I've seen other's solution being smaller.</p>
  
  <pre><code>void treenode::insert(int i)
  {
  
     if(root == 0)
     {
        cout &lt;&lt; "root" &lt;&lt; endl;
        root = new node(i,root);
     }
     else
     {
        node* travel = root;
        node* prev;
        while(travel)
        {
           if(travel-&gt;value &gt; i)
           {
              cout &lt;&lt; "travel left" &lt;&lt; endl;
              prev = travel;
              travel = travel-&gt;left;
           }
           else
           {
              cout &lt;&lt; "travel right" &lt;&lt; endl;
              prev = travel;
              travel = travel-&gt;right;
           }
        }
        //insert
        if(prev-&gt;value &gt; i)
        {
           cout &lt;&lt; "left" &lt;&lt; endl;
           prev-&gt;left = new node(i);
        }
        else
        {
           cout &lt;&lt; "right" &lt;&lt; endl;
           prev-&gt;right = new node(i);
        }
     }
  
  }
  
  void treenode::print()
  {
  
     node* travel = root;
     while(travel)
     {
        cout &lt;&lt; travel-&gt;value &lt;&lt; endl;
        travel = travel-&gt;left;
     }
  
  }
  </code></pre>