title: Why is this C++ code producing a segmentation fault?
body: <p>The code function that I'm specifically talking about is getCount(). There are several other functions that I haven't included here (such as finding the height of this binary tree and the total node count) which work just fine, with correct results. getCount() on the other hand produces segmentation fault except for the first node (the top, first node of the tree). Any ideas?</p>
  
  <pre><code>#include &lt;string&gt; 
  #include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  
  class Word {
      public:
          std::string keyval;
          long long count;
          Word() {
              keyval = "";
              count = 0;
          }
          Word(std::string S) {
              keyval = S;
              count = 1;
          }
  };
  
  class WordBST {
      public:
          Word node;
          WordBST* left_child;
          WordBST* right_child;
          WordBST(std::string key);        
          void add(std::string key){
              if (key == node.keyval){
                  node.count++;
              }
              else if (key &lt; node.keyval){
                  if (left_child == NULL){
                      left_child = new WordBST(key);
                  }else {
                      left_child-&gt;add(key);
                  }
              }else {
                  if (right_child == NULL){
                      right_child = new WordBST(key);
                  }else {
                      right_child-&gt;add(key);
                  }
              }
          }
          long long getCount(std::string key){
              if (key == node.keyval){
                  return (node.count);
              }
              else if (key &lt; node.keyval){
                  left_child-&gt;getCount(key);
              }else if(key &gt; node.keyval){
                  right_child-&gt;getCount(key);
              }else return 0;
              /*else {
                  if (key &lt; node.keyval){
                      left_child-&gt;getCount(key);
                  }else{
                      right_child-&gt;getCount(key);
                  }
              }*/
          }
  };
  
  WordBST::WordBST(std::string key) {
      node = Word(key);
      left_child = NULL;
      right_child = NULL;
  }
  </code></pre>