• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ LeftRotate函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: IntervalTreeNode

IntervalTreeNode * IntervalTree::Insert(Interval * newInterval)
{
  IntervalTreeNode * y;
  IntervalTreeNode * x;
  IntervalTreeNode * newNode;

  x = new IntervalTreeNode(newInterval);
  TreeInsertHelp(x);
  FixUpMaxHigh(x->parent);
  newNode = x;
  x->red=1;
  while(x->parent->red) { /* use sentinel instead of checking for root */
    if (x->parent == x->parent->parent->left) {
      y=x->parent->parent->right;
      if (y->red) {
	x->parent->red=0;
	y->red=0;
	x->parent->parent->red=1;
	x=x->parent->parent;
      } else {
	if (x == x->parent->right) {
	  x=x->parent;
	  LeftRotate(x);
	}
	x->parent->red=0;
	x->parent->parent->red=1;
	RightRotate(x->parent->parent);
      }
    } else { /* case for x->parent == x->parent->parent->right */
             /* this part is just like the section above with */
             /* left and right interchanged */
      y=x->parent->parent->left;
      if (y->red) {
	x->parent->red=0;
	y->red=0;
	x->parent->parent->red=1;
	x=x->parent->parent;
      } else {
	if (x == x->parent->left) {
	  x=x->parent;
	  RightRotate(x);
	}
	x->parent->red=0;
	x->parent->parent->red=1;
	LeftRotate(x->parent->parent);
      }
    }
  }
  root->left->red=0;
  return(newNode);

#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
  CheckAssumptions();
#elif defined(DEBUG_ASSERT)
  Assert(!nil->red,"nil not red in ITTreeInsert");
  Assert(!root->red,"root not red in ITTreeInsert");
  Assert((nil->maxHigh=MIN_INT),
	 "nil->maxHigh != MIN_INT in ITTreeInsert");
#endif
}
开发者ID:gpertea,项目名称:gsrc,代码行数:60,代码来源:GIntervalTree.cpp


示例2: InsertImpl

typename Map<K,V,KeyElementTraits, ValueElementTraits, ThreadingModel>::Node* Map<K,V,KeyElementTraits, ValueElementTraits, ThreadingModel>::Insert( CONST K& Key, CONST V& Value )
{
	Node* pNewNode = InsertImpl( Key, Value );

	Node* pX = pNewNode;
	pX->m_Status = Node::Red;
	Node* pY = 0;
	while (pX != m_pRoot && pX->m_pParent->m_Status == Node::Red)
	{
		if (pX->m_pParent == pX->m_pParent->m_pParent->m_pLeft)
		{
			pY = pX->m_pParent->m_pParent->m_pRight;
			if (pY != NULL && pY->m_Status == Node::Black)
			{
				pX->m_pParent->m_Status = Node::Black;
				pY->m_Status = Node::Black;
				pX->m_pParent->m_pParent->m_Status = Node::Red;
				pX = pX->m_pParent->m_pParent;
			}
			else
			{
				if (pX == pX->m_pParent->m_pRight)
				{
					pX = pX->m_pParent;
					LeftRotate(pX);
				}
				pX->m_pParent->m_Status = Node::Black;
				pX->m_pParent->m_pParent->m_Status = Node::Red;
				RightRotate(pX->m_pParent->m_pParent);
			}
		}
		else
		{
			pY = pX->m_pParent->m_pParent->m_pLeft;
			if (pY != NULL && pY->m_Status == Node::Red)
			{
				pX->m_pParent->m_Status = Node::Black;
				pY->m_Status = Node::Black;
				pX->m_pParent->m_pParent->m_Status = Node::Red;
				pX = pX->m_pParent->m_pParent;
			}
			else
			{
				if (pX == pX->m_pParent->m_pLeft)
				{
					pX = pX->m_pParent;
					RightRotate(pX);
				}
				pX->m_pParent->m_Status = Node::Black;
				pX->m_pParent->m_pParent->m_Status = Node::Red;
				LeftRotate(pX->m_pParent->m_pParent);
			}
		}
	}

	m_pRoot->m_Status = Node::Black;
	SetNil(&m_pRoot->m_pParent);

	return pNewNode;
}
开发者ID:anareboucas,项目名称:nanook,代码行数:60,代码来源:MapImpl.hpp


示例3: InsertImpl

