Пример #1
0
Файл: tree.c Проект: lchish/cosc
/**
 * Balances red black trees.
 * @param t tree to balance.
 * @return balanced tree.
 */
static tree tree_fix(tree t)
{
  if(IS_RED(t->left) && IS_RED(t->left->left)){
    if(IS_RED(t->right)){
      /*colour root red and children a,b black*/
      t->colour = RED;
      t->left->colour = BLACK;
      t->right->colour = BLACK;
    }else if(IS_BLACK(t->right)){
      /*right rotate root , colour new root (a) black,
       * and new child(old root) red*/
      t = right_rotate(t);
      t->colour = BLACK;
      t->right->colour = RED;/*old root*/
    }
  }else if(IS_RED(t->left) && IS_RED(t->left->right)){
    if(IS_RED(t->right)){
      /*colour root red and children a,b black*/
      t->colour = RED;
      t->left->colour = BLACK;
      t->right->colour = BLACK;
    }
    else if(IS_BLACK(t->right)){
      /* Left rotate left child (a), right rotate root (r),
       * colour new root (d) black and new child (R) red */
      t->left = left_rotate(t->left);
      t = right_rotate(t);
      t->colour = BLACK;
      t->right->colour = RED;/* old root */
    }
  }else if(IS_RED(t->right) && IS_RED(t->right->left)){
    if(IS_RED(t->left)){
      /* Colour root (R) red and children (a,b) black*/
      t->colour = RED;
      t->left->colour = BLACK;
      t->right->colour = BLACK;
    }else if(IS_BLACK(t->left)){
      /* Right rotate right child(b),left rotate root(r),
       * colour new root (e) black and new child (r) red */
      t->right = right_rotate(t->right);
      t = left_rotate(t);
      t->colour = BLACK;
      t->left->colour = RED;/* old root */
    }
  }else if(IS_RED(t->right) && IS_RED(t->right->right)){
    if(IS_RED(t->left)){
      /* Colour root (R) red and children (A,B) black */
      t->colour = RED;
      t->left->colour = BLACK;
      t->right->colour = BLACK;
    }
    else if(IS_BLACK(t->left)){
      /* Left rotate root R, colour new root b black and new child R red */
      t = left_rotate(t);
      t->colour = BLACK;
      t->left->colour = RED;/*old root*/
    }
  }
  return t;
}
Пример #2
0
int maintain(int &t,int flag)
{
	if (flag==0)//
	{
		if (SBT[SBT[SBT[t].left].left].s>SBT[SBT[t].right].s)
			right_rotate(t);
		else if (SBT[SBT[SBT[t].left].right].s>SBT[SBT[t].right].s)
		{
			left_rotate(SBT[t].left);
			right_rotate(t);
		}
		else return t;
	}
	else
	{
		if (SBT[SBT[SBT[t].right].right].s>SBT[SBT[t].left].s)
			left_rotate(t);
		else if (SBT[SBT[SBT[t].right].left].s>SBT[SBT[t].left].s)
		{
			right_rotate(SBT[t].right);
			left_rotate(t);
		}
		else return t;
	}
	maintain(SBT[t].left,0);
	maintain(SBT[t].right,1);
	maintain(t,0);
	maintain(t,1);
	return t;
}
Пример #3
0
struct node *balance(struct node *t )
{ unsigned int tmp ;
  unsigned int tmp___0 ;
  unsigned int tmp___1 ;
  unsigned int tmp___2 ;
  unsigned int tmp___3 ;
  unsigned int tmp___4 ;
  unsigned int tmp___5 ;
  unsigned int tmp___6 ;

