当前位置: 首页>>代码示例>>C++>>正文


C++ right_rotate函数代码示例

本文整理汇总了C++中right_rotate函数的典型用法代码示例。如果您正苦于以下问题:C++ right_rotate函数的具体用法?C++ right_rotate怎么用?C++ right_rotate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了right_rotate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: height

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);
}
}
开发者ID:JasonGross,项目名称:c-semantics,代码行数:35,代码来源:avl_tree.cil.c

示例2: maintain

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;
}
开发者ID:comzyh,项目名称:ICPCcode,代码行数:30,代码来源:1442.cpp

示例3: btree_delete_fixup

/* 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;
}
开发者ID:Greatrandom,项目名称:ompi,代码行数:61,代码来源:opal_rb_tree.c

示例4: while

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;
}
开发者ID:venux021,项目名称:mustard,代码行数:60,代码来源:rbtree.sample.cpp

示例5: while

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);
}
开发者ID:kjmikkel,项目名称:rb_tree,代码行数:60,代码来源:rb_tree.cpp

示例6: rb_delete_fixup

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;
}
开发者ID:Limaa,项目名称:libds,代码行数:60,代码来源:tree.c

示例7: rebalance_AVL

/* 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;
}
开发者ID:daniel-k-richardson,项目名称:Assignments-for-KIT205,代码行数:38,代码来源:avl.c

示例8: tree_fix

/**
 * 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;
}
开发者ID:lchish,项目名称:cosc,代码行数:65,代码来源:tree.c

示例9: rb_delete_fixup

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;
}
开发者ID:codeplayer2org,项目名称:CLRS-code,代码行数:58,代码来源:rb_tree.c

示例10: k_rbtree_delete_fixup

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;
}
开发者ID:huangchonghai,项目名称:httpserver-4,代码行数:57,代码来源:k_rbtree.c

示例11: while

 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;
 }
开发者ID:powernano,项目名称:Alloc,代码行数:57,代码来源:_redblacktree.cpp

示例12: rb_delete_fixup

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;
			}
		}
	}
}
开发者ID:LinuxKernelDevelopment,项目名称:ITA,代码行数:57,代码来源:rbtree.c

示例13: rbt_fix

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;
}
开发者ID:lchish,项目名称:cosc,代码行数:57,代码来源:rbt.c

示例14: delete_fixup

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;
}
开发者ID:RocFang,项目名称:dnspod-sr,代码行数:56,代码来源:storage.c

示例15: while

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);
                }

            }
        }
    }
}
开发者ID:HappyShadowWalker,项目名称:HihoCode,代码行数:56,代码来源:splay_tree.cpp


注:本文中的right_rotate函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。