typename Set<K,ElementTraits>::Node* Set<K,ElementTraits>::Insert( CONST K& Key )
{
	Node* pNewNode = InsertImpl( Key );

	Node* pX = pNewNode;
	pX->m_Status = Node::Red;
	Node* pY = 0;
	while (pX != m_pRoot && pX->m_pParent->m_Status == Node::Red)
	{
		if (pX->m_pParent == pX->m_pParent->m_pParent->m_pLeft)
		{
			pY = pX->m_pParent->m_pParent->m_pRight;
			if (pY != NULL && pY->m_Status == Node::Black)
			{
				pX->m_pParent->m_Status = Node::Black;
				pY->m_Status = Node::Black;
				pX->m_pParent->m_pParent->m_Status = Node::Red;
				pX = pX->m_pParent->m_pParent;
			}
			else
			{
				if (pX == pX->m_pParent->m_pRight)
				{
					pX = pX->m_pParent;
					LeftRotate(pX);
				}
				pX->m_pParent->m_Status = Node::Black;
				pX->m_pParent->m_pParent->m_Status = Node::Red;
				RightRotate(pX->m_pParent->m_pParent);
			}
		}
		else
		{
			pY = pX->m_pParent->m_pParent->m_pLeft;
			if (pY != NULL && pY->m_Status == Node::Red)
			{
				pX->m_pParent->m_Status = Node::Black;
				pY->m_Status = Node::Black;
				pX->m_pParent->m_pParent->m_Status = Node::Red;
				pX = pX->m_pParent->m_pParent;
			}
			else
			{
				if (pX == pX->m_pParent->m_pLeft)
				{
					pX = pX->m_pParent;
					RightRotate(pX);
				}
				pX->m_pParent->m_Status = Node::Black;
				pX->m_pParent->m_pParent->m_Status = Node::Red;
				LeftRotate(pX->m_pParent->m_pParent);
			}
		}
	}

	m_pRoot->m_Status = Node::Black;
	SetNil(&m_pRoot->m_pParent);

	return pNewNode;
}
开发者ID:anareboucas,项目名称:nanook,代码行数:60,代码来源:SetImpl.hpp


示例4: IT_insert

IntervalTreeNode *
IT_insert(IntervalTree *it, long low, long high, void *data)
{
    IntervalTreeNode *x, *y, *newNode;

    x = ITN_create(low, high, data);
    TreeInsertHelp(it, x);
    FixUpMaxHigh(it, x->parent);
    newNode = x;
    x->red=1;
    while(x->parent->red) { /* use sentinel instead of checking for root */
        if (x->parent == x->parent->parent->left) {
            y=x->parent->parent->right;
            if (y->red) {
                x->parent->red=0;
                y->red=0;
                x->parent->parent->red=1;
                x=x->parent->parent;
            } else {
                if (x == x->parent->right) {
                    x=x->parent;
                    LeftRotate(it, x);
                }
                x->parent->red=0;
                x->parent->parent->red=1;
                RightRotate(it, x->parent->parent);
            } 
        } else { /* case for x->parent == x->parent->parent->right */
             /* this part is just like the section above with */
             /* left and right interchanged */
            y=x->parent->parent->left;
            if (y->red) {
                x->parent->red=0;
                y->red=0;
                x->parent->parent->red=1;
                x=x->parent->parent;
            } else {
                if (x == x->parent->left) {
                    x=x->parent;
                    RightRotate(it, x);
                }
                x->parent->red=0;
                x->parent->parent->red=1;
                LeftRotate(it, x->parent->parent);
            } 
        }
    }
    it->root->left->red=0;

#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
    IT_CheckAssumptions(it);
#elif defined(DEBUG_ASSERT)
    Assert(!it->nil->red,"nil not red in ITTreeInsert");
    Assert(!it->root->red,"root not red in ITTreeInsert");
    Assert((it->nil->maxHigh=LONG_MIN),
           "nil->maxHigh != LONG_MIN in ITTreeInsert");
#endif
    return newNode;
}
开发者ID:Acidburn0zzz,项目名称:yasm,代码行数:59,代码来源:inttree.c


示例5: RbTreeInsert