  {
  tmp___5 = height(t->left);
  tmp___6 = height(t->right);
  if (tmp___5 > 1U + tmp___6) {
    tmp = height((t->left)->left);
    tmp___0 = height((t->left)->right);
    if (tmp < tmp___0) {
      t->left = left_rotate(t->left);
    }
    t = right_rotate(t);
  } else {
    tmp___3 = height(t->left);
    tmp___4 = height(t->right);
    if (tmp___3 + 1U < tmp___4) {
      tmp___1 = height((t->right)->left);
      tmp___2 = height((t->right)->right);
      if (tmp___1 > tmp___2) {
        t->right = right_rotate(t->right);
      }
      t = left_rotate(t);
    }
  }
  return (t);
}
}
/* rebalance_AVL: checks if the left and right branches of the tree are even,
* if they are not. Check which rotation needs to be carried out and do it.
*
* Params: the AVL root node, and last value entered.
*
* Returns: a newly balanced tree if rebalancing is needed, or just returns
* the current root if no balancing is necessary.
*/
static AVL rebalance_AVL(AVL self, int value)
{
	int balance;

	/* update the height of the ancestor */
	self->height = maximum(height(self->left), height(self->right)) + 1;

	/* check that the left and right subtrees are balanced. */
	balance = check_balance(self);

	/* check for left rotate */
	if (balance > 1 && value < self->student_id) {
		return right_rotate(self);
	}
	/* check for right rotate */
	if (balance < -1 && value > self->student_id) {
		return left_rotate(self);
	}
	/* check for double rotate (left then right) */
	if (balance > 1 && value > self->left->student_id) {
		self->left = left_rotate(self);
		return right_rotate(self);
	}
	/* check for double rotate (right then left) */
	if (balance < -1 && value < self->right->student_id) {
		self->right = right_rotate(self);
		return left_rotate(self);
	}
	return self;
}
Пример #5
0
/* Fixup the balance of the btree after deletion    */
static void btree_delete_fixup(opal_rb_tree_t *tree, opal_rb_tree_node_t * x)
{
    opal_rb_tree_node_t * w;
    opal_rb_tree_node_t * root = tree->root_ptr->left;
    while ((x != root) && (x->color == BLACK)) {
        if (x == x->parent->left) {
            w = x->parent->right;
            if (w->color == RED) {
                w->color = BLACK;
                x->parent->color = RED;
                left_rotate(tree, x->parent);
                w = x->parent->right;
            }
            if ((w->left->color == BLACK) && (w->right->color == BLACK)) {
                w->color = RED;
                x = x->parent;
            } else {
                if (w->right->color == BLACK) {
                    w->left->color = BLACK;
                    w->color = RED;
                    right_rotate(tree, w);
                    w = x->parent->right;
                }
                w->color = x->parent->color;
                x->parent->color = BLACK;
                w->right->color = BLACK;
                left_rotate(tree, x->parent);
                x = root;
            }
        } else { /* right    */

            w = x->parent->left;
            if (w->color == RED) {
                w->color = BLACK;
                x->parent->color = RED;
                right_rotate(tree, x->parent);
                w = x->parent->left;
            }
            if ((w->right->color == BLACK) && (w->left->color == BLACK)) {
                w->color = RED;
                x = x->parent;
            } else {
                if (w->left->color == BLACK) {
                    w->right->color = BLACK;
                    w->color = RED;
                    left_rotate(tree, w);
                    w = x->parent->left;
                }
                w->color = x->parent->color;
                x->parent->color = BLACK;
                w->left->color = BLACK;
                right_rotate(tree, x->parent);
                x = root;
            }
        }
    }

    x->color = BLACK;
    return;
}
Пример #6
0
Файл: tree.c Проект: Limaa/libds
static void rb_delete_fixup(tree_p tr, tnode_p parent, tnode_p node){
	tnode_p sibling;
	while(node != tr->root && rb_color(node) == BLACK){
		if( node == parent->left ){
			sibling = parent->right;
			if(rb_color(sibling) == RED){
				sibling->color = BLACK;
				parent->color = RED;
				left_rotate(tr, parent);
				sibling = parent->right;
			}

			if(rb_color(sibling->left) == BLACK 
					&& rb_color(sibling->right) == BLACK){
				sibling->color = RED;
				node = parent;
				parent = parent->parent;
			} else if(rb_color(sibling->right) == BLACK){
				sibling->left->color = RED;
				right_rotate(tr, sibling);
				sibling = parent->right;
			} else {
				sibling->color = parent->color;
				parent->color = BLACK;
				sibling->right->color = BLACK;
				left_rotate(tr, parent);
				node = tr->root;
				parent = NULL;
			}
		}
		else{
			sibling = parent->left;
			if(rb_color(sibling) == RED){
				sibling->color = BLACK;
				parent->color = RED;
				right_rotate(tr, parent);
				sibling = parent->left;
			}

			if(rb_color(sibling->left) == BLACK 
					&& rb_color(sibling->right) == BLACK){
				sibling->color = RED;
				node = parent;
				parent = parent->parent;
			} else if(rb_color(sibling->left) == BLACK){
				sibling->right->color = RED;
				left_rotate(tr, sibling);
				sibling = parent->left;
			} else {
				sibling->color = parent->color;
				parent->color = BLACK;
				sibling->left->color = BLACK;
				right_rotate(tr, parent);
				node = tr->root;
				parent = NULL;
			}
		}
	}
	if(node != NULL) node->color = BLACK;
}
Пример #7
0
void rb_tree<T>::remove_fixup(rb_vertex<T>* current_vertex) {
    // current_vertex is x in Corman

    // this is w in Corman
    rb_vertex<T>* child;

    while (current_vertex != root && current_vertex->get_colour() == BLACK) {
        if (current_vertex == current_vertex->get_parent()->get_left_child()) {
            child = current_vertex->get_parent()->get_right_child();
            if (child->get_colour() == RED) {
                child.set_colour(BLACK);
                current_vertex->get_parent()->set_colour(RED);
                left_rotate(current_vertex->get_parent);
                child = current_vertex->get_parent->get_right_child();
            }
            if (child->get_left_child()->get_colour() == BLACK && child->get_right_child()->get_colour() == BLACK) {
                child->set_colour(RED);
                current_vertex = current_vertex->get_parent();
            } else {
                if (child->get_right_child()->get_colour() == BLACK) {
                    child->get_left_child()->set_colour(BLACK);
                    child->set_colour(RED);
                    right_rotate(child);
                    child = current_vertex->get_parent()->get_right_child();
                }
                child->set_colour(current_vertex->get_parent()->get_colour());
                current_vertex->get_parent()->set_colour(BLACK);
                child->get_right_child()->set_colour(BLACK);
                left_rotate(current_vertex->get_parent());
                current_vertex = root;
            }
        } else {
            child = current_vertex->get_parent()->get_left_child();
            if (child->get_colour() == RED) {
                child.set_colour(BLACK);
                current_vertex->get_parent()->set_colour(RED);
                right_rotate(current_vertex->get_parent);
                child = current_vertex->get_parent->get_left_child();
            }
            if (child->get_left_child()->get_colour() == BLACK && child->get_right_child()->get_colour() == BLACK) {
                child->set_colour(RED);
                current_vertex = current_vertex->get_parent();
            } else {
                if (child->get_left_child()->get_colour() == BLACK) {
                    child->get_right_child()->set_colour(BLACK);
                    child->set_colour(RED);
                    left_rotate(child);
                    child = current_vertex->get_parent()->get_left_child();
                }
                child->set_colour(current_vertex->get_parent()->get_colour());
                current_vertex->get_parent()->set_colour(BLACK);
                child->get_left_child()->set_colour(BLACK);
                right_rotate(current_vertex->get_parent());
                current_vertex = root;
            }
        }
    }

    current_vertex->set_colour(BLACK);
}
Пример #8
0
void RBTree::remove_fixup(rbnode * x)
{
    while (x != root && x->color == BLACK) {
        rbnode * p = x->parent;
        if (x == p->lc) { // LEFT CHILD
            rbnode * w = p->rc;
            if (w->color == RED) {
                p->color = RED;
                w->color = BLACK;
                left_rotate(p);
                w = p->rc;
            }

            if (w->lc->color == BLACK && w->rc->color == BLACK) {
                w->color = RED;
                x = p;
            } else {
                if (w->rc->color == BLACK) { // w->rc is red
                    w->color = RED;
                    w->lc->color = BLACK;
                    w = right_rotate(w);
                }

                w->color = p->color;
                p->color = BLACK;
                w->rc->color = BLACK;
                left_rotate(p);

                x = root;
            }
        } else { // right child
            rbnode * w = p->lc;
            if (w->color == RED) {
                p->color = RED;
                w->color = BLACK;
                right_rotate(p);
                w = p->lc;
            }

            if (w->lc->color == BLACK && w->rc->color == BLACK) {
                w->color = RED;
                x = p;
            } else {
                if (w->lc->color == BLACK) { // w->rc is red
                    w->color = RED;
                    w->rc->color = BLACK;
                    w = left_rotate(w);
                }

                w->color = p->color;
                p->color = BLACK;
                w->lc->color = BLACK;
                right_rotate(p);

                x = root;
            }
        }
    }
    x->color = BLACK;
}
Пример #9
0
void
rb_delete_fixup(RB_TREE *T, RB_NODE *x)
{
    RB_NODE     *w;

    while (x != T->root && x->color == BLACK) {
        if (x == x->parent->left) {
            w = x->parent->right;
            if (w->color == RED) {
                w->color = BLACK;                               //case 1
                x->parent->color = RED;                         //case 1
                left_rotate(T, x->parent);                      //case 1
                w = x->parent->right;                           //case 1
            }

            if (w->left->color == BLACK && w->right->color == BLACK) {
                w->color = RED;                                 //case 2
                x = x->parent;                                  //case 2
            } else if (w->right->color == BLACK) {
                w->left->color = BLACK;                         //case 3
                w->color = RED;                                 //case 3
                right_rotate(T, w);                             //case 3
                w = x->parent->right;                           //case 3
            }
            w->color = x->parent->color;                        //case 4
            x->parent->color = BLACK;                           //case 4
            w->right->color = BLACK;                            //case 4
            left_rotate(T, x->parent);                          //case 4
            x = T->root;                                        //case 4
        } else {
            if (x == x->parent->right) {
                w = x->parent->left;
                if (w->color == RED) {
                    w->color = BLACK;
                    x->parent->color = RED;
                    right_rotate(T, x->parent);
                    w = x->parent->left;
                }

                if (w->right->color == BLACK && w->left->color == BLACK) {
                    w->color = RED;
                    x = x->parent;
                } else if (w->left->color == BLACK) {
                    w->right->color = BLACK;
                    w->color = RED;
                    left_rotate(T, w);
                    w = x->parent->left;
                }
                w->color = x->parent->color;
                x->parent->color = BLACK;
                w->left->color = BLACK;
                right_rotate(T, x->parent);
                x = T->root;
            }
        }
    }
    x->color = BLACK;
}
Пример #10
0
Файл: rbt.c Проект: lchish/cosc
static rbt rbt_fix(rbt r)
{
  if(IS_RED(r->left) && IS_RED(r->left->left)){
    if(IS_RED(r->right)){
      /*colour root red and children a,b black*/
      r->colour = RED;
      r->left->colour = BLACK;
      r->right->colour = BLACK;
    }else if(IS_BLACK(r->right)){
      /*right rotate root , colour new root (a) black, and new child(old root) red*/
      r = right_rotate(r);
      r->colour = BLACK;
      r->right->colour = RED;/*old root?*/
    }
  }else if(IS_RED(r->left) && IS_RED(r->left->right)){
    if(IS_RED(r->right)){
      /*colour root red and children a,b black*/
      r->colour = RED;
      r->left->colour = BLACK;
      r->right->colour = BLACK;
    }
    else if(IS_BLACK(r->right)){
      /*left rotate left child (a), right rotate root (r),colour new root (d) black and new child (R) red*/
      r->left = left_rotate(r->left);
      r = right_rotate(r);
      r->colour = BLACK;
      r->right->colour = RED;/*old root?*/
    }
  }else if(IS_RED(r->right) && IS_RED(r->right->left)){
    if(IS_RED(r->left)){
      /* colour root (R) red and children (a,b) black*/
      r->colour = RED;
      r->left->colour = BLACK;
      r->right->colour = BLACK;
    }else if(IS_BLACK(r->left)){
      /*right rotate right child(b),left rotate root(r),colour new root (e) black and new child (r) red*/
      r->right = right_rotate(r->right);
      r = left_rotate(r);
      r->colour = BLACK;
      r->left->colour = RED;/*old root?*/
    }
  }else if(IS_RED(r->right) && IS_RED(r->right->right)){
    if(IS_RED(r->left)){
      /*colour root (R) red and children (A,B) black*/
      r->colour = RED;
      r->left->colour = BLACK;
      r->right->colour = BLACK;
    }
    else if(IS_BLACK(r->left)){
      /*left rotate root R, colour new root b black and new child R red*/
      r = left_rotate(r);
      r->colour = BLACK;
      r->left->colour = RED;/*old root?*/
    }
  }
  return r;
}
Пример #11
0
static void
k_rbtree_delete_fixup(k_rbtree_t* t,k_rbnode_t* x)
{
        k_rbnode_t* w;// w is x's brother
        while( (x != t->root)&& (x->color == k_color_black) ){

                if(x == x->parent->left){
                        w = x->parent->right;
                        if(w->color == k_color_red){
                                w->color = k_color_black;
                                x->parent->color = k_color_red;
                                left_rotate(t, x->parent);
                                w = x->parent->left;
                        }
                        if(w->left->color == k_color_black &&
                           w->right->color == k_color_black){
                                w->color = k_color_red;
                                x = x->parent;
                        }else if (w->right->color == k_color_black){
                                w->left->color = k_color_black;
                                w->color = k_color_red;
                                right_rotate(t, w);
                                w = x->parent->right;
                        }
                        w->color = x->parent->color;
                        w->parent->color = k_color_black;
                        left_rotate(t, x->parent);
                        x = t->root;

                }else{//x == x->parent->right;
                        w = x->parent->left;
                        if(w->color == k_color_red){
                                w->color = k_color_black;
                                x->parent->color = k_color_red;
                                right_rotate(t,x->parent);
                                w = x->parent->right;
                        }
                        if(w->right->color == k_color_black &&
                           w->left->color == k_color_black){
                                w->color = k_color_red;
                                x = x->parent;
                        }else if ( w->left->color = k_color_black){
                                w->right->color = k_color_black;
                                w->color = k_color_red;
                                left_rotate(t,w);
                                w = w->parent->left;
                        }
                        w->color = x->parent->color;
                        w->parent->color = k_color_black;
                        right_rotate(t, x->parent);
                        x = t->root;

                }

        }
        x->color = k_color_black;
}
Пример #12
0
 void _redblacktree::DeleteFixup(tree *x){
     tree *w;
     while (x != root && x->color == black) {
         if (x == x->parent->left) {
             w = x->parent->right;
             if (w->color == red) {
                 w->color = black;
                 x->parent->color = red;
                 left_rotate(x->parent);
                 w = x->parent->right;
                 
             }
             if (w->left->color == black &&  w->right->color == black) {
                 w->color = red;
                 x = x->parent;
             } else {
                 if (w->right->color == black) {
                     w->left->color = black;
                     w->color = red;
                     right_rotate(w);
                     w = x->parent->right;
                 }
                 w->color = x->parent->color;
                 x->parent->color =black;
                 w->right->color = black;
                 left_rotate(x->parent);
                 x = root;
             }
         } else {
             w = x->parent->right;
             if (w->color == red) {
                 w->color = black;
                 x->parent->color = red;
                 right_rotate(x->parent);
                 w = x->parent->left;
                 
             }
             if (w->right->color == black &&  w->left->color == black) {
                 w->color = red;
                 x = x->parent;
             } else {
                 if (w->left->color == black) {
                     w->right->color = black;
                     w->color = red;
                     left_rotate(w);
                     w = x->parent->left;
                 }
                 w->color = x->parent->color;
                 x->parent->color =black;
                 w->left->color = black;
                 right_rotate(x->parent);
                 x = root;
             }
         }
     }
     x->color = black;
 }
