avlTreeNode* balanceLeft(avlTreeNode* root) { int balanceDifference; balanceDifference = calculateMaxHight( root-> leftTree ); printf("\nIn balanceLeft Insertion\n"); switch ( balanceDifference ) { case 1: case 0: { printf("\nIn RightRotation Insertion\n"); return rightRotation( root ); } case -1: { printf("\nIn LeftRotation Insertion\n"); root -> leftTree = leftRotation( root -> leftTree ); printf("\nIn RightRotation Insertion\n"); return rightRotation( root ); } } return root; }
NodeT *Balance(NodeT *root) { int balance = getBalance(root); if(balance == -2) { balance = getBalance(root->left); if(balance >=1) { root -> left = leftRotation(root->left); root = rightRotation(root); } else root = rightRotation(root); } else { if(balance == 2) { balance = getBalance(root); if(balance <= -1) { root -> right = rightRotation(root->right); root = leftRotation(root); } else root = leftRotation(root); } } getHeights(root); return root; }
// Worst case insertion O(2*logn) struct Node *insert(struct Node *root,int data) { if(root == NULL) return create(data); if(data > root->data) root->right = insert(root->right,data); else root->left = insert(root->left,data); root->height = max(height(root->left),height(root->right)) + 1; int balance = getbalance(root); // left left case if(balance > 1 && data < root->left->data) return rightRotation(root); else // left right case if(balance > 1 && data > root->left->data) { root->left = leftRotation(root->left); return rightRotation(root); } else // right-right case if(balance < -1 && data > root->right->data) { return leftRotation(root); } else // right left case; if(balance < -1 && data < root->right->data) { root->right = rightRotation(root->right); return leftRotation(root); } // no change required; return root; }
avlTreeNode* balanceRight( avlTreeNode* root ) { int balanceDifference; printf("\nIn BalnaceRight Insertion\n"); balanceDifference = calculateMaxHight( root-> rightTree ); printf("========== %d\n",balanceDifference); switch ( balanceDifference ) { case 1: { printf("\nIn RightRotation Insertion\n"); root -> rightTree = rightRotation( root -> rightTree ); printf("\nIn leftRotation Insertion\n"); return leftRotation( root ); } case -1: case 0 : { return leftRotation( root ); } } return root; }
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; }
TREE* insert(char * name, TREE * t){ addNum++; if(t == NULL){ t = (TREE*)malloc(sizeof(TREE)); t -> name = name; t -> height = 0; t -> ls = t -> rs = NULL; return t; } else if(strcmp(name, t -> name) <= 0){ t -> ls = insert(name, t -> ls); if(height(t -> ls) - height(t -> rs) == 2 ){ if(strcmp(name, t -> ls -> name) <= 0) t = leftRotation(t); else t = doubleLeftRotation(t); } } else if(strcmp(name, t -> name) > 0){ t -> rs = insert(name, t -> rs); if(height(t -> rs) - height(t -> ls) == 2){ if(strcmp(name, t -> rs -> name) > 0) t = rightRotation(t); else t = doubleRightRotation(t); } } t->height = MAX(height(t -> ls), height(t -> rs)) + 1; return t; }
nodePtr splay(char* word, nodePtr leaf) { if(leaf->left != NULL && strcmp(leaf->left->info.word, word)==0) return rightRotation(leaf); else if (leaf->right != NULL && strcmp(leaf->right->info.word, word)==0) return leftRotation(leaf); return leaf; }
void BinarySearchTree::twoRotations(Node *TopNode, Node *SubNode, bool direction) { if(direction == LINKED_LEFT) { // If top node was linked left then rotate to the right // and link to balanced tree TopNode->assignNewRightNode(rightRotation(SubNode)); rotateAndLink(TopNode, LINKED_LEFT); } else { TopNode->assignNewLeftNode(leftRotation(SubNode)); rotateAndLink(TopNode, LINKED_RIGHT); } }
void BinarySearchTree::rotateAndLink(Node* TopNode, bool direction) { // Link the balanced subtree back to the Binary Search Tree if(direction == LINKED_LEFT) { if(TopNode == RootNode) RootNode = leftRotation(TopNode); else { Node *TreeNodeLink = TopNode->getHigherNodePtr(); if(TopNode->getHigherNodeDir() == LINKED_LEFT) TreeNodeLink->assignNewLeftNode(leftRotation(TopNode)); else TreeNodeLink->assignNewRightNode(leftRotation(TopNode)); } } else { if(TopNode == RootNode) RootNode = rightRotation(TopNode); else { Node *TreeNodeLink = TopNode->getHigherNodePtr(); if(TopNode->getHigherNodeDir() == LINKED_LEFT) TreeNodeLink->assignNewLeftNode(rightRotation(TopNode)); else TreeNodeLink->assignNewRightNode(rightRotation(TopNode)); } } }
nodePtr rootInsertion(char* word, nodePtr leaf) { if(leaf == NULL) return createNode(word); if(strcmp(word,leaf->info.word ) < 0) { leaf->left = rootInsertion(word, leaf->left); leaf = rightRotation(leaf); } else if((strcmp(word,leaf->info.word) > 0)) { leaf->right = rootInsertion(word,leaf->right); leaf = leftRotation(leaf); } return leaf; }
void avlInsert(treePointer *parent, element x, int *unbalanced) { if (!*parent) { *unbalanced = TRUE; *parent = malloc(sizeof(Node)); (*parent)->leftChild = (*parent)->rightChild = NULL; (*parent)->bf = 0; (*parent)->data = x; } else if (x.key < (*parent)->data.key) { avlInsert(&(*parent)->leftChild, x, unbalanced); if (*unbalanced) switch ((*parent)->bf) { case -1: (*parent)->bf = 0; *unbalanced = FALSE; break; case 0: (*parent)->bf = 1; break; case 1: leftRotation(parent, unbalanced); } } else if (x.key > (*parent)->data.key) { avlInsert(&((*parent)->rightChild), x, unbalanced); if (*unbalanced) switch ((*parent)->bf) { case 1: (*parent)->bf = 0; *unbalanced = FALSE; break; case 0: (*parent)->bf = -1; break; case -1: rightRotation(parent, unbalanced); } } else { *unbalanced = FALSE; printf("The key is already in the tree"); } }
Tree* checkBalance(Tree *t) { int h; if(t->left != NULL && t->right != NULL) h = height(t); if(height(t->left) - height(t->right) > 1) { if (height(t->left->left) - height(t->left->right) > 0) t = rightRotation(t); else t = leftAndRightRotation(t); } else if(height(t->left) - height(t->right) < -1) { if (height(t->right->left) - height(t->right->right) < 0) t = leftRotation(t); else t = rightAndLeftRotation(t); } return t; }
void avlInsert(treePointer *parent,element x,int *unbalanced) { void leftRotation(treePointer *parent,int *unbalanced); void rightRotation(treePointer *parent,int *unbalanced); if(!*parent) { // insert element into null tree *unbalanced=TRUE; MALLOC(*parent,sizeof(treePointer)); (*parent)->leftChild=(*parent)->rightChild=NULL; (*parent)->bf=0; (*parent)->data=x; } else if(x.key<(*parent)->data.key) { avlInsert(&(*parent)->leftChild,x,unbalanced); if(*unbalanced) // left branch has grown higher switch((*parent)->bf) { case -1: (*parent)->bf=0; *unbalanced=FALSE; break; case 0: (*parent)->bf=1; break; case 1: leftRotation(parent,unbalanced); } } else if(x.key>(*parent)->data.key) { avlInsert(&(*parent)->rightChild,x,unbalanced); if(*unbalanced) // right branch has grown higher switch((*parent)->bf) { case 1: (*parent)->bf=0; *unbalanced=FALSE; break; case 0: (*parent)->bf=-1; break; case -1: rightRotation(parent,unbalanced); } } else { *unbalanced=FALSE; printf("The key is already in the tree\n"); } }
void rightLeftRotation(NodeT **root3) { rightRotation(&((*root3)->right)); leftRotation(root3); }
void leftRightRotation(NodeT **root3) { leftRotation(&((*root3)->left)); rightRotation(root3); }
void RightLeft(NodeT** root) { rightRotation(& ((*root)->right)); leftRotation(root); }
void LeftRight(NodeT** root) { leftRotation(& ((*root)->left)); rightRotation(root); }
TREE* doubleRightRotation(TREE * k1){ k1 -> rs = leftRotation(k1 -> rs); return rightRotation(k1); }
Tree* rightAndLeftRotation(Tree *t) { t = rightRotation(t); return leftRotation(t); }
Tree* leftAndRightRotation(Tree *t) { t = leftRotation(t); return rightRotation(t); }
TREE* doubleLeftRotation(TREE * k3){ k3 -> ls = rightRotation(k3 -> ls); return leftRotation(k3); }