I am implementing red-black tree by c. I'm referring to pseudo codes in CLRS.
I wonder why there is no error handling in deletion's fixup when sibling is nil(Null). currently, there's no error handling when checking sibling's child, so that segmentation fault pops up when I run delete code.
Especially in here(Full codes are at the bottom):
if ((w->left->color == RBTREE_BLACK) && (w->right->color == RBTREE_BLACK))
so i am wondering how could i deal with this situation.. when i debug it, w is nil node so that there is no something like w->left or w->right
what am I missing?? please let me fix my mistakes
while (x != t->root && x->color == RBTREE_BLACK)
{
if (x == x->parent->left)
{
node_t* w = x->parent->right;
if (w->color == RBTREE_RED)
{
w->color = RBTREE_BLACK;
x->parent->color = RBTREE_RED;
rotate_left(t, x->parent);
w = x->parent->right;
}
if ((w->left->color == RBTREE_BLACK) && (w->right->color == RBTREE_BLACK))
{
w->color = RBTREE_RED;
x = x->parent;
}
else {
if (w->right->color == RBTREE_BLACK)
{
w->left->color = RBTREE_BLACK;
w->color = RBTREE_RED;
rotate_right(t, w);
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = RBTREE_BLACK;
w->right->color = RBTREE_BLACK;
rotate_left(t, x->parent);
x = t->root;
}
}
else { same as then clause with “right” and “left” exchanged ..}
x->color = RBTREE_BLACK;