RBNODE
RbTreeInsert (RBTREE tree, RBKEY key, RBVALUE info)
{
  RBNODE y;
  RBNODE x;
  RBNODE newNode;

  ++tree->count;
  x=(RBNODE) SafeMalloc(sizeof(*x));
  x->key=key;
  x->info=info;

  TreeInsertHelp(tree,x);
  newNode=x;
  x->red=1;
  while(x->parent->red) { /* use sentinel instead of checking for root */
    if (x->parent == x->parent->parent->left) {
      y=x->parent->parent->right;
      if (y->red) {
	x->parent->red=0;
	y->red=0;
	x->parent->parent->red=1;
	x=x->parent->parent;
      } else {
	if (x == x->parent->right) {
	  x=x->parent;
	  LeftRotate(tree,x);
	}
	x->parent->red=0;
	x->parent->parent->red=1;
	RightRotate(tree,x->parent->parent);
      } 
    } else { /* case for x->parent == x->parent->parent->right */
      y=x->parent->parent->left;
      if (y->red) {
	x->parent->red=0;
	y->red=0;
	x->parent->parent->red=1;
	x=x->parent->parent;
      } else {
	if (x == x->parent->left) {
	  x=x->parent;
	  RightRotate(tree,x);
	}
	x->parent->red=0;
	x->parent->parent->red=1;
	LeftRotate(tree,x->parent->parent);
      } 
    }
  }
  tree->root->left->red=0;
  return(newNode);

#ifdef DEBUG_ASSERT
  Assert(!tree->nil->red,"nil not red in RbTreeInsert");
  Assert(!tree->root->red,"root not red in RbTreeInsert");
#endif
}
开发者ID:MarcNo,项目名称:lifelines,代码行数:58,代码来源:rbtree.c


示例6: RBTreeInsert

rb_red_blk_node * RBTreeInsert(rb_red_blk_tree* tree, void* key, void* info) {
  rb_red_blk_node * y;
  rb_red_blk_node * x;
  rb_red_blk_node * newNode;

  if (setjmp(rb_jbuf))
    return NULL;
  x=(rb_red_blk_node*) SafeMalloc(sizeof(rb_red_blk_node));
  x->key=key;
  x->info=info;

  TreeInsertHelp(tree,x);
  newNode=x;
  x->red=1;
  while(x->parent->red) { /* use sentinel instead of checking for root */
    if (x->parent == x->parent->parent->left) {
      y=x->parent->parent->right;
      if (y->red) {
	x->parent->red=0;
	y->red=0;
	x->parent->parent->red=1;
	x=x->parent->parent;
      } else {
	if (x == x->parent->right) {
	  x=x->parent;
	  LeftRotate(tree,x);
	}
	x->parent->red=0;
	x->parent->parent->red=1;
	RightRotate(tree,x->parent->parent);
      } 
    } else { /* case for x->parent == x->parent->parent->right */
      y=x->parent->parent->left;
      if (y->red) {
	x->parent->red=0;
	y->red=0;
	x->parent->parent->red=1;
	x=x->parent->parent;
      } else {
	if (x == x->parent->left) {
	  x=x->parent;
	  RightRotate(tree,x);
	}
	x->parent->red=0;
	x->parent->parent->red=1;
	LeftRotate(tree,x->parent->parent);
      } 
    }
  }
  tree->root->left->red=0;
  return(newNode);

#ifdef DEBUG_ASSERT
  Assert(!tree->nil->red,"nil not red in RBTreeInsert");
  Assert(!tree->root->red,"root not red in RBTreeInsert");
#endif
}
开发者ID:TidyHuang,项目名称:vizgems,代码行数:57,代码来源:red_black_tree.c


示例7: RBDeleteFixUp

void RBDeleteFixUp(rb_red_blk_node* x) {
  rb_red_blk_node* root=tree->root->left;
  rb_red_blk_node* w;

  while( (!x->red) && (root != x)) {
    if (x == x->parent->left) {
      w=x->parent->right;
      if (w->red) {
	w->red=0;
	x->parent->red=1;
	LeftRotate(x->parent);
	w=x->parent->right;
      }
/* XXX: original code was :       if ( (!w->right->red) && (!w->left->red) ) {  */
      if ( false &&  (!w->right->red) && (!w->left->red) ) { 
	w->red=1;
	x=x->parent;
      } else {
	if (!w->right->red) {
	  w->left->red=0;
	  w->red=1;
	  RightRotate(w);
	  w=x->parent->right;
	}
	w->red=x->parent->red;
	x->parent->red=0;
	w->right->red=0;
	LeftRotate(x->parent);
	x=root;       }
    } else {       w=x->parent->left;
      if (w->red) {
	w->red=0;
	x->parent->red=1;
	RightRotate(x->parent);
	w=x->parent->left;
      }
      if ( (!w->right->red) && (!w->left->red) ) { 
	w->red=1;
	x=x->parent;
      } else {
	if (!w->left->red) {
	  w->right->red=0;
	  w->red=1;
	  LeftRotate(w);
	  w=x->parent->left;
	}
	w->red=x->parent->red;
	x->parent->red=0;
	w->left->red=0;
	RightRotate(x->parent);
	x=root;       }
    }
  }
  x->red=0;
}
开发者ID:jonahkall,项目名称:blt,代码行数:55,代码来源:rbtree592.cpp


