Search code examples
cred-black-tree

Red Black Tree delete malfunction in C


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 2,1,7,5,8,11,14,15,4 if i delete root=7 and press in-order print i get 2,4,5,8,1,2,4,5,8,1,..... 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!

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->color;     // Keep track of original color

        if (z->LC==nill)        //case 1: (z has no LC)
        {
            x=z->RC;
            RB_transplant(z,z->RC); 
        else if (z->RC==nill)           //case 2: z has LC but no RC
        {
            x=z->LC;
            RB_transplant(z,z->LC);
        }
        else            // two cases: z has both Children
        {
            y=tree_minimum(z->RC);      //find  successor
            originalcolor=y->color;     //save color of successor
            x=y->RC;
                if (y->P==z)    //successor has no LC cause its nill
                    x->P=y;
                else
                {
                    RB_transplant(y,y->RC);
                    y->RC=z->RC;
                    y->RC->P=y;
                }
            RB_transplant(z,y);
            y->LC=z->LC;
            y->LC->P=y;
            y->color=z->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->LC,auxnill);
        if (aux->color==red)
        {
            SetConsoleTextAttribute(hConsole, 12);
            printf("%d,\n",aux->key);fflush(stdout);
            SetConsoleTextAttribute(hConsole, 15);
        }
        if (aux->color==black)
        {
            SetConsoleTextAttribute(hConsole, 9);
            printf("%d,\n",aux->key);fflush(stdout);
            SetConsoleTextAttribute(hConsole, 15);
        }
            io_print(aux->RC,auxnill);
    }
}

Solution

  • Well there was a single misplace of pointer in transplant... And i couldnt find it cause i didnt paid much attention. i had:

    void RB_transplant(struct node *aux,struct node *auxchild)  //replace the subtree rooted at node aux with the subtree rooted at aux-LC or aux->RC
    {
        if (aux->P==nill)                           //if aux=root child becomes root
            root=auxchild;
        else if (aux==aux->P->LC)               //if child is a LC
            aux->P->LC=auxchild;                
        else aux->P->RC=auxchild;                   //if child is RC //connect grandparent's->RC with child
            auxchild->P=aux->P;                         //connect child to point to parent
    }
    

    and the problem was in line 5 where aux was=>auxhild... Problem sovled :)