void rb_insertFixup(rbt *t, rbnode *n) { rbnode *y = NULL; while (n->parent != NULL && n->parent->color == RED) { if (n->parent == getGrandParent(n)->link[0]) { y = getUncle(n); if (y != NULL && y->color == RED) { n->parent->color = BLACK; y->color = BLACK; n = getGrandParent(n); if(n != NULL) n->color = RED; } else { if (n == n->parent->link[1]) { n = n->parent; leftRotate(t, n); } n->parent->color = BLACK; getGrandParent(n)->color = RED; rightRotate(t, getGrandParent(n)); } } else { y = getUncle(n); if (y != NULL && y->color == RED) { n->parent->color = BLACK; y->color = BLACK; n = getGrandParent(n); if(n != NULL) n->color = RED; } else { if (n == n->parent->link[0]) { n = n->parent; rightRotate(t, n); } n->parent->color = BLACK; getGrandParent(n)->color = RED; leftRotate(t, n->parent->parent); } } } // save us from a red violation at the root (t->root)->color = BLACK; }
// This function brings the key at root if key is present in tree. // If key is not present, then it brings the last accessed item at // root. This function modifies the tree and returns the new root struct node *splay(struct node *root, int key) { // Base cases: root is NULL or key is present at root if (root == NULL || root->key == key) return root; // Key lies in left subtree if (root->key > key) { // Key is not in tree, we are done if (root->left == NULL) return root; // Zig-Zig (Left Left) if (root->left->key > key) { // First recursively bring the key as root of left-left root->left->left = splay(root->left->left, key); // Do first rotation for root, second rotation is done after else root = rightRotate(root); } else if (root->left->key < key) // Zig-Zag (Left Right) { // First recursively bring the key as root of left-right root->left->right = splay(root->left->right, key); // Do first rotation for root->left if (root->left->right != NULL) root->left = leftRotate(root->left); } // Do second rotation for root return (root->left == NULL)? root: rightRotate(root); } else // Key lies in right subtree { // Key is not in tree, we are done if (root->right == NULL) return root; // Zag-Zig (Right Left) if (root->right->key > key) { // Bring the key as root of right-left root->right->left = splay(root->right->left, key); // Do first rotation for root->right if (root->right->left != NULL) root->right = rightRotate(root->right); } else if (root->right->key < key)// Zag-Zag (Right Right) { // Bring the key as root of right-right and do first rotation root->right->right = splay(root->right->right, key); root = leftRotate(root); } // Do second rotation for root return (root->right == NULL)? root: leftRotate(root); } }
entry *insert(char lastName[],entry *e){ // 1. Perform the normal BST rotation if(e==NULL) return(newnode(lastName)); if(strcasecmp(lastName,e->lastName) < 0) e->pL = insert(lastName,e->pL); else e->pR = insert(lastName,e->pR); // 2. Get height of this ancestor node e->level = max(height(e->pL), height(e->pR)) + 1; /* 3. Get the balance factor of this ancestor node to check whether * this node became unbalanced */ int balance = getBalance(e); // If this node becomes unbalanced, then there are 4 cases // Left Left Case (LL case) if(balance > 1 && (strcasecmp(lastName,e->pL->lastName) < 0)) return rightRotate(e); // Right Right Case if (balance < -1 && (strcasecmp(lastName,e->pR->lastName) > 0 )) return leftRotate(e); // Left Right Case if(balance > 1 && (strcasecmp(lastName,e->pL->lastName) > 0)) { e->pL = leftRotate(e->pL); return rightRotate(e); } // Right Left Case if (balance < -1 && (strcasecmp(lastName,e->pR->lastName) > 0 )) { e->pR = rightRotate(e->pR); return leftRotate(e); } /* return the (unchanged) node pointer */ return e; }
void splay(SplayTree newRoot, SplayTree tree){ tree_node *p; while(newRoot->father != NULL){ // newRoot is already root p = newRoot->father; if(p->father == NULL){ if(newRoot == p->left) rightRotate(newRoot);//Zig else leftRotate(newRoot);//Zag break; //We are in the root position } if(newRoot == p->left){ //newRoot is the left child if(p == p->father->left){ // p is the left child rightRotate(p); //Zig-Zig rightRotate(newRoot); } else{ //Zig - Zag Situation rightRotate(newRoot); leftRotate(newRoot); } } else{ // newRoot is the right child if(p == p->father->right){ // p is the right child leftRotate(p); // Zag-Zag Situation leftRotate(newRoot); } else{ leftRotate(newRoot); //Zag-Zig Situation rightRotate(newRoot); } } } tree = newRoot; //Resign the new root }
struct node* insert(struct node* node, int key){ if (!node) return (newNode(key)); if (key < node->key) { node->left = insert(node->left, key); }else{ node->right = insert(node->right, key); } node->height = max(height(node->left), height(node->right)) + 1; int balance = getBalance(node); if (balance > 1 && key < node->left->key) { return rightRotate(node); } if (balance < -1 && key > node->right->key) { return leftRotate(node); } if (balance > 1 && key > node->left->key) { node->left = leftRotate(node->left); return rightRotate(node); } if (balance < -1 && key < node->right->key) { node->right = rightRotate(node->right); return leftRotate(node); } return node; }
struct node *avlInsert(struct node *root,int data) { int balance; if(root==NULL) return(newNode(data)); else{ if(data <= root->data) root->left=avlInsert(root->left,data); else root->right=avlInsert(root->right,data); } balance=height(root->left)-height(root->right); if(balance>1){ if(height(root->left->left) >= height(root->left->right)){ return rightRotate(root); }else{ root->left =leftRotate(root->left); return rightRotate(root); } }else if(balance<-1){ if(height(root->right->right) >= height(root->right->left)){ return leftRotate(root); }else{ root->right=rightRotate(root->right); return leftRotate(root); } } root->height=1+max(root->left,root->right); return root; }
//insert 함수에서 z노드는 red로 칠해진 상태로 넘어온다. //이 때, z->parent->color 가 또 red 라면, RBT의 4번 조건 위반 //위반하는 경우 계속 반복 ==> while(z->parent->color == COLOR_RED) void RB_insert_fixUp(RBT* T, node* z) { node* y; while (z->parent->color == COLOR_RED) { if (z->parent == z->parent->parent->left) { y = z->parent->parent->right; //case 1 if (y->color == COLOR_RED) { z->parent->color = COLOR_BLACK; y->color = COLOR_BLACK; z->parent->parent->color = COLOR_RED; z = z->parent->parent; } else { //case 2 if (z == z->parent->right) { z = z->parent; leftRotate(T, z); } //case 3 z->parent->color = COLOR_BLACK; z->parent->parent->color = COLOR_RED; rightRotate(T, z->parent->parent); } } //same as then clause with "right" and "left" exchanged. else { y = z->parent->parent->left; //case 1 if (y->color == COLOR_RED) { z->parent->color = COLOR_BLACK; y->color = COLOR_BLACK; z->parent->parent->color = COLOR_RED; z = z->parent->parent; } else { //case 2 if (z == z->parent->left) { z = z->parent; rightRotate(T, z); } //case 3 z->parent->color = COLOR_BLACK; z->parent->parent->color = COLOR_RED; leftRotate(T, z->parent->parent); } } } T->root->color = COLOR_BLACK; }
void RBTree :: deleteFixup( NodePtr x ) { NodePtr w ; // w is x's sibling while ( ( x != root ) && ( x->color == 'B' ) ) { if ( x == x->p->left ) { // x is a left child w = x->p->right; if(w->color == 'R'){ w->color = 'B'; x->p->color = 'R'; leftRotate(x->p); w = x->p->right; } if(w->left->color == 'B' && w->right->color == 'B'){ w->color = 'R'; x = x->p; } else { if(w->right->color == 'B'){ w->left->color = 'B'; w->color = 'R'; rightRotate(w); w = x->p->right; } w->color = x->p->color; x->p->color = 'B'; w->right->color = 'B'; leftRotate(x->p); x = root; } } else { w = x->p->left; if(w->color == 'R'){ w->color = 'B'; x->p->color = 'R'; rightRotate(x->p); w = x->p->left; } if(w->right->color == 'B' && w->left->color == 'B'){ w->color = 'R'; x = x->p; } else { if(w->left->color == 'B'){ w->right->color = 'B'; w->color = 'R'; leftRotate(w); w = x->p->left; } w->color = x->p->color; x->p->color = 'B'; w->left->color = 'B'; rightRotate(x->p); x = root; } } } x->color = 'B' ; }
int fixup( Node *z ) { Node *y; while ( z->parent->color == 'R' ) { if ( z->parent == z->parent->parent->left ) { y = z->parent->parent->right; if ( y->color == 'R' ) { z->parent->color = 'B'; y->color = 'B'; z->parent->parent->color = 'R'; z = z->parent->parent; } else { if ( z == z->parent->right ) { z = z->parent; leftRotate( z ); } z->parent->color = 'B'; z->parent->parent->color = 'R'; rightRotate( z->parent->parent ); } } else { y = z->parent->parent->left; if ( y->color == 'R' ) { z->parent->color = 'B'; y->color = 'B'; z->parent->parent->color = 'R'; z = z->parent->parent; } else { if ( z == z->parent->left ) { z = z->parent; rightRotate( z ); } z->parent->color = 'B'; z->parent->parent->color = 'R'; leftRotate( z->parent->parent ); } } } Root->color = 'B'; return 0; }
void RepairTree(node **root,node *child,node *nil){ node *parent, *grandParent, *uncle; //printf("child = %ld , parent color = %d",child->data,child->parent->black); while(!(child->parent->black)){ // printf("child = %ld, parent = %ld \n",child->data,child->parent->data); parent = child->parent; grandParent = parent->parent; if(grandParent->left == parent){ //left left uncle = grandParent->right; if(!(uncle->black)){ //case 1 uncle is red parent->black = 1; uncle->black = 1; grandParent->black = 0; child = grandParent; } else{ if(child == parent->right){ // case 2 uncle is black if child lies right to parent convert it to case 3 child = parent; leftRotate(root,child,nil); parent = child->parent; grandParent = parent->parent; } parent->black = 1; //case 3 uncle is black grandParent->black = 0; rightRotate(root,grandParent,nil); break; } } else{ // right right uncle = grandParent->left; if(!(uncle->black)){ //case 1 uncle is red parent->black = 1; uncle->black = 1; grandParent->black = 0; child = grandParent; } else { if(child == parent->left){ // case 2 and case 3 uncle is black child = parent; rightRotate(root,child,nil); parent = child->parent; grandParent = parent->parent; } parent->black = 1; grandParent->black =0; leftRotate(root,grandParent,nil); } } } (*root)->black = 1; }
void SplayTree::zigZag(TreeNode* node, int mode) { if (mode == ZIG_LEFT) { rightRotate(node->parent); leftRotate(node->parent); } else { leftRotate(node->parent); rightRotate(node->parent); } }
void tree::insertFixUp(node* n) { node* z=n; while(red==z->parentNode()->nodeColor()) { if (z->parentNode()==z->parentNode()->parentNode()->leftNode()) { node* y=z->parentNode()->parentNode()->rightNode(); if (red==y->nodeColor()) { z->parentNode()->setNodeColor(black); y->setNodeColor(black); z->parentNode()->parentNode()->setNodeColor(red); z=z->parentNode()->parentNode(); } else { if (z->parentNode()->rightNode()==z) { z=z->parentNode(); leftRotate(z); } z->parentNode()->setNodeColor(black); z->parentNode()->parentNode()->setNodeColor(red); rightRotate(z->parentNode()->parentNode()); } } else if (z->parentNode()==z->parentNode()->parentNode()->rightNode()) { node* y=z->parentNode()->parentNode()->leftNode(); if (red==y->nodeColor()) { z->parentNode()->setNodeColor(black); y->setNodeColor(black); z->parentNode()->parentNode()->setNodeColor(red); z=z->parentNode()->parentNode(); } else { if (z==z->parentNode()->leftNode()) { z=z->parentNode(); rightRotate(z); } z->parentNode()->setNodeColor(black); z->parentNode()->parentNode()->setNodeColor(red); leftRotate(z->parentNode()->parentNode()); } } else { assert(false); } } m_root->setNodeColor(black); }
void rbDeleteFixup(root_ptr ts_root, tree_ptr node_x) { while (node_x != ts_root->root && BLACK == node_x->color) { if (node_x == node_x->parent->left) { tree_ptr node_w = node_x->parent->right; if (RED == node_w->color) { node_w->color = BLACK; node_x->parent->color = RED; leftRotate(ts_root, node_x->parent); node_w = node_x->parent->right; } else if (BLACK == node_w->left->color && BLACK == node_w->right->color) { node_w->color = RED; node_x = node_x->parent; } else if (BLACK == node_w->right->color) { node_w->left->color = BLACK; node_w->color = RED; rightRotate(ts_root, node_w); node_w = node_x->parent->right; } else { node_w->color = node_x->parent->color; node_x->parent->color = BLACK; node_w->right->color = BLACK; leftRotate(ts_root, node_x->parent); node_x = ts_root->root; } } else if (node_x == node_x->parent->right) { tree_ptr node_w = node_x->parent->left; if (RED == node_w->color) { node_w->color = BLACK; node_x->parent->color = RED; leftRotate(ts_root, node_x->parent); node_w = node_x->parent->right; } else if (BLACK == node_w->left->color && BLACK == node_w->right->color) { node_w->color = RED; node_x = node_x->parent; } else if (BLACK == node_w->right->color) { node_w->left->color = BLACK; node_w->color = RED; rightRotate(ts_root, node_w); node_w = node_x->parent->right; } else { node_w->color = node_x->parent->color; node_x->parent->color = BLACK; node_w->right->color = BLACK; leftRotate(ts_root, node_x->parent); node_x = ts_root->root; } } } node_x->color = BLACK; }
void RBInsertFixup(Tree *tree, Node *z) { while (z->parent->color == RED) { if (z->parent == z->parent->parent->left) { Node *y = z->parent->parent->right; if (y->color == RED) { // 情况1 z->parent->color = BLACK; y->color = BLACK; z->parent->parent->color = RED; z = z->parent->parent; } else { if (z == z->parent->right) { // 情况2 z = z->parent; leftRotate(tree, z); } // 情况3 z->parent->color = BLACK; z->parent->parent->color = RED; rightRotate(tree, z->parent->parent); } } else if (z->parent == z->parent->parent->right) { // 与上面对称 Node *y = z->parent->parent->left; if (y->color == RED) { z->parent->color = BLACK; y->color = BLACK; z->parent->parent->color = RED; z = z->parent->parent; } else { if (z == z->parent->left) { z = z->parent; rightRotate(tree, z); } z->parent->color = BLACK; z->parent->parent->color = RED; leftRotate(tree, z->parent->parent); } } } tree->root->color = BLACK; }
/* * Arruma a coloração da arvore */ void rn_insere_fixup (arv *t, no *z) { no *y; // if (z != t -> raiz) //Se o no for raiz, so pinta raiz de preto // { while (z != t -> raiz && z -> pai -> cor == RED) //Enquanto a cor do pai de z for vermelha { if (z -> pai == z -> pai -> pai -> esq) //CASO 1: Se o pai de z eh filho a esquerda de seu avo { y = z -> pai -> pai -> dir; //y aponta para o tio de z if (y != t -> nil && y -> cor == RED) //Se o tio for vermelho { z -> pai -> cor = BLACK; //Pinta o pai de z de preto y -> cor = BLACK; //Pinta z de preto z -> pai -> pai -> cor = RED; //Pinta o avo de z de vermelho z = z -> pai -> pai; //z aponta para o avo } else { if (z == z -> pai -> dir) //CASO 2: Se z eh filho a direita { z = z -> pai; //z aponta para seu pai leftRotate (t, z); //Faz rotacao a esquerda no pai de z } z -> pai -> cor = BLACK; //CASO 3: Pinta o pai de z de preto z -> pai -> pai -> cor = RED; //Pinta o avo de z de vermelho rightRotate (t, z -> pai -> pai); //Faz rotacao a direita no avo de z } } else { y = z -> pai -> pai -> esq; //y aponta para o tio de z if (y != t -> nil && y -> cor == RED) //Se o tio for vermelho { z -> pai -> cor = BLACK; //Pinta o pai de z de preto y -> cor = BLACK; //Pinta z de preto z -> pai -> pai -> cor = RED; //Pinta o avo de z de vermelho z = z -> pai -> pai; //z aponta para o avo } else { if (z == z -> pai -> esq) //CASO 2: Se z eh filho a direita { z = z -> pai; //z aponta para seu pai rightRotate (t, z); //Faz rotacao a esquerda no pai de z } z -> pai -> cor = BLACK; //CASO 3: Pinta o pai de z de preto z -> pai -> pai -> cor = RED; //Pinta o avo de z de vermelho leftRotate (t, z -> pai -> pai); //Faz rotacao a direita no avo de z } } } // } t -> raiz -> cor = BLACK; //Pinta raiz de preto }
static void RBTreeDeleteFixup(struct rb_tree *rb, struct rb_node *x) { struct rb_node *sibling; while (x != rb->root && x->color == BLACK) { if (x == x->p->left) { sibling = x->p->right; if (sibling->color == RED) { sibling->color = BLACK; x->p->color = RED; leftRotate(rb, x->p); sibling = x->p->right; } if (sibling->left->color == BLACK && sibling->right->color == BLACK) { sibling->color = RED; x = x->p; } else { if (sibling->right->color == BLACK) { sibling->color = RED; sibling->left->color = BLACK; rightRotate(rb, sibling); sibling = x->p->right; } sibling->color = x->p->color; x->p->color = BLACK; sibling->right->color = BLACK; leftRotate(rb, x->p); x = rb->root; } } else { sibling = x->p->left; if (sibling->color == RED) { sibling->color = BLACK; x->p->color = RED; rightRotate(rb, x->p); sibling = x->p->left; } if (sibling->left->color == BLACK && sibling->right->color == BLACK) { sibling->color = RED; x = x->p; } else { if (sibling->left->color == BLACK) { sibling->right->color = BLACK; sibling->color = RED; leftRotate(rb, sibling); sibling = x->p->left; } sibling->color = x->p->color; x->p->color = BLACK; sibling->left->color = BLACK; rightRotate(rb, x->p); x = rb->root; } } } x->color = BLACK; }
void splayCreate( SplayTree *x) { while ( x->parent != NULL ) //While we are not at the root { //CASE 1! ZIG STEP contorSplay = contorSplay + 3; // antet if: 2 pointeri si o comparatie if ( x->parent->parent == NULL ) // The zig step. if x is the child of the root we have 2 cases: { contorSplay = contorSplay + 3 + 1; // 3 pentru if si 1 pentru apelarea functiei if ( x->parent->left == x ) // a. if x is the left child of the root rightRotate(x->parent); // we rotate to the right else if ( x->parent->right == x ) // b. if x is the right child of the root leftRotate(x->parent); // we rotate to the left } //CASE 2! ZIG-ZIG STEP else if ( x->parent->left == x && x->parent->parent->left == x->parent ) // 1. if x is the left child of a left child { contorSplay = contorSplay + 2; //apelarea functiilor rightRotate(x->parent->parent); // We rotate first through the grand-parrent. rightRotate(x->parent); // Then we rotate though the partent } else if ( x->parent->right == x && x->parent->parent->right == x->parent ) // 2. if x is the right child of a right child { contorSplay = contorSplay + 2; //apelarea functiilor leftRotate(x->parent->parent); // We rotate first through the grand-parrent. leftRotate(x->parent); // Then we rotate though the partent } //CASE 3! ZIG-ZAG STEP else if ( x->parent->left == x && x->parent->parent->right == x->parent ) // 1. if x is the left child of a right child { contorSplay = contorSplay + 2; //apelarea functiilor rightRotate(x->parent); // We rotate to the right leftRotate(x->parent); // Then we rotate to the left } else if ( x->parent->right == x && x->parent->parent->left == x->parent ) // 2. if x is the right child of a left child { contorSplay = contorSplay + 2; //apelarea functiilor leftRotate(x->parent); // We rotate to the left rightRotate(x->parent); // Then we rotate to the right } //PENTRU A NU STRICA CUM ARATA CODUL, AM ALES SA FAC URMATORUL ARTIFICU: //Calculam cate operatii are fiecare antet ar if-urilor care verifica cazurile 1, 2 si 3 => fiecare if are 9 operatii elementare => //contorSplay = contorSplay + 9; //Acum presupunem ca numarul de accesari, la o signura rulare, al fiecarui if este de jumatare de ori din totalul lor: accesari = #if/2 => accesari = 5/2 = 2.5 //Acum impartim operatii simple la accesari => 9/2.5 = 3.6. facem partea intreaga superioara si vom avea: contorSplay = contorSplay + 4 contorSplay = contorSplay + 4; } }
// Balanceamento para inserção de uma árvore rubro negra void balvp(arvore *A, arvore *no){ arvore *pai = no->pai,*tio,*avo; while(pai->cor==0){ avo=pai->pai; // Esquerda do avô if(no->raiz < avo->raiz){ tio=avo->dir; // Caso 1 if(tio->cor==0){ pai->cor=1; tio->cor=1; avo->cor=0; no=avo; pai=no->pai; }else{ // Caso 2 if(no == pai->dir){ leftRotate(A,pai); no=pai; pai=no->pai; } // Caso 3 pai->cor=1; avo->cor=0; rightRotate(A,avo); } // Direita do avô }else{ tio=avo->esq; // Caso 1 if(tio->cor==0){ pai->cor=1; tio->cor=1; avo->cor=0; no=avo; pai=no->pai; }else{ // Caso 2 if(no==pai->esq){ rightRotate(A,pai); no=pai; pai=no->pai; } // Caso 3 pai->cor=1; avo->cor=0; leftRotate(A,avo); } } } // Raiz de preto A->dir->cor=1; }
AVLTreeNode* insert(AVLTreeNode* node, int val) { /* 1. Perform the normal BST rotation */ if(node == NULL) return (new AVLTreeNode(val)); // If val already exists in BST, icnrement count and return if(val == node->val) { (node->count)++; return node; } /* Otherwise, recur down the tree */ if(val < node->val) { node->left = insert(node->left, val); node->leftSize++; } else { node->right = insert(node->right, val); } /* 2. Update height of this ancestor node */ node->height = max(height(node->left), height(node->right)) + 1; /* 3. Get the balance factor of this ancestor node to check whether this node became unbalanced */ int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases // Left Left Case if(balance > 1 && val < node->left->val) { return rightRotate(node); } // Right Right Case if(balance < -1 && val > node->right->val) { return leftRotate(node); } // Left Right Case if(balance > 1 && val > node->left->val) { node->left = leftRotate(node->left); return rightRotate(node); } // Right Left Case if(balance < -1 && val < node->right->val) { node->right = rightRotate(node->right); return leftRotate(node); } /* return the (unchanged) node pointer */ return node; }
//插入操作保持红黑性质子程序 void RBTree::insertFixup(Node* z) { Node* y; while (z->getParent()->getColour() == RED) { if (z->getParent() == z->getParent()->getParent()->getLeft()) { y = z->getParent()->getParent()->getRight(); if (y->getColour() == RED) { z->getParent()->setColour(BLACK); y->setColour(BLACK); z->getParent()->getParent()->setColour(RED); z = z->getParent()->getParent(); } else { if (z == z->getParent()->getRight()) { z = z->getParent(); leftRotate(z); } z->getParent()->setColour(BLACK); z->getParent()->getParent()->setColour(RED); rightRotate(z->getParent()->getParent()); } } else { y = z->getParent()->getParent()->getLeft(); if (y->getColour() == RED) { z->getParent()->setColour(BLACK); y->setColour(BLACK); z->getParent()->getParent()->setColour(RED); z = z->getParent()->getParent(); } else { if (z == z->getParent()->getLeft()) { z = z->getParent(); rightRotate(z); } z->getParent()->setColour(BLACK); z->getParent()->getParent()->setColour(RED); leftRotate(z->getParent()->getParent()); } } } Treepoint->setColour(BLACK); }
// Inserts a new key to the tree rotted with node. Also, updates *count // to contain count of smaller elements for the new key struct node* insert(struct node* node, int key, int *count) { /* 1. Perform the normal BST rotation */ if (node == NULL) return(newNode(key)); if (key < node->key) node->left = insert(node->left, key, count); else { node->right = insert(node->right, key, count); // UPDATE COUNT OF SMALLER ELEMENTS FOR KEY *count = *count + size(node->left) + 1; } /* 2. Update height and size of this ancestor node */ node->height = max(height(node->left), height(node->right)) + 1; node->size = size(node->left) + size(node->right) + 1; /* 3. Get the balance factor of this ancestor node to check whether this node became unbalanced */ int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases // Left Left Case if (balance > 1 && key < node->left->key) return rightRotate(node); // Right Right Case if (balance < -1 && key > node->right->key) return leftRotate(node); // Left Right Case if (balance > 1 && key > node->left->key) { node->left = leftRotate(node->left); return rightRotate(node); } // Right Left Case if (balance < -1 && key < node->right->key) { node->right = rightRotate(node->right); return leftRotate(node); } /* return the (unchanged) node pointer */ return node; }
//return root Node* deleteNode(Node *node, int key){ if(node == NULL) return NULL; if(key < node->key){ node->left = deleteNode(node->left, key); } else if(key > node->key){ node->right = deleteNode(node->right, key); } else{ if(node->left == NULL && node->right == NULL){ free(node); return NULL; } if(node->left == NULL || node->right == NULL){ Node *temp = node->right ? node->right : node->left; *node = *temp; free(temp); } else { Node *temp = findMinNode(node->right); node->key = temp->key; node->right = deleteNode(node->right, temp->key); } } node->h = max(h(node->left), h(node->right))+1; int balance = getBalance(node); //left left if(balance > 1 && getBalance(node->left) >= 0){ return rightRotate(node); } //left right if(balance > 1 && getBalance(node->left) < 0){ node->left = leftRotate(node->left); return rightRotate(node); } //right right if(balance < -1 && getBalance(node->right) <= 0){ return leftRotate(node); } //right left if(balance < -1 && getBalance(node->right) > 0){ node->right = rightRotate(node->right); return leftRotate(node); } return node; }
struct Node* insert(struct Node* node, int key, int seg_id) { /* 1. Perform the normal BST rotation */ if (node == NULL) return(newNode(key, seg_id)); if (key < node->key) node->left = insert(node->left, key, seg_id); else if (key > node->key) node->right = insert(node->right, key, seg_id); else // Equal keys not allowed return node; /* 2. Update height of this ancestor node */ node->height = 1 + mymax(height(node->left), height(node->right)); /* 3. Get the balance factor of this ancestor node to check whether this node became unbalanced */ int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases // Left Left Case if (balance > 1 && key < node->left->key) return rightRotate(node); // Right Right Case if (balance < -1 && key > node->right->key) return leftRotate(node); // Left Right Case if (balance > 1 && key > node->left->key) { node->left = leftRotate(node->left); return rightRotate(node); } // Right Left Case if (balance < -1 && key < node->right->key) { node->right = rightRotate(node->right); return leftRotate(node); } /* return the (unchanged) node pointer */ return node; }
struct node_bin* insert(struct node_bin* node_bin, int key) { if (node_bin == NULL) return(newnode_bin(key)); if (key < node_bin->key) node_bin->left = insert(node_bin->left, key); else node_bin->right = insert(node_bin->right, key); // Innoim inaltimele la nodurile predecesori node_bin->height = max(height(node_bin->left), height(node_bin->right)) + 1; // Controlam balansarea int balance = getBalance(node_bin); // In caz ca nu este balansat : // cazul STINGA STINGA if (balance > 1 && key < node_bin->left->key) return rightRotate(node_bin); // cazul DREAPTA DREAPTA if (balance < -1 && key > node_bin->right->key) { return leftRotate(node_bin); } // Cazul STINGA DREAPTA if (balance > 1 && key > node_bin->left->key) { node_bin->left = leftRotate(node_bin->left); return rightRotate(node_bin); } // Cazul DREAPTA STINGA if (balance < -1 && key < node_bin->right->key) { node_bin->right = rightRotate(node_bin->right); return leftRotate(node_bin); } return node_bin; }
//Function for handle case3 void SiblingIsRed(Node **rootPtr){ Node *root = *rootPtr; char colour = root->color; // store the original root color if( root->right ){ leftRotate(&(*rootPtr)); (*rootPtr)->left->color = 'r'; if((*rootPtr)->left && ((*rootPtr)->left->right) && ((*rootPtr)->left->right->right) ){ NephewIsRedSiblingIsBlack(&(*rootPtr)->left); // printf("leftSide, enter case1\n"); } else{ NephewAndSiblingIsBlack(&(*rootPtr)->left); // printf("leftSide, enter case2\n"); } } else if( root->left ){ rightRotate(&(*rootPtr)); (*rootPtr)->right->color = 'r'; if((*rootPtr)->right && ((*rootPtr)->right->left) && ((*rootPtr)->right->left->left)){ NephewIsRedSiblingIsBlack(&(*rootPtr)->right); // printf("RightSide, enter case1\n"); } else{ NephewAndSiblingIsBlack(&(*rootPtr)->right); // printf("RightSide, enter case2\n"); } } (*rootPtr)->color = colour; }
void AvlTree::doubleRightLeftRotate (AvlNode * & v) { std::cout << "perform doubleRightLeftRotation" << std::endl; rightRotate(v->right); leftRotate(v); return; }
void SplayTree::zig(TreeNode* node) { if (node->parent->left == node) { rightRotate(node->parent); } else { leftRotate(node->parent); } }
node * insert(node * node,int val) { /* 1. Perform the normal BST rotation */ if (node == NULL) return(newNode(val)); if (val < node->val) node->left = insert(node->left, val); else node->right = insert(node->right, val); /* 2. Update height of this ancestor node */ node->ht = max(ht(node->left), ht(node->right)) + 1; /* 3. Get the balance factor of this ancestor node to check whether this node became unbalanced */ int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases // Left Left Case if (balance > 1 && val < node->left->val) return rightRotate(node); // Right Right Case if (balance < -1 && val > node->right->val) return leftRotate(node); // Left Right Case: left ---right if (balance > 1 && val > node->left->val) { node->left = leftRotate(node->left); return rightRotate(node); } // Right Left Case if (balance < -1 && val < node->right->val) { node->right = rightRotate(node->right); return leftRotate(node); } /* return the (unchanged) node pointer */ return node; }
void rightLeftRotate(Node **rootPtr){ Node *node1, *node2; node1 = *rootPtr; node2 = node1->right; rightRotate(&node2); (*rootPtr)->right = node2; leftRotate(&(*rootPtr)); }
void leftRightRotate(Node **rootPtr){ Node *node1, *node2; node1 = *rootPtr; node2 = node1->left; leftRotate(&node2); (*rootPtr)->left = node2; rightRotate(&(*rootPtr)); }