示例8: while

void RbTree<Type>::DeleteFixup(TreeNodePointer nodepointer) {
    while (nodepointer!=&m_nil && nodepointer->m_color==BLACK) {
        if (nodepointer = nodepointer->m_parent->m_left) {
            TreeNodePointer brothernode = nodepointer->m_parent->m_right;
            if (brothernode->m_color == RED) {
                brothernode->m_color = BLACK;
                nodepointer->m_parent->m_color = RED;
                LeftRotate(nodepointer->m_parent);
                brothernode = nodepointer->m_parent->m_right;
            }
            if (brothernode->m_left->m_color == BLACK && brothernode->m_right->m_color == BLACK) {
                brothernode->m_color = RED;
                nodepointer = nodepointer->m_parent;
            } else { 
                if (brothernode->m_right->m_color == BLACK) {
                    brothernode->m_left->m_color = BLACK;
                    brothernode->m_color = RED;
                    RightRotate(brothernode);
                    brothernode = nodepointer->m_parent->m_right;
                }
                brothernode->m_color = nodepointer->m_parent->m_color;
                nodepointer->m_parent->m_color = BLACK;
                brothernode->m_right->m_color = BLACK;
                LeftRotate(nodepointer->m_parent);
                nodepointer = m_root;
            }
        }else {
            TreeNodePointer brothernode = nodepointer->m_left;
            if (brothernode->m_color == RED) {
                brothernode->m_color = BLACK;
                nodepointer->m_parent->m_color = RED;
                RightRotate(nodepointer->m_parent);
                brothernode = nodepointer->m_parent->m_left;
            }
            if (brothernode->m_left->m_color == BLACK && brothernode->m_right->m_color == BLACK) {
                brothernode->m_color = RED;
                nodepointer = nodepointer->m_parent;
            } else {
                if (brothernode->m_left->m_color == BLACK) {
                    brothernode->m_right->m_color = BLACK;
                    brothernode->m_color = RED;
                    LeftRotate(brothernode);
                    brothernode = nodepointer->m_parent->m_left;
                }
                brothernode->m_color = nodepointer->m_parent->m_color;
                nodepointer->m_parent->m_color = BLACK;
                brothernode->m_left->m_color = BLACK;
                RightRotate(nodepointer->m_parent);
                nodepointer = m_root;
            }
        }
    }
    nodepointer->m_color = BLACK;    
}
开发者ID:magicdog,项目名称:algorithm,代码行数:54,代码来源:rbtree.cpp


示例9: while

void RBTree::InsertFixup(rb_node_t *z)
{
    rb_node_t *y;

    while(p_of(z)->color == RED) {
        if (p_of(z) == pp_of(z)->left) {
            y = pp_of(z)->right;        // uncle
            if (y->color == RED) {
                // case 1: uncle is red
                p_of(z)->color = BLACK;
                y->color = BLACK;
                pp_of(z)->color = RED;
                z = pp_of(z);
            } else {
                if (z == p_of(z)->right) {
                    // case2: uncle is black and z is right child
                    z = p_of(z);
                    LeftRotate(z);
                }
                // case3: uncle is black and z is left child
                p_of(z)->color = BLACK;
                pp_of(z)->color = RED;
                RightRotate(pp_of(z));
            }

        } else if (p_of(z) == pp_of(z)->right) {
            y = pp_of(z)->left;
            if (y->color == RED) {
                p_of(z)->color = BLACK;
                y->color = BLACK;
                pp_of(z)->color = RED;
                z = pp_of(z);
            } else {
                if (z == p_of(z)->left) {
                    z = p_of(z);
                    RightRotate(z);
                }

                p_of(z)->color = BLACK;
                pp_of(z)->color = RED;
                LeftRotate(pp_of(z));
            }

        }
    }
    this->root->color = BLACK;
}
开发者ID:gccli,项目名称:mylibrary,代码行数:47,代码来源:rbtree.cpp


