voiddelete_one_child(structnode*n){/* * Precondition: n has at most one non-null child. */structnode*child=is_leaf(n->right)?n->left:n->right;replace_node(n,child);if(n->color==BLACK){if(child->color==RED)child->color=BLACK;elsedelete_case1(child);}free(n);}
voiddelete_case5(structnode*n){structnode*s=sibling(n);if(s->color==BLACK){/* this if statement is trivial, due to Case 2(even though Case two changed the sibling to a sibling's child, the sibling's child can't be red, since no red parent can have a red child). */// the following statements just force the red to be on the left of the left of the parent, // or right of the right, so case six will rotate correctly.if((n==n->parent->left)&&(s->right->color==BLACK)&&(s->left->color==RED)){// this last test is trivial too due to cases 2-4.s->color=RED;s->left->color=BLACK;rotate_right(s);}elseif((n==n->parent->right)&&(s->left->color==BLACK)&&(s->right->color==RED)){// this last test is trivial too due to cases 2-4.s->color=RED;s->right->color=BLACK;rotate_left(s);}}delete_case6(n);}
#define NIL NULL#define BLACK 1#define RED 0#include<iostream>usingnamespacestd;classbst{private:structNode{intvalue;boolcolor;Node*leftTree,*rightTree,*parent;Node():value(0),color(RED),leftTree(NULL),rightTree(NULL),parent(NULL){}Node*grandparent(){if(parent==NULL){returnNULL;}returnparent->parent;}Node*uncle(){if(grandparent()==NULL){returnNULL;}if(parent==grandparent()->rightTree)returngrandparent()->leftTree;elsereturngrandparent()->rightTree;}Node*sibling(){if(parent->leftTree==this)returnparent->rightTree;elsereturnparent->leftTree;}};voidrotate_right(Node*p){Node*gp=p->grandparent();Node*fa=p->parent;Node*y=p->rightTree;fa->leftTree=y;if(y!=NIL)y->parent=fa;p->rightTree=fa;fa->parent=p;if(root==fa)root=p;p->parent=gp;if(gp!=NULL){if(gp->leftTree==fa)gp->leftTree=p;elsegp->rightTree=p;}}voidrotate_left(Node*p){if(p->parent==NULL){root=p;return;}Node*gp=p->grandparent();Node*fa=p->parent;Node*y=p->leftTree;fa->rightTree=y;if(y!=NIL)y->parent=fa;p->leftTree=fa;fa->parent=p;if(root==fa)root=p;p->parent=gp;if(gp!=NULL){if(gp->leftTree==fa)gp->leftTree=p;elsegp->rightTree=p;}}voidinorder(Node*p){if(p==NIL)return;if(p->leftTree)inorder(p->leftTree);cout<<p->value<<" ";if(p->rightTree)inorder(p->rightTree);}stringoutputColor(boolcolor){returncolor?"BLACK":"RED";}Node*getSmallestChild(Node*p){if(p->leftTree==NIL)returnp;returngetSmallestChild(p->leftTree);}booldelete_child(Node*p,intdata){if(p->value>data){if(p->leftTree==NIL){returnfalse;}returndelete_child(p->leftTree,data);}elseif(p->value<data){if(p->rightTree==NIL){returnfalse;}returndelete_child(p->rightTree,data);}elseif(p->value==data){if(p->rightTree==NIL){delete_one_child(p);returntrue;}Node*smallest=getSmallestChild(p->rightTree);swap(p->value,smallest->value);delete_one_child(smallest);returntrue;}else{returnfalse;}}voiddelete_one_child(Node*p){Node*child=p->leftTree==NIL?p->rightTree:p->leftTree;if(p->parent==NULL&&p->leftTree==NIL&&p->rightTree==NIL){p=NULL;root=p;return;}if(p->parent==NULL){deletep;child->parent=NULL;root=child;root->color=BLACK;return;}if(p->parent->leftTree==p){p->parent->leftTree=child;}else{p->parent->rightTree=child;}child->parent=p->parent;if(p->color==BLACK){if(child->color==RED){child->color=BLACK;}elsedelete_case(child);}deletep;}voiddelete_case(Node*p){if(p->parent==NULL){p->color=BLACK;return;}if(p->sibling()->color==RED){p->parent->color=RED;p->sibling()->color=BLACK;if(p==p->parent->leftTree)//rotate_left(p->sibling());rotate_left(p->parent);else//rotate_right(p->sibling());rotate_right(p->parent);}if(p->parent->color==BLACK&&p->sibling()->color==BLACK&&p->sibling()->leftTree->color==BLACK&&p->sibling()->rightTree->color==BLACK){p->sibling()->color=RED;delete_case(p->parent);}elseif(p->parent->color==RED&&p->sibling()->color==BLACK&&p->sibling()->leftTree->color==BLACK&&p->sibling()->rightTree->color==BLACK){p->sibling()->color=RED;p->parent->color=BLACK;}else{if(p->sibling()->color==BLACK){if(p==p->parent->leftTree&&p->sibling()->leftTree->color==RED&&p->sibling()->rightTree->color==BLACK){p->sibling()->color=RED;p->sibling()->leftTree->color=BLACK;rotate_right(p->sibling()->leftTree);}elseif(p==p->parent->rightTree&&p->sibling()->leftTree->color==BLACK&&p->sibling()->rightTree->color==RED){p->sibling()->color=RED;p->sibling()->rightTree->color=BLACK;rotate_left(p->sibling()->rightTree);}}p->sibling()->color=p->parent->color;p->parent->color=BLACK;if(p==p->parent->leftTree){p->sibling()->rightTree->color=BLACK;rotate_left(p->sibling());}else{p->sibling()->leftTree->color=BLACK;rotate_right(p->sibling());}}}voidinsert(Node*p,intdata){if(p->value>=data){if(p->leftTree!=NIL)insert(p->leftTree,data);else{Node*tmp=newNode();tmp->value=data;tmp->leftTree=tmp->rightTree=NIL;tmp->parent=p;p->leftTree=tmp;insert_case(tmp);}}else{if(p->rightTree!=NIL)insert(p->rightTree,data);else{Node*tmp=newNode();tmp->value=data;tmp->leftTree=tmp->rightTree=NIL;tmp->parent=p;p->rightTree=tmp;insert_case(tmp);}}}voidinsert_case(Node*p){if(p->parent==NULL){root=p;p->color=BLACK;return;}if(p->parent->color==RED){if(p->uncle()->color==RED){p->parent->color=p->uncle()->color=BLACK;p->grandparent()->color=RED;insert_case(p->grandparent());}else{if(p->parent->rightTree==p&&p->grandparent()->leftTree==p->parent){rotate_left(p);p->color=BLACK;p->parent->color=RED;rotate_right(p);}elseif(p->parent->leftTree==p&&p->grandparent()->rightTree==p->parent){rotate_right(p);p->color=BLACK;p->parent->color=RED;rotate_left(p);}elseif(p->parent->leftTree==p&&p->grandparent()->leftTree==p->parent){p->parent->color=BLACK;p->grandparent()->color=RED;rotate_right(p->parent);}elseif(p->parent->rightTree==p&&p->grandparent()->rightTree==p->parent){p->parent->color=BLACK;p->grandparent()->color=RED;rotate_left(p->parent);}}}}voidDeleteTree(Node*p){if(!p||p==NIL){return;}DeleteTree(p->leftTree);DeleteTree(p->rightTree);deletep;}public:bst(){NIL=newNode();NIL->color=BLACK;root=NULL;}~bst(){if(root)DeleteTree(root);deleteNIL;}voidinorder(){if(root==NULL)return;inorder(root);cout<<endl;}voidinsert(intx){if(root==NULL){root=newNode();root->color=BLACK;root->leftTree=root->rightTree=NIL;root->value=x;}else{insert(root,x);}}booldelete_value(intdata){returndelete_child(root,data);}private:Node*root,*NIL;};