コード例 #1
0
ファイル: SetImpl.hpp プロジェクト: anareboucas/nanook
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;
}
コード例 #2
0
ファイル: GIntervalTree.cpp プロジェクト: gpertea/gsrc
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
}
コード例 #3
0
ファイル: MapImpl.hpp プロジェクト: anareboucas/nanook
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;
}
コード例 #4
0
ファイル: inttree.c プロジェクト: Acidburn0zzz/yasm
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;
}
コード例 #5
0
ファイル: rbtree.c プロジェクト: MarcNo/lifelines
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
}
コード例 #6
0
ファイル: red_black_tree.c プロジェクト: TidyHuang/vizgems
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
}
コード例 #7
0
ファイル: rbtree592.cpp プロジェクト: jonahkall/blt
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;
}
コード例 #8
0
ファイル: rbtree.cpp プロジェクト: magicdog/algorithm
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;    
}
コード例 #9
0
ファイル: rbtree.cpp プロジェクト: gccli/mylibrary
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;
}
コード例 #10
0
ファイル: rbtree337.cpp プロジェクト: jonahkall/blt
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;
}
コード例 #11
0
ファイル: driver.c プロジェクト: pmiriyals/C_Programs
void testrotate()
{
	struct node* node = buildlistinsortedorder(10);
	printlist(node, "node");
	RightRotate(&node, 2);
	printlist(node, "node after rotating");

}
コード例 #12
0
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;
}
	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);
	}
コード例 #14
0
 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);
 }
コード例 #15
0
ファイル: rbtree.cpp プロジェクト: magicdog/algorithm
void RbTree<Type>::InsertFixup(TreeNodePointer nodepointer) {
    TreeNodePointer brothernode;
    while (nodepointer->m_parent->m_color == RED) {
        if (nodepointer->m_parent == nodepointer->m_parent->m_parent->m_left) {
            brothernode = nodepointer->m_parent->m_parent->m_right;
            if (brothernode->m_color == RED) {
                brothernode->m_color = BLACK;
                brothernode->m_parent->m_color = RED;
                nodepointer->m_parent->m_color = BLACK;
                nodepointer = brothernode->m_parent;
            } else {
                if (nodepointer->m_parent->m_right == nodepointer) {
                    nodepointer = nodepointer->m_parent;
                    LeftRotate(nodepointer);
                }
                nodepointer->m_parent->m_color = BLACK;
                nodepointer->m_parent->m_parent->m_color = RED;
                RightRotate(nodepointer->m_parent->m_parent);
            }
        } else if(nodepointer->m_parent == nodepointer->m_parent->m_parent->m_right) {
            brothernode = nodepointer->m_parent->m_parent->m_left;
            if (brothernode->m_color == RED) {
                brothernode->m_color = BLACK;
                brothernode->m_parent->m_color = RED;
                nodepointer->m_parent->m_color = BLACK;
                nodepointer = nodepointer->m_parent;
            } else {
                if (nodepointer->m_parent->m_left == nodepointer) {
                    nodepointer = nodepointer->m_parent;
                    RightRotate(nodepointer);
                }
                nodepointer->m_parent->m_color = BLACK;
                nodepointer->m_parent->m_parent->m_color = RED;
                LeftRotate(nodepointer->m_parent->m_parent);
            }
        }
        m_root->m_color = BLACK;
        brothernode = NULL;
    }
}
コード例 #16
0
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;
}
コード例 #17
0
ファイル: RedBlackTree.cpp プロジェクト: kinnylee/Project
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;
}
コード例 #18
0
ファイル: SetImpl.hpp プロジェクト: anareboucas/nanook
VOID Set<K,ElementTraits>::DeleteFixup(Node* pNode)
{
	Node* pX = pNode;
	Node* pW = NULL;

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

	pX->m_Status = Node::Black;
}
コード例 #19
0
ファイル: GIntervalTree.cpp プロジェクト: gpertea/gsrc
void IntervalTree::DeleteFixUp(IntervalTreeNode* x) {
  IntervalTreeNode * w;
  IntervalTreeNode * rootLeft = root->left;

  while( (!x->red) && (rootLeft != 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;
      }
      if ( (!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=rootLeft; /* this is to exit while loop */
      }
    } else { /* the code below is has left and right switched from above */
      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=rootLeft; /* this is to exit while loop */
      }
    }
  }
  x->red=0;