Пример #13
0
void rb_delete_fixup(rbt *T, rbn *x)
{
	rbn *w, *y;
	while (x != T->root && x->color == BLACK)
	{
		if (x == x->p->left) {
			w = x->p->right;
			if (w->color == RED) {
				w->color = BLACK;
				x->p->color = RED;
				left_rotate(T, x->p);
				w = x->p->right;
			}
			if (w->left->color == BLACK && w->right->color == BLACK) {
				w->color = RED;
				x = x->p;
			} else {
				if (w->right->color == BLACK) {
					w->left->color = BLACK;
					w->color = RED;
					right_rotate(T, w);
					w = x->p->right;
				}
				w->color = x->p->color;
				x->p->color = BLACK;
				w->right->color = BLACK;
				left_rotate(T, x->p);
				x = T->root;
			}
		} else {
			w = x->p->left;
			if (w->color == RED) {
				w->color = BLACK;
				x->p->color = RED;
				right_rotate(T, x->p);
				w = x->p->left;
			}
			if (w->left->color == BLACK && w->right->color == BLACK) {
				w->color = RED;
				x = x->p;
			} else {
				/*dbg(x, w);*/
				if (w->left->color == BLACK) {
					w->right->color = BLACK;
					w->color = RED;
					left_rotate(T, w);
					w = x->p->left;
				}
				w->color = x->p->color;
				x->p->color = BLACK;
				w->left->color = BLACK;
				right_rotate(T, x->p);
				x = T->root;
			}
		}
	}
}
Пример #14
0
static void
delete_fixup(struct rbtree *rbt, struct rbnode *nd)
{
    struct rbnode *tmp = &rbt->nil;
    while (nd != rbt->root && nd->color == BLACK)
        if (nd == nd->parent->left) {
            tmp = nd->parent->right;
            if (tmp->color == RED) {
                tmp->color = BLACK;
                nd->parent->color = RED;
                left_rotate(rbt, nd->parent);
                tmp = nd->parent->right;
            }
            if (tmp->left->color == BLACK && tmp->right->color == BLACK) {
                tmp->color = RED;
                nd = nd->parent;
            } else {
                if (tmp->right->color == BLACK) {
                    tmp->left->color = BLACK;
                    tmp->color = RED;
                    right_rotate(rbt, tmp);
                    tmp = nd->parent->right;
                }
                tmp->color = nd->parent->color;
                nd->parent->color = BLACK;
                tmp->right->color = BLACK;
                left_rotate(rbt, nd->parent);
                nd = rbt->root; //end while
            }
        } else {
            tmp = nd->parent->left;
            if (tmp->color == RED) {
                tmp->color = BLACK;
                nd->parent->color = RED;
                right_rotate(rbt, nd->parent);
                tmp = nd->parent->left;
            }
            if (tmp->right->color == BLACK && tmp->left->color == BLACK) {
                tmp->color = RED;
                nd = nd->parent;
            } else {
                if (tmp->left->color == BLACK) {
                    tmp->right->color = BLACK;
                    tmp->color = RED;
                    left_rotate(rbt, tmp);
                    tmp = nd->parent->left;
                }
                tmp->color = nd->parent->color;
                nd->parent->color = BLACK;
                tmp->left->color = BLACK;
                right_rotate(rbt, nd->parent);
                nd = rbt->root; //end while
            }
        }
    nd->color = BLACK;
}
Пример #15
0
void SplayTree<T>::splay(SplayNode<T> *x, SplayNode<T> *y)
{
    while(x->father != y)
    {
        SplayNode<T> * p = x->father;

        if (p->father == y)
        {
            // 因为p的父亲是y,所以只需要Zig操作,就可以使得x的父亲变成y
            if (p->left == x)
            {
                right_rotate(x);
            }
            else
            {
                left_rotate(x);
            }
        }
        else
        {
            SplayNode<T> * g = p->father;

            if (g->left == p)
            {
                if (p->left == x)
                {
                    // x, p同为左儿子,zig-zig操作
                    right_rotate(p);
                    right_rotate(x);
                }
                else
                {
                    //p为左, x为右,zig-zag操作
                    left_rotate(x);
                    right_rotate(x);
                }
            }
            else
            {
                if (p->right == x)
                {
                    // x, p同为右儿子,zig-zig操作
                    left_rotate(p);
                    left_rotate(x);
                }
                else
                {
                    //p为右, x为左,zig-zag操作
                    right_rotate(x);
                    left_rotate(x);
                }

            }
        }
    }
}
Пример #16
0
static void bbtree_insert_fixup( bbtree_t *tree,
                                 bbtree_node_t *node )
{
    bbtree_node_t *node2;
    bbtree_node_t *nil = &tree->nil;

    while (node != tree->root && node->parent->colour == BBTREE_RED)
    { 
        if (node->parent == node->parent->parent->left)
        { 
            node2 = node->parent->parent->right;
            if (node2 != nil && node2->colour == BBTREE_RED)
            { 
                node->parent->colour = BBTREE_BLACK;
                node2->colour = BBTREE_BLACK;
                node->parent->parent->colour = BBTREE_RED;
                node = node->parent->parent;
            }
            else
            {
                if (node == node->parent->right)
                {
                    node = node->parent;
                    left_rotate( tree, node );
                }
                node->parent->colour = BBTREE_BLACK;
                node->parent->parent->colour = BBTREE_RED;
                right_rotate( tree, node->parent->parent );
            }
        }
        else
        {
            node2 = node->parent->parent->left;
            if (node2 != nil && node2->colour == BBTREE_RED)
            { 
                node->parent->colour = BBTREE_BLACK;
                node2->colour = BBTREE_BLACK;
                node->parent->parent->colour = BBTREE_RED;
                node = node->parent->parent;
            }
            else
            {
                if (node == node->parent->left)
                {
                    node = node->parent;
                    right_rotate( tree, node );
                }
                node->parent->colour = BBTREE_BLACK;
                node->parent->parent->colour = BBTREE_RED;
                left_rotate( tree, node->parent->parent );
            }
        }
    }
    tree->root->colour = BBTREE_BLACK;
}
Пример #17
0
/*rbtree_delete_fixup*/
static inline void
rbtree_delete_fixup(mln_sarbt_t *t, mln_sarbt_node_t *n)
{
    mln_sarbt_node_t *tmp;
    while ((n != t->root) && (n->color == M_SARB_BLACK)) {
        if (n == n->parent->left) {
            tmp = n->parent->right;
            if (tmp->color == M_SARB_RED) {
                tmp->color = M_SARB_BLACK;
                n->parent->color = M_SARB_RED;
                left_rotate(t, n->parent);
                tmp = n->parent->right;
            }
            if ((tmp->left->color == M_SARB_BLACK) && (tmp->right->color == M_SARB_BLACK)) {
                tmp->color = M_SARB_RED;
                n = n->parent;
                continue;
            } else if (tmp->right->color == M_SARB_BLACK) {
                tmp->left->color = M_SARB_BLACK;
                tmp->color = M_SARB_RED;
                right_rotate(t, tmp);
                tmp = n->parent->right;
            }
            tmp->color = n->parent->color;
            n->parent->color = M_SARB_BLACK;
            tmp->right->color = M_SARB_BLACK;
            left_rotate(t, n->parent);
            n = t->root;
        } else {
            tmp = n->parent->left;
            if (tmp->color == M_SARB_RED) {
                tmp->color = M_SARB_BLACK;
                n->parent->color = M_SARB_RED;
                right_rotate(t, n->parent);
                tmp = n->parent->left;
            }
            if ((tmp->right->color == M_SARB_BLACK) && (tmp->left->color == M_SARB_BLACK)) {
                tmp->color = M_SARB_RED;
                n = n->parent;
                continue;
            } else if (tmp->left->color == M_SARB_BLACK) {
                tmp->right->color = M_SARB_BLACK;
                tmp->color = M_SARB_RED;
                left_rotate(t, tmp);
                tmp = n->parent->left;
            }
            tmp->color = n->parent->color;
            n->parent->color = M_SARB_BLACK;
            tmp->left->color = M_SARB_BLACK;
            right_rotate(t, n->parent);
            n = t->root;
        }
    }
    n->color = M_SARB_BLACK;
}
Пример #18
0
void fix_rb_tree(struct node **root, struct node *z)
{
	struct node *y;
	while(z->parent->color == RED)
	{
		if(z->parent == z->parent->parent->left)
		{
			y = z->parent->parent->right;
			if(y != NIL && y->color == RED)
			{
				//Case 1.1
				z->parent->color = y->color = BLACK;
				z->parent->parent->color = RED;
				z = z->parent->parent;
			}
			else //Segmentation fault should come
			{
				if(z == z->parent->right)
				{
					//Case 1.2
					z = z->parent;
					left_rotate(root, z);
				}
				//Case 1.3
				z->parent->color = BLACK;
				z->parent->parent->color = RED;
				right_rotate(root, z->parent->parent);
			}
		}
		else
		{
			y = z->parent->parent->left;
			if(y != NIL && y->color == RED)
			{
				//Case 2.1
				z->parent->color = y->color = BLACK;
				z->parent->parent->color = RED;
				z = z->parent->parent;
			}
			else
			{
				if(z == z->parent->left)
				{
					//Case 2.2
					z = z->parent;
					right_rotate(root, z);
				}
				z->parent->color = BLACK;
				z->parent->parent->color = RED;
				left_rotate(root, z->parent->parent);
			}
		}
	}
	(*root)->color = BLACK;
}
Пример #19
0
void RB::rb_balance(rb_node *x)
{
	x->color = rb_red;
	while (x != root && x->parent->color == rb_red)
	{
		rb_node *y = 0;
		if (x->parent == x->parent->parent->left)
		{
			y = x->parent->parent->right;
			if (y && y->color == rb_red)
			{
				y->color = rb_black;
				x->parent->parent->color = rb_red;
				x->parent->color = rb_black;
				x = x->parent->parent;
			}
			else
			{
				if (x == x->parent->right)
				{
					x = x->parent;
					left_rotate(x);
				}
				x->parent->color = rb_black;
				x->parent->parent->color = rb_red;
				right_rotate(x->parent->parent);
			}
		}
		else
		{
			y = x->parent->parent->left;
			if (y && y->color == rb_red)
			{
				y->color = rb_black;
				x->parent->color = rb_black;
				x->parent->parent->color = rb_red;
				x = x->parent->parent;
			}
			else
			{
				if (x == x->parent->left)
				{
					x = x->parent;
					right_rotate(x);
				}
				x->parent->color = rb_black;
				x->parent->parent->color = rb_red;
				left_rotate(x->parent->parent);
			}
		}
	}
	root->color = rb_black;
}
Пример #20
0
void RBTree::rb_insert_fixup(Node *nd)
{
	Node *y;
	while(nd->parent->color == RED)
	{
		if(nd->parent == nd->parent->parent->left)
		{
			y = nd->parent->parent->right;
			if(y->color == RED)//右叔叔是RED
			{
				nd->parent->color = BLACK;
				y->color = BLACK;
				nd->parent->parent->color = RED;
				nd = nd->parent->parent;
			}
			else //右叔叔是BLANK
			{
				if(nd == nd->parent->right)
				{
					nd = nd->parent;
					left_rotate(nd);
				}
				nd->parent->color = BLACK;
				nd->parent->parent->color = RED;
				right_rotate(nd->parent->parent);
			}
		}
		else
		{
			y = nd->parent->parent->left;
			if(y->color == RED)//左叔叔是RED
			{
				y->color = BLACK;
				nd->parent->color = BLACK;
				nd->parent->parent->color = RED;
				nd = nd->parent->parent;
			}
			else			  //左叔叔是BLANK
			{
				if(nd == nd->parent->left)
				{
					nd = nd->parent;
					right_rotate(nd);
				}

				nd->parent->color = BLACK;
				nd->parent->parent->color = RED;
				left_rotate(nd->parent->parent);
			}
		}
	}
	root->color = BLACK;
}
Пример #21
0
void rb_insert_fixup(Node* &T, Node *n)
{
	// 如果n的父亲是红色,则需要调整
	// 如果n的父亲是黑色,则不需要调整,因为n本身是红色
	while(IS_RED(PARENT(n))) {
		// 根据n的叔叔节点的颜色,来决定调整方案
		Node *p = IS_LEFT(PARENT(n))? RIGHT(PARENT(PARENT(n))): LEFT(PARENT(PARENT(n)));

		// 如果叔叔是红色,那很简单,把爷爷的黑色转移给父亲和叔叔,爷爷刷成红色
		// 这样,即满足了性质4,也没有破坏性质5及其他性质
		// 但是,爷爷可能破坏了红黑性质4,则从爷爷开始继续调整(向上递归了两层)
		if (IS_RED(p)) {
			// 父亲刷成黑色
			SET_BLACK(PARENT(n));
			// 叔叔刷成黑色
			SET_BLACK(p);
			// 爷爷刷成红色
			SET_RED(PARENT(PARENT(n)));

			// 从爷爷开始继续调整
			n = PARENT(PARENT(n));
			continue;
		} 

		// 如果叔叔是黑色,就复杂一点,引入旋转操作
		// 如果n是左孩子,那么需要一次右旋+颜色调整即可
		// 如果n是右孩子,则通过一次左旋调整成左孩子,然后按上面情况处理
		if (IS_LEFT(PARENT(n))) { 
			// 如果n是右孩子,通过右旋调整成左孩子
			if (IS_RIGHT(n)) {
				n = PARENT(n);
				left_rotate(T, n);
			}

			// 现在n是左孩子了
			SET_BLACK(PARENT(n));
			SET_RED(PARENT(PARENT(n)));
			right_rotate(T, PARENT(PARENT(n)));
		} else {
			if (IS_LEFT(n)) {
				n = PARENT(n);
				right_rotate(T,n);
			}
			SET_BLACK(PARENT(n));
			SET_RED(PARENT(PARENT(n)));
			left_rotate(T,PARENT(PARENT(n)));
		}
	}
	
	// 如果n是根节点,则把根设置为黑色
	if (NIL == PARENT(n))
		SET_BLACK(n);
}
Пример #22
0
Node* avl_insert(int k, Node* tree){
	/* Lisää uuden solmun puuhun. */
	if (tree == NULL){
		tree = memory_allocate(tree);
		tree->key = k;
		tree->ptrLeft = NULL;
		tree->ptrRight = NULL;
	}
	/* Vasempaan haaraan lisääminen. */
	else if (tree->key > k){
		if (tree->ptrLeft == NULL)
			printf("Arvo %d asetetaan solmun %d vasemmanpuoleiseksi lapseksi.\n", k, tree->key);
		tree->ptrLeft = avl_insert(k, tree->ptrLeft);
		/* tasapainottaminen: */
		if(get_height(tree->ptrLeft) == get_height(tree->ptrRight)+ 2){
			if(k < tree->ptrLeft->key){
				tree = right_rotate(tree);
				printf("R-rotaatio\n");
			}
			else{
				tree->ptrLeft = left_rotate(tree->ptrLeft);
				tree = right_rotate(tree);
				printf("LR-rotaatio\n");
			}
		}
	}
	/* Oikeaan haaraan lisääminen */
	else if (tree->key < k){
		if (tree->ptrRight == NULL)
			printf("Arvo %d asetetaan solmun %d oikeanpuoleiseksi lapseksi.\n", k, tree->key);
		tree->ptrRight = avl_insert(k, tree->ptrRight);
		/* tasapainottaminen: */
		if (get_height(tree->ptrRight) == get_height(tree->ptrLeft) + 2){
			/* R-rotaatio */
			if (k > tree->ptrRight->key){
				tree = left_rotate(tree);
				printf("L-rotaatio\n");
			}
			else{
				/* RL-rotaatio */
				tree->ptrRight =  right_rotate(tree->ptrRight);
				tree = left_rotate(tree);
				printf("RL-rotaatio\n");
			}
		}
	}
	else{
		printf("Arvo on jo aikaisemmin lisätty puuhun!\n");
	}
	/* Korjataan solmum korkeus. */
	tree->height = max(get_height(tree->ptrLeft), get_height(tree->ptrRight)) + 1;
	return (tree);
}
Пример #23
0
void rb_insert_fix(node* x)
{
	node_list y;
	while(x->p==red)
	{
		if(((x->p)->p)->left==(x->p))
		{
			y=((x->p)->p)->right;
			if(y->color==RED)
			{
				y->color=BLACK;
				(x->p)->color=BLACK;
				((x->p)->p)->color=RED;
				x=(x->p)->p;
			}
			else if(x==(x->p)->right)
			{
				x=x.p;
				left_rotate(x);
			}
			else
			{
				(x->p)->color=BALCKk;
				((x->p)->p)->color=RED;
				right_rotate(((x->p)->p));
			}
		}
		else
		{
			y=((x->p)->p)->left;
			if(y->color==RED)
			{
				y->color=BLACK;
				(y->p)->color=RED;
				(x->p)->color=BLACK;
				x=(x->p)->P;
			}
			else if(x==(x->p)->left)
			{
				x=x->p;
				right_rotate(x);
			}
			else
			{
				(x->p)->color=BLACK;
				((x->p)->p)->color=RED;
				left_rotate(((x->p)->p));
			}
		}
	}
}
Пример #24
0
inline void sha1::process_block()
{
    unsigned int w[80];
    for (std::size_t i=0; i<16; ++i) {
        w[i]  = (block_[i*4 + 0] << 24);
        w[i] |= (block_[i*4 + 1] << 16);
        w[i] |= (block_[i*4 + 2] << 8);
        w[i] |= (block_[i*4 + 3]);
    }
    for (std::size_t i=16; i<80; ++i) {
        w[i] = left_rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
    }

    unsigned int a = h_[0];
    unsigned int b = h_[1];
    unsigned int c = h_[2];
    unsigned int d = h_[3];
    unsigned int e = h_[4];

    for (std::size_t i=0; i<80; ++i) {
        unsigned int f;
        unsigned int k;

        if (i<20) {
            f = (b & c) | (~b & d);
            k = 0x5A827999;
        } else if (i<40) {
            f = b ^ c ^ d;
            k = 0x6ED9EBA1;
        } else if (i<60) {
            f = (b & c) | (b & d) | (c & d);
            k = 0x8F1BBCDC;
        } else {
            f = b ^ c ^ d;
            k = 0xCA62C1D6;
        }

        unsigned temp = left_rotate(a, 5) + f + e + k + w[i];
        e = d;
        d = c;
        c = left_rotate(b, 30);
        b = a;
        a = temp;
    }

    h_[0] += a;
    h_[1] += b;
    h_[2] += c;
    h_[3] += d;
    h_[4] += e;
}
Пример #25
0
static void insert_fixup( pj_rbtree *tree, pj_rbtree_node *node ) 
{
    pj_rbtree_node *temp, *parent;

    PJ_CHECK_STACK();

    while (node != tree->root && node->parent->color == PJ_RBCOLOR_RED) {
        parent = node->parent;
        if (parent == parent->parent->left) {
	    temp = parent->parent->right;
	    if (temp->color == PJ_RBCOLOR_RED) {
	        temp->color = PJ_RBCOLOR_BLACK;
	        node = parent;
	        node->color = PJ_RBCOLOR_BLACK;
	        node = node->parent;
	        node->color = PJ_RBCOLOR_RED;
	    } else {
	        if (node == parent->right) {
		   node = parent;
		   left_rotate(tree, node);
	        }
	        temp = node->parent;
	        temp->color = PJ_RBCOLOR_BLACK;
	        temp = temp->parent;
	        temp->color = PJ_RBCOLOR_RED;
	        right_rotate( tree, temp);
	    }
        } else {
	    temp = parent->parent->left;
	    if (temp->color == PJ_RBCOLOR_RED) {
	        temp->color = PJ_RBCOLOR_BLACK;
	        node = parent;
	        node->color = PJ_RBCOLOR_BLACK;
	        node = node->parent;
	        node->color = PJ_RBCOLOR_RED;
	    } else {
	        if (node == parent->left) {
		    node = parent;
		    right_rotate(tree, node);
	        }
	        temp = node->parent;
	        temp->color = PJ_RBCOLOR_BLACK;
	        temp = temp->parent;
	        temp->color = PJ_RBCOLOR_RED;
	        left_rotate(tree, temp);
	   }
        }
    }
	
    tree->root->color = PJ_RBCOLOR_BLACK;
}
Пример #26
0
static void
k_rbtree_insert_fixup(k_rbtree_t* t,k_rbnode_t* z)
{
        k_rbnode_t* y = K_NULL;
        while(z->parent->color == k_color_red)
        {
                if(z->parent == z->parent->parent->left)
                {
                        y = z->parent->parent->right;
                        if(y->color == k_color_red)//case 1
                        {
                                z->parent->color = k_color_black;
                                y->color = k_color_black;
                                z->parent->parent->color = k_color_red;
                                z = z->parent->parent;
                        }else{
                                if(z == z->parent->right){//case 2
                                        z = z->parent;
                                        left_rotate(t,z);
                                }
                                //case 2 will turn to case 3
                                z->parent->color = k_color_black;//case 3
                                z->parent->parent->color = k_color_red;
                                right_rotate(t,z->parent->parent);

                        }

                }else{
                        y = z->parent->parent->left;
                        if(y->color == k_color_red)//case 1
                        {
                                z->parent->color = k_color_black;
                                y->color = k_color_black;
                                z->parent->parent->color = k_color_red;
                                z = z->parent->parent;
                        }else{
                                if(z == z->parent->left){//case 2
                                        z = z->parent;
                                        right_rotate(t,z);
                                }

                                z->parent->color = k_color_black;//case 3
                                z->parent->parent->color = k_color_red;
                                left_rotate(t,z->parent->parent);
                        }

                }
        }
        t->root->color = k_color_black;
}
Пример #27
0
void interval_insert_fixup(INT_TREE tree, INT_NODE node)
{
    while(node -> parent != NULL && node -> parent -> color == RED)
    {
        if(node -> parent == node -> parent -> parent -> left)
        {
            INT_NODE node_uncle = node -> parent -> parent -> right; // uncle's node
            if(node_uncle != NULL && node_uncle -> color == RED)    // case 1
            {
                node -> parent -> color = BLACK;
                node_uncle -> color = BLACK;
                node -> parent -> parent -> color = RED;
                node = node -> parent -> parent;
            }else
            {
                if(node == node -> parent -> right) // case 2: uncle is black, parent's right child
                {
                    node = node -> parent;
                    left_rotate(tree, node);
                }  // turn to case 3
                node -> parent -> color = BLACK;
                node -> parent -> parent -> color = RED;
                right_rotate(tree, node -> parent -> parent);
            }
            
        }else
        {
            INT_NODE node_uncle = node -> parent -> parent -> left;
            if(node_uncle != NULL && node_uncle -> color == RED)
            {
                node -> parent -> color = BLACK;
                node_uncle -> color = BLACK;
                node -> parent -> parent -> color = RED;
                node = node -> parent -> parent;
            }else
            {
                if(node == node -> parent -> left)
                {
                    node = node -> parent;
                    right_rotate(tree, node);
                }
                node -> parent -> color = BLACK;
                node -> parent -> parent -> color = RED;
                left_rotate(tree, node -> parent -> parent);
            }
        }
    }
    tree -> root -> color = BLACK;
}
Пример #28
0
/* As we add a new node with color red,
 * if color of its parent is red too,
 * we need to resolve the additional red color.
 * We make it by considering the following 3 cases :
 *
 * 1. When z's uncle is red too,so both z's parent and z's uncle is red.
 * In this case, we simply color z's parent and z's uncle to black,
 * and color z's grand-parent from black to red.
 * Now there's no violation between z and its parent.
 * The node that may cause violation goes to z's grand-parent,
 * who is colored from black to red.So we start a new loop with z=p(p(z)). 
 *
 * 2. If z's uncle is black,we cannot simply do as the case above.
 * Considering our goal to resolve the additional red color,
 * we may make it by some rotation and the red color will finally go
 * to z's uncle's side.As z's uncle is black,it will be okay for its uncle.
 * Mention that we may need more than one rotation to make it.
 * Here we only consider the case of p(z) == l(p(p(z))),
 * which is symmetric with the case of p(z) == r(p(p(z))).
 * Now we only condiser the first case,and there are two subcases for each one:
 *
 *	2.1 When z is right child of its parent ( z == r(p(z)) ).
 *	We do the following :
 *		z = p(z);
 *		left-rotate(T,z);
 *	After then,z and p(z) will still be red,which is still violating the rule.
 *	But z now becomes left child of its parent,which is next case we will consider.
 *
 *	2.2 When z is left child of its parent ( z == l(p(z)) ).
 *	We do the following :
 *		c(p(z)) = RBT_BLACK;
 *		c(p(p(z))) = RBT_RED;
 *		right_rotate(T,p(p(z)));
 *	We paint p(z) to black and p(p(z)) to red,and do the right rotateo on p(p(z)),
 *	after then the additional red color goes to z's uncle's side and
 *	the black-height property will stay unchanged.
 *
 * PS:ROTATE WILL NOT CHANGE THE BLACK-HEIGHT PROPERTY.
 *
 * FINALLY,WE SHOULD SET THE ROOT TO BLACK TO MAKE SURE WHEN THE FIRST NODE IS INSERTED,
 * IT WILL BE TRE ROOT AND BE BLACK.
 *	*/