示例10: TreeInsertHelp

void RBTree::RBTreeInsert(int key) {
  rb_red_blk_node * y;
  rb_red_blk_node * x;
  rb_red_blk_node * newNode;

  x=new rb_red_blk_node;
  x->key=key;

  TreeInsertHelp(x);
  newNode=x;
  x->red=1;
  while(x->parent->red) {     if (x->parent == x->parent->parent->left) {
      y=x->parent->parent->right;
      if (y->red) {
	x->parent->red=0;
	y->red=0;
	x->parent->parent->red=1;
	x=x->parent->parent;
      } else {
/* XXX: original code was : 	if (x == x->parent->right) { */
	if (x  >  x->parent->right) {
	  x=x->parent;
	  LeftRotate(x);
	}
	x->parent->red=0;
	x->parent->parent->red=1;
	RightRotate(x->parent->parent);
      } 
    } else {       y=x->parent->parent->left;
      if (y->red) {
	x->parent->red=0;
	y->red=0;
	x->parent->parent->red=1;
	x=x->parent->parent;
      } else {
	if (x == x->parent->left) {
	  x=x->parent;
	  RightRotate(x);
	}
	x->parent->red=0;
	x->parent->parent->red=1;
	LeftRotate(x->parent->parent);
      } 
    }
  }
  tree->root->left->red=0;
}
开发者ID:jonahkall,项目名称:blt,代码行数:47,代码来源:rbtree337.cpp


示例11: insertFixup

void insertFixup(BRTree * T, BRNode * z) {
    if (z->p == NULL) {
        z->color = BLACK;
        return;
    }
    if (z->p->p == NULL) {
        return;
    }
    BRNode * y;
    while(z->p != NULL && z->p->color == RED) {
        if(z->p == z->p->p->left) {
            y = z->p->p->right;
            if(y != NULL && y->color == RED) {
                z->p->color = BLACK;
                y->color = BLACK;
                z->p->p->color = RED;
                z = z->p->p;
            } else {
                if(z == z->p->right) {
                    z = z->p;
                    LeftRotate(T, z);
                }
                z->p->color = BLACK;
                z->p->p->color = RED;
                RightRotate(T, z->p->p);
            }
        } else {
            y = z->p->p->left;
            if(y != NULL && y->color == RED) {
                z->p->color = BLACK;
                y->color = BLACK;
                z->p->p->color = RED;
                z = z->p->p;
            } else {
                if(z == z->p->left) {
                    z = z->p;
                    RightRotate(T,z);
                }
                z->p->color = BLACK;
                z->p->p->color = RED;
                LeftRotate(T, z->p->p);
            }
        }
    }
    T->root->color = BLACK;
}
开发者ID:komea,项目名称:leetcode,代码行数:46,代码来源:220-Contains+Duplicate+III.c


示例12: Camellia_DecryptBlock_Rounds

void Camellia_DecryptBlock_Rounds(int grandRounds, const u8 ciphertext[],
                                  const KEY_TABLE_TYPE keyTable,
                                  u8 plaintext[])
{
    u32 s0, s1, s2, s3;
    const u32 *k = keyTable + grandRounds * 16, *kend = keyTable + 4;

    s0 = GETU32(ciphertext) ^ k[0];
    s1 = GETU32(ciphertext + 4) ^ k[1];
    s2 = GETU32(ciphertext + 8) ^ k[2];
    s3 = GETU32(ciphertext + 12) ^ k[3];

    while (1) {
        /* Camellia makes 6 Feistel rounds */
        k -= 12;
        Camellia_Feistel(s0, s1, s2, s3, k + 10);
        Camellia_Feistel(s2, s3, s0, s1, k + 8);
        Camellia_Feistel(s0, s1, s2, s3, k + 6);
        Camellia_Feistel(s2, s3, s0, s1, k + 4);
        Camellia_Feistel(s0, s1, s2, s3, k + 2);
        Camellia_Feistel(s2, s3, s0, s1, k + 0);

        if (k == kend)
            break;

        /*
         * This is the same function as the diffusion function D of the
         * accompanying documentation. See section 3.2 for properties of the
         * FLlayer function.
         */
        k -= 4;
        s1 ^= LeftRotate(s0 & k[2], 1);
        s2 ^= s3 | k[1];
        s0 ^= s1 | k[3];
        s3 ^= LeftRotate(s2 & k[0], 1);
    }

    k -= 4;
    s2 ^= k[0], s3 ^= k[1], s0 ^= k[2], s1 ^= k[3];

    PUTU32(plaintext, s2);
    PUTU32(plaintext + 4, s3);
    PUTU32(plaintext + 8, s0);
    PUTU32(plaintext + 12, s1);
}
开发者ID:InfoHunter,项目名称:openssl,代码行数:45,代码来源:camellia.c


