title: Recursively Insert Element Into A Binary Tree
body: <p>So I finished my List exercise and went ahead with Binary Trees. My code thus far: </p>
  
  <p>Tree.h</p>
  
  <pre><code>#include "Node.h"
  
  class Tree
  {
  private:
      int mCount;
      Node *root;
  
  public:
      Tree();
      ~Tree();
  
      void insert(int, Node *);
  };
  </code></pre>
  
  <p>Tree.cpp</p>
  
  <pre><code>void Tree::insert(int data, Node *node)
  {
      if( root == 0 )
      {
          Node *temp = new Node;
          temp-&gt;setData(100);
          temp-&gt;setRight(0);
          temp-&gt;setLeft(0);
          root = temp;
      }
      else
      {
          if( data &gt; root-&gt;getData() )
              return insert( data, root-&gt;getRight() );
          else
              return insert( data, root-&gt;getLeft() );
      }
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>int main(int argc, char** argv)
  {
      Tree *tree = new Tree;
      tree-&gt;insert( 100, 0 );
  
      std::cin.get();
      return 0;
  }
  </code></pre>
  
  <p>I hope this is sufficient code. <code>Node</code> and <code>Tree</code> are two separate classes. I'm having difficulties wrapping my head around the recursion. </p>
  
  <p>I have <code>Node *root</code> defined in my Tree class to have a root node at the top of the tree. However, the way I see it, when I call <code>tree-&gt;insert</code> insert in main, I don't have to specify any node. The root from the Tree class will do all the looking. However, when I'm in the code and need to recur, then I am suddenly a parameter short, as shown above.</p>
  
  <p>My solution was to just place the parameter <code>Node *node</code> in the argument list for <code>insert()</code> anyway and call it with a <code>0</code> from main. I would also need to call <code>tree-&gt;display(0);</code> as parameter for <code>Node *node</code> as well.</p>
  
  <p>This seems hackish. Am I missing something obvious? </p>