#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
  CheckAssumptions();
#elif defined(DEBUG_ASSERT)
  Assert(!nil->red,"nil not black in ITDeleteFixUp");
  Assert((nil->maxHigh=MIN_INT),
	 "nil->maxHigh != MIN_INT in ITDeleteFixUp");
#endif
}
コード例 #20
0
static void RBDeleteFixUp(rb_red_blk_tree* tree, 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(tree,x->parent);
	w=x->parent->right;
      }
      if ( (!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(tree,w);
	  w=x->parent->right;
	}
	w->red=x->parent->red;
	x->parent->red=0;
	w->right->red=0;
	LeftRotate(tree,x->parent);
	x=root; /* this is to exit while loop */
      }
    } else { /* the code below is has left and right switched from above */
      w=x->parent->left;
      if (w->red) {
	w->red=0;
	x->parent->red=1;
	RightRotate(tree,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(tree,w);
	  w=x->parent->left;
	}
	w->red=x->parent->red;
	x->parent->red=0;
	w->left->red=0;
	RightRotate(tree,x->parent);
	x=root; /* this is to exit while loop */
      }
    }
  }
  x->red=0;

#ifdef DEBUG_ASSERT
  Assert(!tree->nil->red,"nil not black in RBDeleteFixUp");
#endif
}
コード例 #21
0
/*
* This function is used to fix-up a tree after a recent
* deletion from the tree
*
* @param Root reference to the main tree
* @param z node to delete to the main tree
* @return N/A
*/
void RBDeleteFixUp(RBTNode **Root, RBTNode *z, RBTNode *parent, BOOL leftNode)
{
	RBTNode *tempDelete = new RBTNode;

	//if z is NULL, we can assume that the node deleted
	//has no children. Change to BLACK, which we revert afterwards
	if (z == NULL)
	{
		z = tempDelete;
		tempDelete = z;
		z->color = BLACK;
	}

	//This is to make sure we are not running this when the tree is just the root
	if (Successor(*Root) == NULL && Predeccessor(*Root) == NULL)
	{
		return;
	}

	//while z is not pointing to the root and has a black node
	while (z != *Root && z->color != RED)
	{
		RBTNode *wNode = NULL;

		//if we know we deleted the left node, proceed
		if (leftNode == TRUE)
		{
			wNode = parent->rightChild;

			//Case 1 - z's Sibling is Red
			if (wNode->color == RED)
			{
				wNode->color = BLACK;
				parent->color = RED;
				LeftRotate(Root, parent);
				wNode = parent->rightChild;
			}

			//Case 2 - z's sibling w is black and both children are black
			if ((wNode->leftChild == NULL) && (wNode->rightChild == NULL) ||
				(wNode->leftChild == NULL) && (wNode->rightChild->color == BLACK) ||
				(wNode->rightChild == NULL) && (wNode->leftChild->color == BLACK) ||
				wNode->leftChild->color == BLACK &&	wNode->rightChild->color == BLACK)
			{
				wNode->color = RED;
				z = parent;
				parent = z->parent;

				if ((parent != NULL) && (parent->leftChild != NULL) && z == parent->leftChild)
				{
					leftNode = TRUE;
				}
				else
				{
					leftNode = FALSE;
				}
			}
			//Case 3 - z's sibling w is black with left red and black right
			else
			{
				if ((wNode->rightChild == NULL) || wNode->rightChild->color == BLACK)
				{
					wNode->leftChild->color = BLACK;
					wNode->color = RED;
					RightRotate(Root, wNode);
					wNode = parent->rightChild;
				}

				//Case 4 - z's sibling w is black with a right child is red
				wNode->color = parent->color;
				parent->color = BLACK;

				if (wNode->rightChild != NULL)
				{
					wNode->rightChild->color = BLACK;
				}

				LeftRotate(Root, parent);
				z = *Root;
			}
		}
		//otherwise we deleted the right node
		else
		{
			wNode = parent->leftChild;

			//Case 1 - z's Sibling is Red
			if (wNode->color == RED)
			{
				wNode->color = BLACK;
				parent->color = RED;
				RightRotate(Root, parent);
				wNode = parent->leftChild;
			}

			//Case 2 - z's sibling w is black and both children are black
			if ((wNode->leftChild == NULL) && (wNode->rightChild == NULL) || 
				(wNode->leftChild == NULL) && (wNode->rightChild->color == BLACK) ||
				(wNode->rightChild == NULL) && (wNode->leftChild->color == BLACK) ||
				wNode->leftChild->color == BLACK &&	wNode->rightChild->color == BLACK)
			{
				wNode->color = RED;
				z = parent;
				parent = z->parent;

				if ((parent != NULL) && (parent->leftChild != NULL) && z == parent->leftChild)
				{
					leftNode = TRUE;
				}
				else
				{
					leftNode = FALSE;
				}
			}
			//Case 3 - z's sibling w is black with left red and black right
			else
			{
				if ((wNode->leftChild == NULL) || wNode->leftChild->color == BLACK)
				{
					wNode->rightChild->color = BLACK;
					wNode->color = RED;
					LeftRotate(Root, wNode);
					wNode = parent->leftChild;
				}

				//Case 4 - z's sibling w is black with a right child is red
				wNode->color = parent->color;
				parent->color = BLACK;

				if (wNode->leftChild != NULL)
				{
					wNode->leftChild->color = BLACK;
				}

				RightRotate(Root, parent);
				z = *Root;
			}
		}

	}

	//make z equal to black (cautionary) and delete our
	//allocated memory
	z->color = BLACK;
	delete(tempDelete);
}
コード例 #22
0
/*
* This function is used to fix the tree given by
* root after an insertion of a new node.
*
* @param Root reference to the main tree
* @param z node to add to the main tree
* @return N/A
*/
void RBInsertFixUp(RBTNode **Root, RBTNode *z)
{
	RBTNode *yNode = NULL;

	while (z != *Root && z->parent->color == RED)
	{
		if ((z->parent != NULL) && z->parent == z->parent->parent->leftChild)
		{
			yNode = z->parent->parent->rightChild;

			//Case 1 - z's uncle y is red
			if (yNode != NULL && yNode->color == RED)
			{
				z->parent->color = BLACK;
				yNode->color = BLACK;
				z->parent->parent->color = RED;
				z = z->parent->parent;
			}
			else
			{
				//Case 2 - z's uncle y is black and z is a right child
				if (z == z->parent->rightChild)
				{
					z = z->parent;
					LeftRotate(Root, z);
				}

				//Case 3 - z's uncle y is black and z is a left child
				z->parent->color = BLACK;
				z->parent->parent->color = RED;
				RightRotate(Root, z->parent->parent);
			}
		}
		else if (z->parent != NULL)
		{
			yNode = z->parent->parent->leftChild;

			//Case 1 - z's uncle y is red
			if (yNode != NULL && yNode->color == RED)
			{
				z->parent->color = BLACK;
				yNode->color = BLACK;
				z->parent->parent->color = RED;
				z = z->parent->parent;
			}
			else
			{
				//Case 2 - z's uncle y is black and z is a right child
				if (z == z->parent->leftChild)
				{
					z = z->parent;
					RightRotate(Root, z);
				}

				//Case 3 - z's uncle y is black and z is a left child
				z->parent->color = BLACK;
				z->parent->parent->color = RED;
				LeftRotate(Root, z->parent->parent);
			}
		}
	}

	//Make sure the root is black
	(*Root)->color = BLACK;
}
コード例 #23
0
ファイル: std_map.c プロジェクト: cjheres/linux-3.0.8
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);

}
コード例 #24
0
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;
}
コード例 #25
0
ファイル: MyTree.cpp プロジェクト: styxschip/Note
//更新深度
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;
  }
}
コード例 #26
0
ファイル: redblacktree.cpp プロジェクト: rliu825/stocksystem
void RedBlackTree<T>::RBDeleteFixUp(Node<T>* x, Node<T>* xparent, bool xisleftchild) {
	Node<T>* y;
	//x == NULL is imaginary node(treated as black too) x.is_black is black node
	while (x != root && (x == NULL || x->is_black == true)) {
		//x is leftchild -->if (x == x.p.left)
		if (xisleftchild == true)
		{
			y = xparent->right;
			//uncle y is red
			if (y->is_black == false && y != NULL) {
				y->is_black = true;
				xparent->is_black = false;
				LeftRotate(xparent);
				y = xparent->right;
			}
			//both y's children are black
			if ((y->left->is_black == true || y->left == NULL) && (y->right->is_black == true || y->right == NULL)) {
				//make y red
				y->is_black = false;
				x = xparent;
			}

			else {
				// y's red child not in line with x's parent
				if (y->right == NULL || y->right->is_black == true) {
					y->left->is_black = true;
					y->is_black = false;
					RightRotate(y);
					y = xparent->right;
				}
				y->is_black = xparent->is_black;
				xparent->is_black = true;
				y->right->is_black = true;
				LeftRotate(xparent);
				x = root;
			}
		}
		else {//symmetric to if: x is rightchild
			y = xparent->left;
			if (y->is_black == false) {
				y->is_black = true;
				xparent->is_black = false;
				RightRotate(xparent);
				y = xparent->left;
			}
			if ((y->left == NULL || y->left->is_black == true) && (y->right == NULL || y->right->is_black == true)) {
				y->is_black = false;
				x = xparent;
				//??
			}
			else {
				if (y->left == NULL || y->left->is_black == true) {
					y->right->is_black = true;
					y->is_black = false;
					LeftRotate(y);
					y = xparent->left;
				}
				y->is_black = xparent->is_black;
				xparent->is_black = true;
				y->left->is_black = true;
				RightRotate(xparent);
				x = root;
			}
		}
	}
	x->is_black = true;
}
コード例 #27
0
ファイル: std_map.c プロジェクト: cjheres/linux-3.0.8
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;
}
コード例 #28
0
ファイル: tree.c プロジェクト: why0603/angel
void DeleteNodeFix(TreePtr tree,TreeNodePtr node)
{
	TreeNodePtr brother = NULL;
	TreeNodePtr parent = NULL;
	NodePosition position;
	TreeColor brother_color,parent_color,color,brother_left_color,brother_right_color;
	parent = node->parent;
	//当前节点是父节点的左孩子还是右孩子
	position = (node == parent->left) ? LeftNode : RightNode;
	brother = (node == parent->left) ? parent->right : parent->left;
	brother_color = (NULL == brother) ? BlackNode : brother->color;
	parent_color = parent->color;
	color = node->color;
	//兄弟节点时红色
	if (brother_color == RedNode)
	{
		if (position == LeftNode)
		{
			LeftRotate(tree,parent);
		}
		else
		{
			RightRotate(tree,parent);
		}
		//交互兄弟节点与父节点的颜色
		brother->color = BlackNode;
		parent->color = RedNode;
		//经过上面的转换,brother成为红色,兄弟节点成为黑色
		//则需要按照下面的逻辑处理
		DeleteNodeFix(tree,node);
	}
	else if ((NULL != brother) && (brother_color == BlackNode))
	{
		brother_left_color = (NULL == brother->left) ? BlackNode : brother->left->color;
		brother_right_color = (NULL == brother->right) ? BlackNode : brother->right->color;
		//全部为黑色 case 1
		if ((parent_color == BlackNode) && 
			//(brother_color == BlackNode) && 
			(brother_left_color == BlackNode) && 
			(brother_right_color == BlackNode))
		{
			//这样不违反红黑树特性,又让兄弟节点少了一个黑色节点,刚好平衡
			brother->color = RedNode;
		}//父节点为红色,其他都是黑色 case 2
		else if ((parent_color == RedNode) && 
			//(brother_color == BlackNode) && 
			(brother_left_color == BlackNode) && 
			(brother_right_color == BlackNode))
		{
			//交互兄弟节点与父节点的颜色
			//这样等于将删除的黑色节点补齐,兄弟节点没有变化
			brother->color = BlackNode;
			parent->color = RedNode;
		}//case 3,转为符合case 5的情形
		else if ((position == LeftNode) && 
			(brother_left_color == RedNode) && //左孩子是红色
			(brother_right_color == BlackNode))
		{//这样就变成了有一个右孩子是红色的兄弟节点
			RightRotate(tree,brother);
			//兄弟节点与他的左孩子节点颜色
			brother->color = RedNode;
			brother->left->color = BlackNode;
			DeleteNodeFix(tree,node);
		}//case 4,转为符合case 6的情形
		else if ((position == RightNode) && //按照目前的算法,这种情况是不可能会有的
			(brother_left_color == RedNode) && 
			(brother_right_color == BlackNode))//右孩子是红色
		{//获得一个红色兄弟
			LeftRotate(tree,brother);
			brother->color = RedNode;
			brother->right->color = BlackNode;
			DeleteNodeFix(tree,node);
		}//case 5
		else if ((position == LeftNode) &&
			(brother_right_color == RedNode))
		{
			brother->color = parent->color;
			parent->color = BlackNode;
			brother->right->color = BlackNode;
			//给N添加了一个黑色节点
			LeftRotate(tree,parent);
		}//case 6
		else if ((position == RightNode) &&
			(brother_left_color == RedNode))
		{
			brother->color = parent->color;
			parent->color = BlackNode;
			brother->left->color = BlackNode;
			//给N添加了一个黑色节点
			RightRotate(tree,parent);
		}
		else
		{
			//printf("\r\n 难道还有其他情况 \r\n");
		}
	}
	else
	{
		//printf("\r\n brother node = null \r\n");
	}
}
コード例 #29
0
ファイル: RedBlackTree.cpp プロジェクト: kinnylee/Project
/***
    Six situations:
    but we only research three situations, other three situations is symmetrical
    1£ºparent is red, and uncle also red 
            do: change parent and uncle color to black, and change grandfather to red,
                the current point to grandfather
    2£ºparent is red, but uncle is black, current node is parent right child 
            do: the current point to parent and left rotate the current node
            Because: situation 2 left rotate will become to situation 3
    3£ºparent is red, but uncle is black, current node is parent left child 
            do: the parent color change to black and the grandfather color change
                to red, then right rotate the grandfather
*/
void RedBlackTree::RBInsertFix(RedBlackNode *pNode)
{
    RedBlackNode *pUncle = nullptr;
    while (pNode->m_pParent->m_nColor == red)
    {
        if (pNode->m_pParent == pNode->m_pParent->m_pLeft)
        {
            pUncle = pNode->m_pParent->m_pRight;
            if (pUncle->m_nColor == red)
            {
                pNode->m_pParent->m_nColor = black;
                pUncle->m_nColor = black;
                pNode->m_pParent->m_pParent->m_nColor = red;
                pNode = pNode->m_pParent->m_pParent;
            }
            else if (pNode == pNode->m_pParent->m_pRight)
            {
                pNode = pNode->m_pParent;
                LeftRotate(pNode);
                pNode->m_pParent->m_nColor = black;
                pNode->m_pParent->m_pParent->m_nColor = red;
                RightRotate(pNode->m_pParent->m_pParent);
            }
            else
            {
                pNode = pNode->m_pParent;
                RightRotate(pNode);
                pNode->m_pParent->m_nColor = black;
                pNode->m_pParent->m_pParent->m_nColor = red;
                LeftRotate(pNode->m_pParent->m_pParent);
            }
        }
        else
        {
            pUncle = pNode->m_pParent->m_pLeft;
            if (pUncle->m_nColor == red)
            {
                pNode->m_pParent->m_nColor = black;
                pUncle->m_nColor = black;
                pNode->m_pParent->m_pParent->m_nColor = red;
                pNode = pNode->m_pParent->m_pParent;
            }
            else if (pNode == pNode->m_pParent->m_pLeft)
            {
                pNode = pNode->m_pParent;
                RightRotate(pNode);
                pNode->m_pParent->m_nColor = black;
                pNode->m_pParent->m_pParent->m_nColor = red;
                LeftRotate(pNode->m_pParent->m_pParent);
            }
            else
            {
                pNode = pNode->m_pParent;
                LeftRotate(pNode);
                pNode->m_pParent->m_nColor = black;
                pNode->m_pParent->m_pParent->m_nColor = red;
                RightRotate(pNode->m_pParent->m_pParent);
            }
        }
    }
    m_pRoot->m_nColor = black;
}
コード例 #30
0
ファイル: tree.c プロジェクト: why0603/angel
static void CheckTree(TreePtr tree,TreeNodePtr node)
{
	//父节点,祖父节点,叔父节点
	TreeNodePtr parent,grand,uncle;//great

	//与父节点的关系(左孩子还是右孩子),父节点与祖父节点的关系
	NodePosition selfPosition,parentPosition;
	parent = node->parent;
	/*
	great = (NULL != parent) ? parent->parent : NULL;
	uncle = (NULL != great) ? ((great->left == parent) ? great->right : great->left) : NULL;
	TreeColor selfColor = node->color;
	TreeColor parentColor = (NULL != parent) ? parent->color : BlackNode;
	TreeColor uncleColor = (NULL != uncle) ? uncle->color : BlackNode;
	TreeColor greatColor = (NULL != great) ? great->color : BlackNode;
	*/
	//父节点为NULL,这表示是根节点
	if (NULL == parent)
	{
		SetRootNode(tree,node);
	}
	else if ((node->color == RedNode) && (parent->color == RedNode))
	{
		//自身节点 父节点为红色
		//这个时候一定会有一个祖父节点
		grand = parent->parent;
		uncle = (grand->left == parent) ? grand->right : grand->left;
		//自身节点 父节点 叔父节点都为红色
		if ((NULL != uncle) && (uncle->color == RedNode))
		{
			grand->color = RedNode;
			parent->color = BlackNode;
			uncle->color = BlackNode;
			CheckTree(tree,grand);
			//将祖父节点作为新插入的节点开始检测树的合法性
		}
		else  //叔父节点是黑色或者空节点
		{
			//自身节点 父节点为红色 叔父节点为黑色(NULL表示黑色)
			selfPosition = (node == parent->left) ? LeftNode : RightNode;
			parentPosition = (parent == grand->left) ? LeftNode : RightNode;
			//旋转分为 左左 左右 右左 右右
			if ((selfPosition == LeftNode) && (parentPosition == LeftNode))
			{
				/*
				G(b)                    P(b)
				/      \                 /     \
				P(r)     U(b)   -->      N(r)    G(r)
				/   \                            /    \
				N(r)    M                          M     U(b)
				*/
				RightRotate(tree,grand);
				//左左的情况需要更换节点颜色,可以知道的是祖父节点肯定是黑色
				parent->color = BlackNode;
				grand->color = RedNode;
			}
			else if ((parentPosition == RightNode) && (selfPosition == LeftNode))
			{
				//右左
				//以父节点右转,这个时候将变成右右的关系
				RightRotate(tree,parent);
				//要是减少递归,可以再转一次
				//CheckTree(tree,node);
				LeftRotate(tree,grand);
				parent->color = BlackNode;
				grand->color = RedNode;
			}
			else if ((parentPosition == LeftNode) && (selfPosition == RightNode))
			{
				//左右
				//以父节点左转,这个时候将变成左左的关系
				LeftRotate(tree,parent);
				//下次检测则会发现是左左关系
				//要是减少递归,可以再转一次
				//CheckTree(tree,node);
				RightRotate(tree,grand);
				parent->color = BlackNode;
				grand->color = RedNode;
			}
			else
			{
				//右右
				/*
				G(b)                    P(b)
				/      \                 /    \
				U(b)     P(r)   -->      G(r)   N(r)
				/     \          /   \
				M      N(r)     U(b)   M
				*/
				LeftRotate(tree,grand);
				parent->color = BlackNode;
				grand->color = RedNode;
			}
		}
		//这里发现祖父节点肯定被染成了红色,而父节点都被染成黑色
		//因为插入的是红色节点,而一个红节点必须包含两个黑色节点
		//·
	}
	else
	{
		//这样表示调整基本完毕
	}
}