示例13: Zig

	void Zig(Vertex<T>* d) {
		Vertex<T>* b(d->father);
		if (b->right_son == d) {
			RightRotate(d);
		} else if (b->left_son == d) {
			LeftRotate(d);
		}
		root = FindRoot(root);
	}
开发者ID:luxe,项目名称:Cpp-mini-program-scraps,代码行数:9,代码来源:ideone-c++4.8.1-t550kg-20:02:44_2014-02-27_cET.cpp


示例14: maintain

 void maintain(int &root , bool flag)
 {
     if (root == 0) return ;
     if ( !flag )
     {
         if ( SZ[LC[LC[root]]] > SZ[RC[root]] )
         {
             RightRotate( root );
         }
         else if ( SZ[RC[LC[root]]] > SZ[RC[root]] )
         {
             LeftRotate( LC[root] );
             RightRotate( root );
         }
         else
         {
             return ;
         }
     }
     else
     {
         if ( SZ[RC[RC[root]]] > SZ[LC[root]] )
         {
             LeftRotate( root );
         }
         else if ( SZ[LC[RC[root]]] > SZ[LC[root]] )
         {
             RightRotate( RC[root] );
             LeftRotate( root );
         }
         else
         {
             return ;
         }
     }
     maintain(LC[root] , false);
     maintain(RC[root] , true);
     maintain(root , false);
     maintain(root , true);
 }
开发者ID:fanshaoze,项目名称:acm-codes,代码行数:40,代码来源:NOTONLLY的SBT模板.cpp


示例15: while

void RedBlackTree::RBDeleteFix(RedBlackNode *pNode)
{
    while (pNode != m_pRoot && pNode->m_nColor == black)
    {
        if (pNode = pNode->m_pParent->m_pLeft)
        {
            RedBlackNode *pBrother = pNode->m_pParent->m_pRight;
            if (pBrother->m_nColor == red)
            {
                pBrother->m_nColor = black;
                pBrother->m_pParent->m_nColor = red;
                LeftRotate(pNode->m_pParent);
                pBrother = pNode->m_pParent->m_pRight;
            }
            if (pBrother->m_pLeft->m_nColor == black && pBrother->m_pRight->m_nColor == black)
            {
                pBrother->m_nColor = red;
                pNode = pNode->m_pParent;
            }
            else if (pBrother->m_pRight->m_nColor = black)
            {
                pBrother->m_pLeft->m_nColor = black;
                pBrother->m_nColor = red;
                RightRotate(pBrother);
                pBrother = pNode->m_pParent->m_pRight;
            }

            pBrother->m_nColor = pNode->m_pParent->m_nColor;
            pNode->m_pParent->m_nColor = black;
            pBrother->m_pRight->m_nColor = black;
            LeftRotate(pNode->m_pParent);
            pNode = m_pRoot;
        }
        else
        {

        }
    }
    pNode->m_nColor = black;
}
开发者ID:kinnylee,项目名称:Project,代码行数:40,代码来源:RedBlackTree.cpp


示例16: Insert

TNode* Insert(TNode* root,int data){

if(!root){
return NewNode(data);
}

if(root->data>data){
root->left=Insert(root->left,data);
}else{
root->right=Insert(root->right,data);
}

root->height=max(Height(root->left),Height(root->right))+1;

int bf=BalanceFactor(root);

if(bf>1){

if(data<root->left->data){
return RightRotate(root);
}else if(data>=root->left->data){
root->left=LeftRotate(root->left);
return RightRotate(root);
}

}

if(bf<-1){

if(data>root->right->data){
return LeftRotate(root);
}else if(data<root->right->data){
root->right=RightRotate(root->right);
return LeftRotate(root);
}
}

return root;
}
开发者ID:sidpka,项目名称:repo,代码行数:39,代码来源:Array_IsubsequenceOfLength3WithMaxProduct.C


示例17: BSTInsert

