title: Red Black Tree delete malfunction in C
body: <p>I have almost completed a red black tree but my delete is malfunctioning. It may delete a node it may not. After i delete the root and press the print option my screen is spammed of nodes that i already have in the tree. in a test tree of <code>2,1,7,5,8,11,14,15,4</code> if i delete <code>root=7</code> and press in-order print i get <code>2,4,5,8,1,2,4,5,8,1,.....</code> and so on until the program crashes. If i delete 2 the program instantly crashes. Node 11 deletes fine as do all leaves like 1-4-15. I've tried to find the problem with debugging but everything seems to work fine. The code is based on Cormen's introduction to algorithms. Thanks!   </p>
  
  <pre><code>void RB_delete(struct node* z,struct node* y) //delete z y=z on call
  {
      struct node *x;
      enum type originalcolor;         //x  moves into y's original position
      originalcolor=y-&gt;color;     // Keep track of original color
  
          if (z-&gt;LC==nill)        //case 1: (z has no LC)
          {
              x=z-&gt;RC;
              RB_transplant(z,z-&gt;RC); 
          else if (z-&gt;RC==nill)           //case 2: z has LC but no RC
          {
              x=z-&gt;LC;
              RB_transplant(z,z-&gt;LC);
          }
          else            // two cases: z has both Children
          {
              y=tree_minimum(z-&gt;RC);      //find  successor
              originalcolor=y-&gt;color;     //save color of successor
              x=y-&gt;RC;
                  if (y-&gt;P==z)    //successor has no LC cause its nill
                      x-&gt;P=y;
                  else
                  {
                      RB_transplant(y,y-&gt;RC);
                      y-&gt;RC=z-&gt;RC;
                      y-&gt;RC-&gt;P=y;
                  }
              RB_transplant(z,y);
              y-&gt;LC=z-&gt;LC;
              y-&gt;LC-&gt;P=y;
              y-&gt;color=z-&gt;color;
          }
          if (originalcolor == black)
              RB_delete_fix(x);
          free(z);
  }
  
  void io_print(struct node *aux,struct node *auxnill)
  {
      HANDLE  hConsole;
      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      if(aux != auxnill)
      {
          io_print(aux-&gt;LC,auxnill);
          if (aux-&gt;color==red)
          {
              SetConsoleTextAttribute(hConsole, 12);
              printf("%d,\n",aux-&gt;key);fflush(stdout);
              SetConsoleTextAttribute(hConsole, 15);
          }
          if (aux-&gt;color==black)
          {
              SetConsoleTextAttribute(hConsole, 9);
              printf("%d,\n",aux-&gt;key);fflush(stdout);
              SetConsoleTextAttribute(hConsole, 15);
          }
              io_print(aux-&gt;RC,auxnill);
      }
  }
  </code></pre>