title: Seg fault in my delete operation in c++
body: <p>i am writing a code for a c++ assignment, it is a dictionary implementation using a binary search tree. My code compiles but when i try to "remove" i get a seg Fault. any ideas why did might be happening. Thanks</p>
  
  <p>Here is my code</p>
  
  <pre><code>// this function calls the deleteNode function where the deletion is done
  void BST::deleteContent(string *word)
  {
      deleteNode(word, root);
  }
  // a helper fuuntion for the deletecontent function
  //uses recursion to find the node to be deleted
  void BST::deleteNode(const string *word, Node *&amp;nodePtr)
  {
      if(word &lt; nodePtr-&gt;word)
          deleteNode(word, nodePtr-&gt;left);
      else if(word &gt; nodePtr-&gt;word)
          deleteNode(word, nodePtr-&gt;right);
      else
          makeDeletion(nodePtr);
  }
  // a helper function for the deleteNode function
  void BST::makeDeletion(Node *&amp;nodePtr)
  {
      Node *tempNodePtr;
  
      if(nodePtr == NULL)
          cout&lt;&lt; "cannot delete empty node. \n";
      // if node has no right child
      else if (nodePtr-&gt;right == NULL)
          {
              tempNodePtr = nodePtr;
              nodePtr = nodePtr-&gt;left; // reattach child
              delete tempNodePtr;
          }
      else if(nodePtr-&gt; left == NULL)
          {
              tempNodePtr = nodePtr;
              nodePtr = nodePtr-&gt;right; // reattach child
              delete tempNodePtr;
          }
      // if node has 2 children
      else
          {
              tempNodePtr = nodePtr-&gt;right;
              while (tempNodePtr-&gt;left)
                  tempNodePtr = tempNodePtr-&gt;left;
              tempNodePtr-&gt;left = nodePtr-&gt;left;
              tempNodePtr = nodePtr;
              nodePtr = nodePtr-&gt;right;
              delete tempNodePtr;
          }
  }
  </code></pre>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>Thank you all!! From your post i realised it was a good idea to check if the node was the last and had no children. I added this check in deleteNode  </p>
  
  <pre><code>    if((nodePtr-&gt;left) &amp;&amp; word &lt; nodePtr-&gt;word)
      {
          do something
      }
  </code></pre>
  
  <p>i did the same for the right 
  it worked and did not throw any errors or seg faults. Thanks so much!!!! </p>