0

I am implementing red-black tree by c. I'm referring to pseudo codes in CLRS.

pseudo code: enter image description here

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;
YSEO
  • 101
  • 7
  • You're probably calling this function on a node that you shouldn't be calling it on. – Matt Timmermans Apr 03 '23 at 13:00
  • 2
    If the sibling is a leaf node then the current node _must_ be red for you to have a valid red-black tree. And in that case you should simply be able to remove the current node and not worry about the tree's balance. If the current node is black with a leaf sibling then you have more serious problems. – SoronelHaetir Apr 03 '23 at 16:41

0 Answers0