Beispiel #1
0
void delete_case5(rb_node* n, rb_tree* tree) {
	rb_node* s = sibling(n);
	
	/* this if statement is trivial, 
	due to Case 2 (even though Case two changed the sibling to a sibling's child, 
	the sibling's child can't be red, since no red parent can have a red child). */
	/* the following statements just force the red to be on the left of the left of the parent, 
	or right of the right, so case six will rotate correctly. */
	if	(s->color == BLACK) { 
		if ((n == n->parent->left) &&
		(s->right->color == BLACK) &&
		(s->left->color == RED)) { /* this last test is trivial too due to cases 2-4. */
			s->color = RED;
			s->left->color = BLACK;
			rotate_right(s, tree);
		} else if ((n == n->parent->right) &&
		(s->left->color == BLACK) &&
		(s->right->color == RED)) {/* this last test is trivial too due to cases 2-4. */
			s->color = RED;
			s->right->color = BLACK;
			rotate_left(s, tree);
		}
	}
	delete_case6(n, tree);
}
Beispiel #2
0
/**
 * Do following while the current node u is double black or it is not root. Let sibling of node be s.
 * @brief \n(a): If sibling s is black and at least one of sibling’s children is red, perform rotation(s). Let the red child of s be r. This case can be divided in four subcases depending upon positions of s and r.
 * \n(i) Left Left Case (s is left child of its parent and r is left child of s or both children of s are red). This is mirror of right right case shown in below diagram.
 * \n(ii) Left Right Case (s is left child of its parent and r is right child). This is mirror of right left case shown in below diagram.
 * \n(iii) Right Right Case (s is right child of its parent and r is right child of s or both children of s are red)
 * \n(iv) Right Left Case (s is right child of its parent and r is left child of s)
 * @param t is the tree root.
 * @param n is the node at which deletion is taking place.
 */
void delete_case5(rbtree t, node n)
{
    if (n == n->parent->left && node_color(sibling(n)) == BLACK && node_color(sibling(n)->left) == RED && node_color(sibling(n)->right) == BLACK)
    {
        sibling(n)->color = RED;
        sibling(n)->left->color = BLACK;
        rotate_right(t, sibling(n));
    }
    else if (n == n->parent->right && node_color(sibling(n)) == BLACK && node_color(sibling(n)->right) == RED && node_color(sibling(n)->left) == BLACK)
    {
        sibling(n)->color = RED;
        sibling(n)->right->color = BLACK;
        rotate_left(t, sibling(n));
    }
    delete_case6(t, n);
}
Beispiel #3
0
static void delete_case5(L_RBTREE *t, node *n) {
    if (n == n->parent->left &&
        node_color(sibling(n)) == L_BLACK_NODE &&
        node_color(sibling(n)->left) == L_RED_NODE &&
        node_color(sibling(n)->right) == L_BLACK_NODE) {
        sibling(n)->color = L_RED_NODE;
        sibling(n)->left->color = L_BLACK_NODE;
        rotate_right(t, sibling(n));
    } else if (n == n->parent->right &&
               node_color(sibling(n)) == L_BLACK_NODE &&
               node_color(sibling(n)->right) == L_RED_NODE &&
               node_color(sibling(n)->left) == L_BLACK_NODE) {
        sibling(n)->color = L_RED_NODE;
        sibling(n)->right->color = L_BLACK_NODE;
        rotate_left(t, sibling(n));
    }
    delete_case6(t, n);
}
Beispiel #4
0
void delete_case5(struct rbtree *t, rbtree_node *n)
{
    if (n == n->parent->left &&
            get_color(sibling(n)) ==RB_BLACK &&
            get_color(sibling(n)->left) == RB_RED &&
            get_color(sibling(n)->right) == RB_BLACK)
    {
        sibling(n)->color = RB_RED;
        sibling(n)->left->color =RB_BLACK;
        rotate_right(sibling(n),t);
    }
    else if (n == n->parent->right &&
            get_color(sibling(n)) == RB_BLACK &&
            get_color(sibling(n)->right) == RB_RED &&
            get_color(sibling(n)->left) == RB_BLACK)
    {
        sibling(n)->color = RB_RED;
        sibling(n)->right->color = RB_BLACK;
        rotate_left(sibling(n),t);
    }
    delete_case6(t, n);
}
Beispiel #5
0
static inline void
delete_case5(struct RBTREE_TYPENAME* target, struct RBTREE_NODE* P,
             struct RBTREE_NODE* N, struct RBTREE_NODE* S)
{
  valgrind_assert(S->color == BLACK);
  if ((N == P->left) &&
      (S->right == NULL || S->right->color == BLACK))
  {
    valgrind_assert(S->left->color == RED);
    S->color = RED;
    S->left->color = BLACK;
    rotate_right(target, S);
  }
  else if ((N == P->right) &&
           (S->left == NULL || S->left->color == BLACK))
  {
    valgrind_assert(S->right->color == RED);
    S->color = RED;
    S->right->color = BLACK;
    rotate_left(target, S);
  }
  delete_case6(target, N);
}
Beispiel #6
0
void Tree::delete_case5()
{
	//if sibling is black
	if(!(sibling()->red))
	{
		//if siblings family has a single red, we have to make sure that the opposite
		//child of the sibling is red which case 6 solvse
		if(this == parent->left && !sibling()->right->red && sibling()->left->red)
		{
			sibling()->red = true;
			sibling()->left->red = false;
			rotate_right(sibling());
		}
		else if(this == parent->right && 
			!sibling()->left->red && sibling()->right->red)
		{
			sibling()->red = true;
			sibling()->right->red = false;
			rotate_left(sibling());
		}
	}
	delete_case6();
}