void RBTree<T>::DeleteFixup(T* fixNode) { while( fixNode != root && fixNode->color == BLACK ) { bool sideFixNode = 1; EventNode* parent = fixNode->parent; EventNode* brother = sideFixNode? parent->right : parent->left; if (brother->color == RED) { //brother is red is case 1 brother->color = BLACK; parent->color = RED; if (sideFixNode == 1) { LeftRotation(parent); brother = parent->right; } else { RightRotation(parent); brother = parent->left; } } if ((!brother->left || brother->left->color_ == BLACK) && (!brother->right || brother->right->color_ == BLACK)) { //case 2 brother->color = RED; fixNode = parent; } else { if (sideFixNode && (!brother->right || brother->right->color == BLACK)) { // case 3 fixNode is leftson of parent brother->left->color = BLACK; brother->color = RED; RightRotation(brother); brother = parent->right; } if (sideFixNode && ( !brother->left || brother->left->color_ == BLACK)) { //case 3 fixNode is rightson of parent brother->right->color = BLACK; brother->color = RED; LeftRotation(brother); brother = parent->left; } brother->color = parent->color_; // case 4 parent->color = BLACK; if (sideFixNode) { brother->right->color_ = BLACK; LeftRotation(parent); } else { brother->left->color = BLACK; RightRotation(parent); } fixNode = root; } } fixNode->color = BLACK; }
void RBTree<T>::InsertFixup(T *fixNode) { T *parent = fixNode->parent; T *uncle = NULL; while ( parent && parent->color == RED) { if ( !parent->parent ) break; // if grandparent isnot exit break; bool sideParent = 1; //default parent is leftson of grandparent if (parent == parent->parent->right) sideParent = 0; uncle = (sideParent) ? parent->parent->right : parent->parent->left; if (uncle && uncle->color == RED) { //case 1 parent->parent->color = RED; parent->color = BLACK; uncle->color = BLACK; fixNode = parent->parent; } else { if (sideParent == 1 && fixNode == parent->right) {//case 2 fixNode = parent; LeftRotation(fixNode); } else if( sideParent == 0 && fixNode == parent->left) { fixNode = parent; RightRotation(fixNode); } fixNode->parent->color = BLACK; fixNode->parent->parent->color = RED; if (sideParent == 1) RightRotation(fixNode->parent->parent); else LeftRotation(fixNode->parent->parent); } parent = fixNode->parent; } root->color = BLACK; }
NodeT* Insert(NodeT* Node, int data) { int b,r,l; if(Node==NULL) return createNode(data); if(data<Node->data) Node->left=Insert(Node->left,data); else Node->right=Insert(Node->right, data); l=NodeHeight(Node->left); r=NodeHeight(Node->right); Node->height=Max(l,r); b=BalanceFactor(Node); if(b<-1 && data>Node->right->data) return LeftRotation(Node); if(b>1 && data<Node->left->data) return RightRotation(Node); if(b<-1 && data<Node->right->data) { Node->right=RightRotation(Node->right); return LeftRotation(Node); } if(b>1 && data>Node->left->data) { Node->left=LeftRotation(Node->left); return RightRotation(Node); } return Node; }
void AVLinsert (BST &T,records R,int &unblanced) { if(!T) { unblanced=1; T=new node; T->data=R; T->lchild=T->rchild=NULL; T->bf=0; } else if(R.key<T->data.key) { AVLinsert(T->lchild,R,unblanced); if(unblanced) switch(T->bf) { case -1:T->bf=0; unblanced=0; break; case 0:T->bf=1; break; case 1:LeftRotation(T,unblanced); default:break; } } else if(R.key>T->data.key) { AVLinsert(T->rchild,R,unblanced); if(unblanced) switch(T->bf) { case -1: RightRotation(T,unblanced); break; case 0:T->bf=-1; break; case 1: T->bf=0; unblanced=0; break; default:break; } } else unblanced=0; }