void RedBlackTree::Insert_Fixup(RedBlackNode::Ptr z) { while(z->Parent()->isRed() && z->Parent() != nil) { if(z->Parent() == z->Parent()->Parent()->Left()) { RedBlackNode::Ptr y = z->Parent()->Parent()->Right(); if(y->isRed()) { z->Parent()->Color(RedBlackNode::BLACK); //Case 1 y->Color(RedBlackNode::BLACK); z->Parent()->Parent()->Color(RedBlackNode::RED); z = z->Parent()->Parent(); } else { if(z == z->Parent()->Right()) { z = z->Parent(); //Case 2 Left_Rotate(z); //Case 2 } z->Parent()->Color(RedBlackNode::BLACK); //Case 3 z->Parent()->Parent()->Color(RedBlackNode::RED); //Case 3 Right_Rotate(z->Parent()->Parent()); //Case 3 } } else { RedBlackNode::Ptr y = z->Parent()->Parent()->Left(); if(y->isRed()) { z->Parent()->Color(RedBlackNode::BLACK); //Case 1 y->Color(RedBlackNode::BLACK); z->Parent()->Parent()->Color(RedBlackNode::RED); z = z->Parent()->Parent(); } else { if(z == z->Parent()->Left()) { z = z->Parent(); //Case 2 Right_Rotate(z); //Case 2 } z->Parent()->Color(RedBlackNode::BLACK); //Case 3 z->Parent()->Parent()->Color(RedBlackNode::RED); //Case 3 Left_Rotate(z->Parent()->Parent()); //Case 3 } } } root->Color(RedBlackNode::BLACK); }
void SplayTree(Splay_Node* node) { //Rotate until node become root; while(1) { if(node -> Parent == NULL) { root = node; //preOrder(node); //printf("\n"); break; } // Parent is root else if(node -> Parent -> Parent == NULL) { //Right_Rotate if(node -> Parent -> Lchild == node) {Right_Rotate(node);} //Left_Rotate else if(node -> Parent -> Rchild == node){Left_Rotate(node);} } //Parent is not root else if(node -> Parent -> Parent != NULL) { //Right_Right if(node -> Parent -> Lchild == node && node -> Parent -> Parent -> Lchild == node -> Parent) { Right_Rotate(node->Parent); Right_Rotate(node); } //Left_Left else if(node -> Parent -> Rchild == node && node -> Parent -> Parent -> Rchild == node -> Parent) { Left_Rotate(node->Parent); Left_Rotate(node); } //Right_Left else if(node -> Parent -> Lchild == node && node -> Parent -> Parent -> Rchild == node -> Parent) { Right_Rotate(node); Left_Rotate(node); } //Left_Right else if(node -> Parent -> Rchild == node && node -> Parent -> Parent -> Lchild == node -> Parent ) { Left_Rotate(node); Right_Rotate(node); } } } }
void redblack_node::RB_Delete_Fixup( redblack_node *T, redblack_node *x ) { redblack_node *w, *temp = T; while( temp->parent != NULL ) temp = temp->parent; // point temp to the root of T while( x != temp && strcmp( x->color, "BLACK" ) == 0 ) { if( x->parent == NULL ) x->parent = new redblack_node; if( x == x->parent->left ) { w = x->parent->right; if( strcmp( w->color, "RED" ) == 0 ) { w->color = strdup( "BLACK" ); x->parent->color = strdup( "RED" ); Left_Rotate( T, x->parent ); w = x->parent->right; } if( strcmp( w->left->color, "BLACK" ) == 0 && strcmp( w->right->color, "BLACK" ) == 0 ) { w->color = strdup( "RED" ); x = x->parent; } else if( strcmp( w->right->color, "BLACK" ) == 0 ) { w->left->color = strdup( "BLACK" ); w->color = strdup( "RED" ); Right_Rotate( T, w ); w = x->parent->right; } w->color = strdup( x->parent->color ); x->parent->color = strdup( "BLACK" ); w->right->color = strdup( "BLACK" ); Left_Rotate( T, x->parent ); x = temp; } else { w = x->parent->left; if( w == NULL ) w = new redblack_node(); if( strcmp( w->color, "RED" ) == 0 ) { w->color = strdup( "BLACK" ); x->parent->color = strdup( "RED" ); Left_Rotate( T, x->parent ); w = x->parent->left; } if( w->right == NULL ) w->right = new redblack_node(); if( w->left == NULL ) w->left = new redblack_node(); if( strcmp( w->right->color, "BLACK" ) == 0 && strcmp( w->left->color, "BLACK" ) == 0 ) { w->color = strdup( "RED" ); x = x->parent; } else if( strcmp( w->left->color, "BLACK" ) == 0 ) { w->right->color = strdup( "BLACK" ); w->color = strdup( "RED" ); Right_Rotate( T, w ); w = x->parent->left; } if( x->parent == NULL ) x->parent = new redblack_node(); w->color = strdup( x->parent->color ); x->parent->color = strdup( "BLACK" ); w->left->color = strdup( "BLACK" ); Left_Rotate( T, x->parent ); x = temp; } } x->color = strdup( "BLACK" ); }
void redblack_node::RB_Insert_Fixup( redblack_node *T, redblack_node *z ) { redblack_node *y = new redblack_node(), *temp = T; while( temp->parent != NULL ) temp = temp->parent; // point temp to the root of T while( strcmp( z->parent->color, "RED" ) == 0 ) { if( z->parent == z->parent->parent->left ) { y = z->parent->parent->right; if( y == NULL ) y = new redblack_node(); if( strcmp(y->color, "RED") == 0 ) { z->parent->color = strdup( "BLACK" ); y->color = strdup( "BLACK" ); z->parent->parent->color = strdup( "RED" ); z = z->parent->parent; } else if( z == z->parent->right ) { z = z->parent; Left_Rotate( T, z ); } z->parent->color = strdup( "BLACK" ); z->parent->parent->color = strdup( "RED" ); Right_Rotate( T, z->parent->parent ); } else { y = z->parent->parent->left; if( y == NULL ) y = new redblack_node(); if( strcmp(y->color, "RED") == 0 ) { z->parent->color = strdup( "BLACK" ); y->color = strdup( "BLACK" ); z->parent->parent->color = strdup( "RED" ); z = z->parent->parent; } else if( z == z->parent->left ) { z = z->parent; Right_Rotate( T, z ); } if( z->parent == NULL ) z->parent = new redblack_node(); z->parent->color = strdup( "BLACK" ); if( z->parent->parent == NULL ) z->parent->parent = new redblack_node(); z->parent->parent->color = strdup( "RED" ); Left_Rotate( T, z->parent->parent ); } } temp->color = strdup( "BLACK" ); }
pNode Tree_Delete_Fixup( pNode root, pNode t ) { while( (t != root ) && ( t -> color ) == "BLACK") { //{* the t is the left subtree if( t == ( t -> parent -> left ) ) { pNode BrotherNode = t -> parent -> right; //{*Case1: the BrotherNode's color is red if( BrotherNode -> color == "RED" ) { BrotherNode -> color = "BLACK"; t -> parent -> color = "RED"; Left_Rotate(root, t -> parent); BrotherNode = t -> parent -> right; } //*} //{* Case2: the BrotherNode and its children are all black. if( ( BrotherNode -> left -> color == "BLACK" )&& ( BrotherNode ->right -> color == "BLACK" )) { BrotherNode -> color = "RED"; t = t -> parent; }//*} else { //{* Case3: the BotherNode and his right is blackk , and its left child is red. if( BrotherNode -> right -> color == "BLACK") { BrotherNode -> left -> color = "BLACK"; BrotherNode -> color = "RED"; Right_Rotate( root, BrotherNode); BrotherNode = t -> parent -> right; } //*} //{* Case4: BrotherNode's color is black, and its right child is red BrotherNode -> color = t -> parent -> color; t -> parent -> color = "BLACK"; BrotherNode -> right -> color = "BLACK"; Left_Rotate(root, t -> parent); t = root; //*} } } //*} else { pNode BrotherNode = t -> parent -> left; if( BrotherNode -> color == "RED" ) { BrotherNode -> color = "BLACK"; t -> parent -> color = "RED"; Right_Rotate( root, t -> parent); BrotherNode = t -> parent -> left; } if( (BrotherNode -> left -> color == "BLACK") &&( BrotherNode -> right -> color == "BLACK") ) { BrotherNode -> color = "RED"; t = t -> parent; } else { if( BrotherNode -> right -> color == "BLACK" ) { BrotherNode -> right -> color = "BLACK"; BrotherNode -> color = "RED"; Left_Rotate(root, BrotherNode); BrotherNode = t -> parent -> left; } BrotherNode -> color = t -> parent -> color; t -> parent -> color = "BLACK"; BrotherNode -> left -> color = "BLACK"; Right_Rotate( root, t -> parent ); } } } t = root; t -> color = "BLACK"; return root; }
void RedBlackTree::Delete_Fixup(RedBlackNode::Ptr x) { while(x != root && x->Color() == RedBlackNode::BLACK) { if(x == x->Parent()->Left()) { RedBlackNode::Ptr w = x->Parent()->Right(); if(w->isRed()) { w->Color(RedBlackNode::BLACK); //Case 1 x->Parent()->Color(RedBlackNode::RED); //Case 1 Left_Rotate(x->Parent()); //Case 1 w = x->Parent()->Right(); //Case 1 } if(w->Left()->Color() == RedBlackNode::BLACK && w->Right()->Color() == RedBlackNode::BLACK) { w->Color(RedBlackNode::RED); //Case 2 x = x->Parent(); //Case 2 } else { if(w->Right()->Color() == RedBlackNode::BLACK) { w->Left()->Color(RedBlackNode::BLACK); //Case 3 w->Color(RedBlackNode::RED); //Case 3 Right_Rotate(w); //Case 3 w = x->Parent()->Right(); //Case 3 } w->Color(x->Parent()->Color()); //Case 4 x->Parent()->Color(RedBlackNode::BLACK); //Case 4 w->Right()->Color(RedBlackNode::BLACK); //Case 4 Left_Rotate(x->Parent()); x = root; } } else { RedBlackNode::Ptr w = x->Parent()->Left(); if(w->isRed()) { w->Color(RedBlackNode::BLACK); //Case 1 x->Parent()->Color(RedBlackNode::RED); //Case 1 Right_Rotate(x->Parent()); //Case 1 w = x->Parent()->Left(); //Case 1 } if(w->Right()->Color() == RedBlackNode::BLACK && w->Left()->Color() == RedBlackNode::BLACK) { w->Color(RedBlackNode::RED); //Case 2 x = x->Parent(); //Case 2 } else { if(w->Left()->Color() == RedBlackNode::BLACK) { w->Right()->Color(RedBlackNode::BLACK); //Case 3 w->Color(RedBlackNode::RED); //Case 3 Left_Rotate(w); //Case 3 w = x->Parent()->Left(); //Case 3 } w->Color(x->Parent()->Color()); //Case 4 x->Parent()->Color(RedBlackNode::BLACK); //Case 4 w->Left()->Color(RedBlackNode::BLACK); //Case 4 Right_Rotate(x->Parent()); x = root; } } } x->Color(RedBlackNode::BLACK); }
int Insert(NodeP Tree, int Val) { //printf("Insert %d\n", Val); if (Tree == NULL) { fprintf(stderr, "Tree hasn't been initialized\n"); return -1; } //Case 1 if (Tree->left == NULL && Tree->right == NULL) { NodeP root = Create_New_Node(Tree, Val); Tree->left = Tree->right = root; root->color = BLACK; //root must be black return 0; } NodeP Current = Tree->left; //start of the actual values in the tree NodeP Parent = NULL; int side = -1; while (1) { if (Current == NULL) { //Insert value break; } if (Current->value > Val) { Parent = Current; Current = Current->left; side = LEFT; } else if (Current->value < Val) { Parent = Current; Current = Current->right; side = RIGHT; } else if (Current->value == Val) { //value already in tree return 0; } } assert(Parent != NULL); NodeP newInsertion = Create_New_Node(Parent, Val); if (side == RIGHT) { assert(Parent->right == NULL); Parent->right = newInsertion; } else { assert(Parent->left == NULL); Parent->left = newInsertion; } //Case 2 if (Parent->color == BLACK) { return 0; } Current = newInsertion; NodeP GrandParent = NULL; NodeP Uncle = NULL; while (1) { Parent = Current->parent; //Case 1 if (Parent->color == NO_COLOR) { Current->color = BLACK; break; } //Case 2 if (Parent->color == BLACK) { break; } GrandParent = Parent->parent; if (GrandParent->left == Parent) { Uncle = GrandParent->right; } else { Uncle = GrandParent->left; } //Case 3 if ((Uncle != NULL) && (Uncle->color == RED)) { Parent->color = BLACK; Current->color = BLACK; GrandParent->color = RED; Current = GrandParent; continue; } //Case 4 if ((Current == Parent->right) && (Parent == GrandParent->left)) { Left_Rotate(Parent); Parent = GrandParent->left; Current = Parent->left; } else if ((Current == Parent->left) && (Parent == GrandParent->right)) { Right_Rotate(Parent); Parent = GrandParent->right; Current = Parent->right; } //Case 5 Parent->color = BLACK; GrandParent->color = RED; if (Current == Parent->left) { Right_Rotate(GrandParent); } else { Left_Rotate(GrandParent); } break; } return 0; }