static Position DoubleRotateWithRight(Position k3) { k3->right = SingleRotateWithLeft(k3->right); return SingleRotateWithRight(k3); }
static Position DoubleRotateWithLeft(Position K3) { K3->Left = SingleRotateWithRight(K3->Left); return SingleRotateWithLeft(K3); }
void delete_node(AVLTree& root, int data) { if (root == NULL) { printf("NULL to delete\n"); return; } if (root->data == data) { AVLTree node = root; printf("begin to delete\n"); if (root->lchild == NULL && root->rchild == NULL) { root = NULL; } else if(root->rchild != NULL && root->lchild != NULL) { AVLTree loop = root->rchild; while (loop->lchild != NULL) { loop = loop->lchild; } root->data = loop->data; delete_node(root->rchild, loop->data); } else { if (root->lchild == NULL) { root = root->rchild; } else { root = root->lchild; } } free(node); } else if (root->data < data) { delete_node(root->rchild, data); } else { delete_node(root->lchild, data); } if (root == NULL) { return; } root->height = Max(Height(root->lchild), Height(root->rchild)) + 1; if (Height(root->lchild) - Height(root->rchild) == 2) { if (Height(root->lchild->lchild) > Height(root->lchild->rchild)) { SingleRotateWithLeft(root); } else { DoubleRotateWithLeft(root); } } if (Height(root->rchild) - Height(root->lchild) == 2) { if (Height(root->rchild->rchild) > Height(root->rchild->lchild)) { SingleRotateWithRight(root); } else { DoubleRotateWithRight(root); } } }
void DoubleRotateWithRight(AVLTree& node) { SingleRotateWithLeft(node->rchild); SingleRotateWithRight(node); }
/*Delete X from Tree T*/ AVLTree Delete(ElementType X,AVLTree T){ Position Tmp; if(T == NULL) printf("Element not found."); else if(X < T->Element){ /*Delete From Left*/ T->Left = Delete(X,T->Left); /*handle the height first*/ T->Height = 1 + (MAX(Height(T->Left),Height(T->Right))); /*Check balance:*/ /*If T is right heavy*/ if(2 == (Height(T->Right) - Height(T->Left))){ if(Height(T->Right->Right) > Height(T->Right->Left)){ /*If T's right subtree is right heavy, Just Single Roate With Left.*/ T = SingleRotateWithLeft(T); } else{ /*T's right subtree is left heavy, Then Double Rotate With Right-Left.*/ T = DoubleRotateWithRightLeft(T); } } } else if(X > T->Element){ /*Delete From Right*/ T->Right = Delete(X,T->Right); /*handle the height first*/ T->Height = 1 + (MAX(Height(T->Left),Height(T->Right))); /*Check balance:*/ /*If T is left heavy*/ if(2 == (Height(T->Left) - Height(T->Right))){ if(Height(T->Left->Left) > Height(T->Left->Right)){ /*If T's left subtree is left heavy, Just Single Rotate With Right.*/ T = SingleRotateWithRight(T); } else{ /*T's left subtree is right heavy, Then Double Rotate With Left-Right.*/ T = DoubleRotateWithLeftRight(T); } } } else /*Found element to be deleted*/ if(T->Left && T->Right){/*Two children*/ /*Delete the successor node of middle-parent order traversal*/ //Tmp = DeleteSuccessor(T->Right); Tmp = FindMin(T->Right); /*Replace with smallest in right subtree*/ /*T->Element = Tmp->Element; free(Tmp);*/ T->Element = Tmp->Element; T->Right = Delete(T->Element,T->Right); } else{ /*one or zero children*/ Tmp = T; if(T->Left == NULL) /*Also handles 0 children*/ T = T->Right; else if(T->Right == NULL) T = T->Left; free(Tmp); } /*Handle the height.*/ if(T != NULL) T->Height = 1 + (MAX(Height(T->Left),Height(T->Right))); return T; }
AvlTree DoubleRotateWithRight(AvlTree root){ root->right = SingleRotateWithLeft(root->right); return SingleRotateWithRight(root); }