node* balanceNode(node* nod) { node* newroot = NULL; int bf = 0; if(nod->left) nod->left = balanceNode(nod->left); if(nod->right) nod->right= balanceNode(nod->right); bf = balanceFactor(nod); if(bf >= 2) { if(balanceFactor(nod->left) <= -1 ) newroot = rotateLR(nod); else newroot = rotateLL(nod); }else if(bf <= -2) { if(balanceFactor(nod->right)<= 1) newroot = rotateRL(nod); else newroot = rotateRR(nod); }else newroot = nod; return(newroot); }
void TreeDB::balance(TreeNode*& root) { fixHeight(root); //by convention, we are right heavy if(balanceFactor(root) == 2) { //if we have a zig-zag form, we must rotate right then left if(balanceFactor(root->right) < 0) { cout << "rebalancing..." << endl; rotateRight(root->right); } //if we have a linked list form, we only need to rotate left, //if we had a zig zag form, we already rotated right, now left cout << "rebalancing..." << endl; rotateLeft(root); } else if(balanceFactor(root) == -2) //left heavy { //if we have a zig-zag form, we must rotate left then right if(balanceFactor(root->left) > 0) { cout << "rebalancing..." << endl; rotateLeft(root->left); } //if we have a linked list form, we only need to rotate right, //if we had a zig zag form, we already rotated left, now right cout << "rebalancing..." << endl; rotateRight(root); } }
void AvlTree::rebalance(int node) { for(int n = node; n != NIL; ) { const int p = parentNode(n); updateAggregates(n); switch(balanceFactor(n)) { case -2: { const int right = rightNode(n); if(balanceFactor(right) == 1) { rotateRight(right); } rotateLeft(n); break; } case 2: { const int left = leftNode(n); if(balanceFactor(left) == -1) { rotateLeft(left); } rotateRight(n); break; } case -1: case 1: case 0: break; default: // We should throw an error assert(true == false); } n = p; } }
NodeT* insertNodeAVLtree(NodeT* root, int data) { if(root==NULL) return createNodeT(data); else { if(root->data < data) root->right=insertNodeAVLtree(root->right, data); else root->left=insertNodeAVLtree(root->left, data); } root->height=maxx(height(root->left), height(root->right))+1; int aux=balanceFactor(root); if(aux>1) { if(root->left->data > data)//RR rightRotation(&root); else//LR LeftRight(&root); } else if(aux<-1) { if(root->right->data < data)//LL leftRotation(&root); else//RL RightLeft(&root); } //else return root; }
int verifBal(Node *N){ if(N != NULL){ N->bal = balanceFactor(N); if((N->bal == -1) || (N->bal == 0) || (N->bal == 1)){ return(NO_ACTION); }else{ //A positivo if(sign(N->bal) == 1){ //A e B positivos if(sign(N->bal) == sign(N->l->bal)){ return(SIMPLE_RIGHT); //A positivo e B negativo }else{ return(DOUBLE_LR); } //A negativo }else{ //A e B negativos if(sign(N->bal) == sign(N->r->bal)){ return(SIMPLE_LEFT); //A negativo e B positivo }else{ return(DOUBLE_RL); } } } }else{ return(NO_ACTION); } }
/****Function to insert the node ****/ nodeT* insertNode(nodeT* node, int data) { if (node == NULL) return(createNode(data)); if (data < node->data) node->left = insertNode(node->left, data); else node->right = insertNode(node->right, data); node->height = max(node->left, node->right) + 1; int balance =balanceFactor(node); if (balance > 1 && data < node->left->data) return singleRotRight(node); if (balance < -1 && data > node->right->data) return singleRotLeft(node); if (balance > 1 && data > node->left->data) { return doubleRotLeftRight(node); } if (balance < -1 && data < node->right->data) { return doubleRotRightLeft(node); } return node; }
nodeT* insertNew(nodeT*root, int value) { ///make the actual insertion if (root==NULL) return createNode(value); if (value<root->data) root->left=insertNew(root->left,value); else if (value>root->data) root->right=insertNew(root->right,value); ///update height root->height=max(height(root->left),height(root->right))+1; ///calculating balance factor int bF=balanceFactor(root); ///make the rotation, if necessary if (bF==-2) { if (root->right->data<value) singleLeftRotation(&root); if (root->right->data>value) rightLeftRotation(&root); } else { if (bF==2) { if (root->left->data>value) singleRightRotation(&root); if (root->left->data<value) leftRightRotation(&root); } } return root; }
TreeNode *balanseNode (TreeNode *node) { findHeight(node); if (balanceFactor(node) == 2) { if (balanceFactor(node->right) < 0) node->right = rightRoate(node->right); return leftRoate(node); } if (balanceFactor(node) == -2) { if (balanceFactor(node->left) > 0) node->left = leftRoate(node->left); return rightRoate(node); } return node; }
void updateBal(Node *N){ if(N == NULL){ return; }else{ updateBal(N->l); updateBal(N->r); N->bal = balanceFactor(N); } }
Node* insertNode(Node *N,int num){ if(N == NULL){ N = (Node*)malloc(sizeof(Node)); N->inf = num; N->l = NULL; N->r = NULL; N->bal = balanceFactor(N); }else{ if(num < N->inf){ N->l = insertNode(N->l,num); }else{ N->r = insertNode(N->r,num); } N = OPfunction(N); } return(N); }
Node* Node::rebalance ( ) { updateHeightAndCount(); int parent_balance_factor = balanceFactor(); int child_balance_factor; Node* new_root = this; assert(parent_balance_factor >= -2 && parent_balance_factor <= 2); if (parent_balance_factor >= -1 && parent_balance_factor <= 1) { // Nothing to do, just return the root return new_root; } if (parent_balance_factor == -2) { // Tree is right-heavy child_balance_factor = right_->balanceFactor(); if (child_balance_factor == 1) { // Rotate right through child right_ = right_->rotateRight(); } // Rotate left through us new_root = rotateLeft(); } else { // Tree is left-heavy child_balance_factor = left_->balanceFactor(); if (child_balance_factor == -1) { // Rotate left through child left_ = left_->rotateLeft(); } // Rotate right through us new_root = rotateRight(); } return new_root; }