title: C++ - BST segmentation fault upon deletion of node with 2 children
body: <h2>Segmentation faults when trying to delete a node with 2 children</h2>
  <p>So i've recently started studying algorithms and have been working a bit on binary search trees, basing my C++ code on the pseudo code taken from the book <code>Introduction to Algorithms</code>. I'm however having large issues with it's delete function for it's binary search tree. The code for the delete function is:</p>
  <pre><code>void bstDelete(BSTtree &amp;bst, int key)
  {
      treeNode *z = findNode(bst, key);
      if (z != nullptr)
      {
          treeNode *y = z;
          treeNode *x;
          if (z-&gt;left == bst.NIL)
          {
              x = z-&gt;right;
              transplant(bst, z, z-&gt;right);
          }
          else if (z-&gt;right == bst.NIL)
          {
              x = z-&gt;left;
              transplant(bst, z, z-&gt;left);
          }
          else
          {
              y = treeMin(z-&gt;right);
              x = y-&gt;right;
              if (y-&gt;parent == z)
              {
                  x-&gt;parent = y;
              }
              else
              {
                  transplant(bst, y, y-&gt;right);
                  y-&gt;right = z-&gt;right;
                  y-&gt;right-&gt;parent = y;
              }
              transplant(bst, z, y);
              y-&gt;left = z-&gt;left;
              y-&gt;left-&gt;parent = y;  
          }
      }
  }
  </code></pre>
  <p>Transplant function:</p>
  <pre><code>void transplant(BSTtree &amp;bst, treeNode *u, treeNode *v)
  {
      if (u-&gt;parent == bst.NIL)
      {
          v-&gt;right = u-&gt;right;
          v-&gt;left = u-&gt;left;
  
          v-&gt;right-&gt;parent = v;
          v-&gt;left-&gt;parent = v;
          bst.root = v;
      }
      else if (u == u-&gt;parent-&gt;left)
      {
          u-&gt;parent-&gt;left = v;
      }
      else
      {
          u-&gt;parent-&gt;right = v;
      }
      v-&gt;parent = u-&gt;parent;
  }
  </code></pre>
  <p>I'm not expecting someone to debug my code, I've just sat pretty much for multiple days trying to figure it out, looking at other people's versions of the delete function trying to understand why mine doesn't work. There's also the issue of why the book's own delete function doesn't do it's job, or if it's just me who's at fault. I would really appreciate if someone could take a look at this. I've been looking around quite a lot and still haven't found anyone having issues with this.</p>
  <p><strong>Edit</strong><br>
  Added other parts related to the code</p>
  <pre><code>treeNode *findNode(RBtree bst, int key)
  {
      treeNode *z = nullptr;
      treeNode *x = bst.root;
      treeNode *y = bst.NIL;
      while (x != nullptr &amp;&amp; x != bst.NIL)
      {
          y = x;
          if (key &lt; x-&gt;value)
          {
              x = x-&gt;left;
          }
          else
          {
              x = x-&gt;right;
          }
  
          if (y-&gt;value == key)
          {
              break;
          }
      }
      if (y-&gt;value == key)
      {
          z = y;
      }
      return z;
  }
  </code></pre>
  <pre><code>treeNode *treeMin(treeNode *root)
  {
      treeNode *treeNode = root;
      while (treeNode-&gt;left != nullptr)
      {
          treeNode = treeNode-&gt;left;
      }
      return treeNode;
  }
  </code></pre>
  <pre><code>struct treeNode
  {
      treeNode *left;
      treeNode *right;
      treeNode *parent;
      std::string color;
      int value;
      treeNode() : left(nullptr), right(nullptr), parent(nullptr) {}
      treeNode(int paramValue) : left(nullptr), right(nullptr), parent(nullptr), value(paramValue) {}
      treeNode(int paramValue, treeNode *parent) : value(paramValue), parent(parent) {}
  };
  </code></pre>
  <pre><code>struct BSTtree
  {
      treeNode *root;
      treeNode *NIL;
      BSTtree()
      {
          NIL = new treeNode();
          root = nullptr;
      }
  };
  </code></pre>
  <p>Reason i have with NIL is so that i can easier make this into an RB tree later on. The smallest scale needed to cause a crash is a 3 node tree, where you have a root, and it has a right and left child. That way you push the delete function into case three, where it has to find a successor and transplant it. It's in this part of the delete-function which it crashes and gives me a segmentation-fault.</p>
  <p><strong>Edit 2</strong><br>
  Code which causes a crash:</p>
  <pre><code>int main()
  {
      BSTtree bst;
      bstInsert(bst, 5);
      bstInsert(bst, 7);
      bstInsert(bst, 2);
  
  
      bstDelete(bst, 5);
  }
  </code></pre>
  <p>Insert function</p>
  <pre><code>void bstInsert(BSTtree &amp;bst, int key)
  {
      treeNode *z = new treeNode(key);
      treeNode *y = bst.NIL;
      treeNode *x = bst.root;
      while (x != nullptr &amp;&amp; x != bst.NIL)
      {
          y = x;
          if (z-&gt;value &lt; x-&gt;value)
          {
              x = x-&gt;left;
          }
          else
          {
              x = x-&gt;right;
          }
      }
      z-&gt;parent = y;
      if (y == bst.NIL)
      {
          bst.root = z;
          z-&gt;parent = bst.NIL;
      }
      else if (z-&gt;value &lt; y-&gt;value)
      {
          y-&gt;left = z;
      }
      else
      {
          y-&gt;right = z;
      }
      z-&gt;left = bst.NIL;
      z-&gt;right = bst.NIL;
  }
  </code></pre>
  <p>The nullptr is not a problem here as it's resolved in the insert function, as you can see if it's root, root is given <code>bst.NIL</code> as it's parent. I agree it's error prone to have this construction, it's only there to be able to be used for red and black trees which uses a specific NIL node instead of nullptr.</p>