title: Node and Binary Tree Constructor and Destructor Segfaulting
body: <p>I've seen this question asked before here, specifically</p>
  
  <p><a href="https://stackoverflow.com/questions/9745736/generic-binary-tree-node-destructor-issue">Generic binary tree node destructor issue</a>
  and
  <a href="https://stackoverflow.com/questions/8018686/binary-search-tree-destructor">Binary Search Tree Destructor</a>
  among other things</p>
  
  <p>but so far the answers I got was to set the Node pointers to NULL in the constructor. Here is the code of my constructors and destructors for the Node and the Tree.</p>
  
  <pre><code>template &lt;class Type&gt;
  class Node {
  protected:
      Type data;
  public:
      Node&lt;Type&gt;(Type data) { this-&gt;data = data; }
  };
  </code></pre>
  
  <p>The Binary Tree Node inherits from the Node above (I know that it would be easier to not use inheritance and simply go with a BinaryTreeNode straight with data, but I chose to do it this way because I am practicing)</p>
  
  <p>The BinaryTreeNode class:</p>
  
  <pre><code>template &lt;class Type&gt;
  class BinaryTreeNode: public Node&lt;Type&gt; {
  protected:
  BinaryTreeNode&lt;Type&gt; *left;
  BinaryTreeNode&lt;Type&gt; *right;
  BinaryTreeNode&lt;Type&gt; *parent;
  public:
  BinaryTreeNode&lt;Type&gt;(Type data) : Node&lt;Type&gt;(data) {
      this-&gt;left = NULL;
      this-&gt;right = NULL;
      this-&gt;parent = NULL;
  }
  ~BinaryTreeNode&lt;Type&gt;() {
      delete left;
      delete right;
      delete parent;
  }
  };
  </code></pre>
  
  <p>For the Tree:</p>
  
  <pre><code>template &lt;class Type, template &lt;class&gt; class NodeType = BinaryTreeNode&gt;
  class BinaryTree {
  protected:
      int size;
      NodeType&lt;Type&gt; *root;
  public:
      ~BinaryTree&lt;Type, NodeType&gt;() {
          delete root;
      }
      BinaryTree&lt;Type, NodeType&gt;() {
          this-&gt;size = 0;
          this-&gt;root = NULL;
      }
  };
  </code></pre>
  
  <p>Now in main, I do the following:</p>
  
  <pre><code>BinaryTree&lt;int&gt; *bt = new BinaryTree&lt;int&gt;();
  bt-&gt;insert(100);
  bt-&gt;insert(50);
  bt-&gt;insert(101);
  </code></pre>
  
  <p>The above works, so far so good.</p>
  
  <p>The insert method/function is where creation of NEW nodes are done. I then tried using each of the following (one by one) and they all result in a segfault (core dump):</p>
  
  <pre><code>delete bt-&gt;getRoot();
  </code></pre>
  
  <p>This resulted in segfault. I then tried</p>
  
  <pre><code>delete bt-&gt;getRoot()-&gt;getRight();
  </code></pre>
  
  <p>again segfault. So last I tried</p>
  
  <pre><code>delete bt;
  </code></pre>
  
  <p>still segfault</p>
  
  <p>I was expecting to simply have memory leaks due to other nodes being unreachable (I am running it with valgrind) but to my surprise, valgrind crashed, and gcc and clang did not even print out any warnings even with -Wall. I need advise on how to do this correctly. Thanks in advance.</p>