static void rbt_insert_fixup(redBlackTree_t *T,rb_node_t * z)
{
	rb_node_t * y;
	while(c(p(z)) == RBT_RED){
		/* p(z) not null and color is red,
		 * so p(z) is not root --> p(p(z)) exists
		 * and c(p(p(z))) is RBT_BLACK */
		if(p(z) == l(p(p(z)))){
			/* p(z) is left child of its parent */
			y = r(p(p(z)));
			/*------------- case 1 --------------*/
			if(c(y) == RBT_RED){
				/* p(z) 's parent must be black! */
				c(y) = RBT_BLACK;
				c(p(z)) = RBT_BLACK;
				c(p(p(z))) = RBT_RED;
				z = p(p(z));
			/*------------- case 2 --------------*/
			}else if(z == r(p(z))){
				/* y == NIL_NODE or c(y) is black */
				z = p(z);
				left_rotate(T,z);
			/*------------- case 3 --------------*/
			}else{
				/* z is left child of its parent */
				c(p(z)) = RBT_BLACK;
				c(p(p(z))) = RBT_RED;
				right_rotate(T,p(p(z)));
			}
		}else{
			/* p(z) is right child of its parent */
			y = l(p(p(z)));
			if(c(y) == RBT_RED){
				c(y) = RBT_BLACK;
				c(p(z)) = RBT_BLACK;
				c(p(p(z))) = RBT_RED;
				z = p(p(z));
			}else if(z == l(p(z))){
				z = p(z);
				right_rotate(T,z);
			}else{
				c(p(z)) = RBT_BLACK;
				c(p(p(z))) = RBT_RED;
				left_rotate(T,p(p(z)));
			}
		}
	}
	c(T->root) = RBT_BLACK;
}
Пример #29
0
static void insert_fixup(struct rbtree * tree, struct rbtree_node * z)
{
	assert(z->parent != NULL);

	while(z->parent->color == RBTREE_RED)
	{
		if(z->parent == z->parent->parent->left)
		{
			struct rbtree_node * y = z->parent->parent->right;
			if(y->color == RBTREE_RED)
			{
				z->parent->color = RBTREE_BLACK;
				y->color = RBTREE_BLACK;
				z->parent->parent->color = RBTREE_RED;
				z = z->parent->parent;
			} else {
				if(z == z->parent->right)
				{
					z = z->parent;
					left_rotate(tree, z);
				}
				z->parent->color = RBTREE_BLACK;
				z->parent->parent->color = RBTREE_RED;
				right_rotate(tree, z->parent->parent);
			}
		} else {
			struct rbtree_node * y = z->parent->parent->left;
			if(y->color == RBTREE_RED)
			{
				z->parent->color = RBTREE_BLACK;
				y->color = RBTREE_BLACK;
				z->parent->parent->color = RBTREE_RED;
				z = z->parent->parent;
			} else {
				if(z == z->parent->left)
				{
					z = z->parent;
					right_rotate(tree, z);
				}
				z->parent->color = RBTREE_BLACK;
				z->parent->parent->color = RBTREE_RED;
				left_rotate(tree, z->parent->parent);
			}

		}
	}

	tree->root->color = RBTREE_BLACK;
}
Пример #30
0
struct node * balance(struct node *tree)
{
  if (height(tree->left) - height(tree->right) > 1) {
    if (height(tree->left->left) < height(tree->left->right))
      tree->left = left_rotate(tree->left);
    tree = right_rotate(tree);
  }
  else if (height(tree->left) - height(tree->right) < -1) {
    if (height(tree->right->left) > height(tree->right->right))
      tree->right = right_rotate(tree->right);
    tree = left_rotate(tree);
  }

  return tree;
}