title: C++ Binary Tree, modifying root node, pointer to pointer to root node
body: <p>I realize the title isn't too descriptive so here are the details.  I'm implementing my own Binary Tree class in C++.  I have written a template Node class and template Binary Tree class already, for the most part, and am stuck on something. I created an empty binary tree (root node is null) and when I try to set that node it fails miserably. here is the code and more explanation:</p>
  
  <pre><code>template&lt;class T&gt; class Node
  {
      T _key;
      Node&lt;T&gt; *_leftChild;
      Node&lt;T&gt; *_rightChild;
  
      public:
          Node();
          Node(T key);
          Node(T key, Node&lt;T&gt; *leftChild, Node&lt;T&gt; *rightChild);
          ~Node();
  
          bool hasLeftChild();
          bool hasRightChild();
  
          void setKey(T key);
          void setLeftChild(Node&lt;T&gt; *node);
          void setRightChild(Node&lt;T&gt; *node);
  
          T getKey();
          Node&lt;T&gt;* getLeftChild();
          Node&lt;T&gt;* getRightChild();
  
          bool compare(Node&lt;T&gt; *compareNode); // return true if this.Node &lt; compareNode
  };
  </code></pre>
  
  <p>Node implementation not really necessary.. ( I dont think ) it's quite long.</p>
  
  <pre><code>#include "Node.cpp"
  #include &lt;iostream&gt;
  using namespace std;
  
  template&lt;class T&gt; class BinaryTree
  {
      Node&lt;T&gt; *_root;
  
      public:
          BinaryTree();
          BinaryTree(Node&lt;T&gt; *root);
          ~BinaryTree();
  
          Node&lt;T&gt;* getRoot();
          void insert(Node&lt;T&gt; **root, Node&lt;T&gt; *node);
  };
  
  template&lt;class T&gt;
  BinaryTree&lt;T&gt;::BinaryTree()
  {
      this-&gt;_root = NULL;
  }
  
  template&lt;class T&gt;
  BinaryTree&lt;T&gt;::BinaryTree(Node&lt;T&gt; *root)
  {
      this-&gt;_root = root;
  }
  
  template&lt;class T&gt;
  BinaryTree&lt;T&gt;::~BinaryTree()
  {
      // delete stuff
  }
  
  template&lt;class T&gt;
  Node&lt;T&gt;* BinaryTree&lt;T&gt;::getRoot()
  {
      return this-&gt;_root;
  }
  
  template&lt;class T&gt;
  void BinaryTree&lt;T&gt;::insert(Node&lt;T&gt; **root, Node&lt;T&gt; *node)
  {
      if(!*root)
      {
          *root = node;
      }
  }
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>BinaryTree&lt;int&gt; *tree = new BinaryTree&lt;int&gt;();
  
  Node&lt;int&gt; *root = tree-&gt;getRoot();
  Node&lt;int&gt; **root1 = &amp;root;
  
  cout &lt;&lt; tree-&gt;getRoot() &lt;&lt; endl;
  Node&lt;int&gt; *noChildrenNode = new Node&lt;int&gt;(2);
  tree-&gt;insert(&amp;root1, noChildrenNode);
  cout &lt;&lt; tree-&gt;getRoot() &lt;&lt; endl;
  </code></pre>
  
  <p>Inserts current functionality is just supposed to replace the NULL root pointer to the node pointer passed in as a parameter.  The failing miserably part is since the pointer is a copy it isn't actually setting the root node.. but I can't seem to figure out how to set up a pointer to a pointer to the root node so it can be altered.. I've got to be close and any help will be MUCH appreciated.</p>
  
  <p>Thanks</p>