bool RedBlackTree<T>::Insert(T item) {
    if (Search(item) == true) { // Make sure no similar item to the passed in one exists in the tree. 
        return false;
    }

    Node<T>* x = BSTInsert(item); // Normal binary tree insertion.
    ++size; // Mainly used to make sure that root assignment in the BSTInsert only done once if there are more than
    //   one item in the tree.



    // Everything below is just to fix the tree after binary insertion.



    if (x != NULL) { // This condition might not be necessary but just to make
        //   sure that there is an item that was inserted (i.e. BSTInsert did not return NULL).
        x->is_black = false;
        Node<T>* y = NULL;
        while (x != root && x->p != NULL && x->p->is_black == false) { // Iterate until root or parent is reached.
            if (x->p->p != NULL && x->p == x->p->p->left) {
                if (x->p != NULL && x->p->p != NULL) {
                    y = x->p->p->right; // "Uncle" of x.
                }

                if (y != NULL && y->is_black == false) { // Same as x->p.
                    x->p->is_black = true;
                    y->is_black = true;
                    x->p->p->is_black = false;
                    x = x->p->p;
                } else { // y->is_black = true.
                    if (x->p != NULL && x == x->p->right) {
                        x = x->p;
                        LeftRotate(x);
                    }
                    if (x->p != NULL && x->p->p != NULL) {
                        x->p->is_black = true;
                        x->p->p->is_black = false;
                        RightRotate(x->p->p);
                    }
                }
            } else { // Symmetric to the case above, by changing every left word with right,
                //   and every left rotation with right rotation.
                if (x->p != NULL && x->p->p != NULL) {
                    y = x->p->p->left;
                }

                if (y != NULL && y->is_black == false) {
                    x->p->is_black = true;
                    y->is_black = true;
                    x->p->p->is_black = false;
                    x = x->p->p;
                } else {
                    if (x->p != NULL && x == x->p->left) {
                        x = x->p;
                        RightRotate(x);
                    }
                    if (x->p != NULL && x->p->p != NULL) {
                        x->p->is_black = true;
                        x->p->p->is_black = false;
                        LeftRotate(x->p->p);
                    }
                }
            }
        }
    }

    root->is_black = true;
    return true;
}
开发者ID:falhumai96,项目名称:Stock-System-Simulator,代码行数:70,代码来源:redblacktree.cpp


示例18: while

//更新深度
void MyTree::UpdateDepth(TreeNode* pNode)
{
  TreeNode* pParent = pNode->m_pParent; 
  while(pParent)
  {
    //设置父节点的深度
    int nLeftDepth = GetDepth(pParent->m_pLeft);
    int nRightDepth = GetDepth(pParent->m_pRight);
    pParent->m_nDepth = max(nLeftDepth, nRightDepth) + 1;

    //判断是否平衡
    if(abs(nLeftDepth - nRightDepth) >= 2)
    {
      TreeNode* pNode1 = pParent;
      TreeNode* pNode2 = NULL;
      TreeNode* pNode3 = NULL;

      if(nLeftDepth < nRightDepth)
      {
        pNode2 = pNode1->m_pRight;
      }
      else
      {
        pNode2 = pNode1->m_pLeft;
      }

      nLeftDepth = GetDepth(pNode2->m_pLeft);
      nRightDepth = GetDepth(pNode2->m_pRight);

      if(nLeftDepth < nRightDepth)
      {
        pNode3 = pNode2->m_pRight;
      }
      else
      {
        pNode3 = pNode2->m_pLeft;
      }

//       if(pNode1->m_pLeft)
//       {
//         pNode2 = pNode1->m_pLeft;
//       }
//       else
//       {
//         pNode2 = pNode1->m_pRight;
//       }
// 
//       if(pNode2->m_pLeft)
//       {
//         pNode3 = pNode2->m_pLeft;
//       }
//       else
//       {
//         pNode3 = pNode2->m_pRight;
//       }

      //进行相关的旋转操作
      if(pNode1->m_pLeft == pNode2 && pNode2->m_pLeft == pNode3)
      {
        //右单旋
        RightRotate(pNode1, pNode2);
      }
      else if(pNode1->m_pRight == pNode2 && pNode2->m_pRight == pNode3)
      {
        //左单旋
        LeftRotate(pNode1, pNode2);
      }
      else if(pNode1->m_pRight == pNode2 && pNode2->m_pLeft == pNode3)
      {
        //先右单旋 再左单旋
        RightRotate(pNode2, pNode3);
        LeftRotate(pNode1, pNode3);
      }
      else if(pNode1->m_pLeft == pNode2 && pNode2->m_pRight == pNode3)
      {
        //先左单旋 再右单旋
        LeftRotate(pNode2, pNode3);
        RightRotate(pNode1, pNode3);
      }
    }

    pParent = pParent->m_pParent;
  }
}
开发者ID:styxschip,项目名称:Note,代码行数:85,代码来源:MyTree.cpp


