title: Calculating Height of Binary Search Tree
body: <p>I've been searching for how to calculate the height of a Binary Search Tree and my research has lead me to the following implementation. I'm still trying to wrap my head around why it should work, but I'm also unsure why it isn't working. Here is my height function.</p>
  
  <pre><code>int BinaryTreeNode::height() const {
      int lefth = left-&gt;height();
      int righth = right-&gt;height();
      if(lefth &gt; righth) {
          return lefth + 1;
      } else {
          return righth + 1;
      }
  }
  </code></pre>
  
  <p>and here's my class definition for the node</p>
  
  <pre><code>class BinaryTreeNode {
    public:
      Data * nodeData;
      BinaryTreeNode * left;
      BinaryTreeNode * right;
  </code></pre>
  
  <p>When I try to run it my program locksup and crashes. Am I missing something obvious?</p>
  
  <p>EDIT: Why shouldn't this work?</p>
  
  <pre><code>int BinaryTreeNode::height() const {
      int l = 0;
      if (left != NULL) {
          left-&gt;height();
      }
      int r = 0;
      if (right != NULL) {
          right-&gt;height();
      }
      if (l &gt; r) {
          return l + 1;
      }
      else {
          return r + 1;
      }
  }
  </code></pre>