示例19: Insert

static
std_map_node * Insert(std_map* self, void* pKey, void* pData)
{
	std_map_node * y;
	std_map_node * x;
	std_map_node * newNode;

	x=(std_map_node*) GaloisMalloc(sizeof(std_map_node));
	if (!x) return NULL;
	x->pKey=pKey;
	x->pData=pData;

	InsertHelper(self,x);
	newNode=x;
	x->bRed=1;
	while(x->pParent->bRed)
	{ /* use sentinel instead of checking for root */
		if (x->pParent == x->pParent->pParent->pLeft)
		{
			y=x->pParent->pParent->pRight;
			if (y->bRed)
			{
				x->pParent->bRed=0;
				y->bRed=0;
				x->pParent->pParent->bRed=1;
				x=x->pParent->pParent;
			}
			else
			{
				if (x == x->pParent->pRight)
				{
					x=x->pParent;
					LeftRotate(self,x);
				}
				x->pParent->bRed=0;
				x->pParent->pParent->bRed=1;
				RightRotate(self,x->pParent->pParent);
			} 
		}
		else
		{
			/* case for x->pParent == x->pParent->pParent->pRight */
			y=x->pParent->pParent->pLeft;
			if (y->bRed)
			{
				x->pParent->bRed=0;
				y->bRed=0;
				x->pParent->pParent->bRed=1;
				x=x->pParent->pParent;
			}
			else
			{
				if (x == x->pParent->pLeft)
				{
					x=x->pParent;
					RightRotate(self,x);
				}
				x->pParent->bRed=0;
				x->pParent->pParent->bRed=1;
				LeftRotate(self,x->pParent->pParent);
			} 
		}
	}
	self->m_pRoot->pLeft->bRed=0;
	return(newNode);

}
开发者ID:cjheres,项目名称:linux-3.0.8,代码行数:67,代码来源:std_map.c


示例20: RBDeleteFixUp

static
void RBDeleteFixUp(std_map* self, std_map_node* x) {
	std_map_node* root=self->m_pRoot->pLeft;
	std_map_node* w;

	while( (!x->bRed) && (root != x))
	{
		if (x == x->pParent->pLeft)
		{
			w=x->pParent->pRight;
			if (w->bRed)
			{
				w->bRed=0;
				x->pParent->bRed=1;
				LeftRotate(self,x->pParent);
				w=x->pParent->pRight;
			}
			if ( (!w->pRight->bRed) && (!w->pLeft->bRed) )
			{
				w->bRed=1;
				x=x->pParent;
			}
			else
			{
				if (!w->pRight->bRed)
				{
					w->pLeft->bRed=0;
					w->bRed=1;
					RightRotate(self,w);
					w=x->pParent->pRight;
				}
				w->bRed=x->pParent->bRed;
				x->pParent->bRed=0;
				w->pRight->bRed=0;
				LeftRotate(self,x->pParent);
				x=root; /* this is to exit while loop */
			}
		}
		else
		{ /* the code below is has left and right switched from above */
			w=x->pParent->pLeft;
			if (w->bRed)
			{
				w->bRed=0;
				x->pParent->bRed=1;
				RightRotate(self,x->pParent);
				w=x->pParent->pLeft;
			}
			if ( (!w->pRight->bRed) && (!w->pLeft->bRed) )
			{
				w->bRed=1;
				x=x->pParent;
			}
			else
			{
				if (!w->pLeft->bRed)
				{
					w->pRight->bRed=0;
					w->bRed=1;
					LeftRotate(self,w);
					w=x->pParent->pLeft;
				}
				w->bRed=x->pParent->bRed;
				x->pParent->bRed=0;
				w->pLeft->bRed=0;
				RightRotate(self,x->pParent);
				x=root; /* this is to exit while loop */
			}
		}
	}
	x->bRed=0;
}
开发者ID:cjheres,项目名称:linux-3.0.8,代码行数:72,代码来源:std_map.c



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ Len函数代码示例发布时间:2022-05-30
下一篇:
C++ Left函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap