# Deletion In AVL Tree In C++

## C++ Program For Deletion In AVL Tree:

AVL tree is self balancing tree in which for all nodes, the difference of height between the left subtree and the right subtree is less than or equal to 1. In this article, an avl tree is created and the difference of height is printed for each node. ## Deletion in an AVL Tree

• Deletion in an AVL tree is similar to that in a BST.
• Deletion of a node tends to disturb the balance factor. Thus to balance the tree, we again use the Rotation mechanism.
• Deletion in AVL tree consists of two steps:
• Removal of the node: The given node is removed from the tree structure. The node to be removed can either be a leaf or an internal node.
• Re-balancing of the tree: The elimination of a node from the tree can cause disturbance to the balance factor of certain nodes. Thus it is important to re- balance theb_fact of the nodes; since the balance factor is the primary aspect that ensures the tree is an AVL Tree.

Note: There are certain points that must be kept in mind during a deletion process.

• If the node to be deleted is a leaf node, it is simply removed from the tree.
• If the node to be deleted has one child node, the child node is replaced with the node to be deleted simply.
• If the node to be deleted has two child nodes then,
• Either replace the node with it’s inorder predecessor , i.e, the largest element of the left sub tree.
• Or replace the node with it’s inorder successor , i.e, the smallest element of the right sub tree.

Let us consider the same example as above with some additional elements as shown.

We can see in the image the balance factor of each node in the tree. #### Step 1:

• The node to be deleted from the tree is 8.
• If we observe it is the parent node of the node and 9.
• Since the node 8 has two children it can be replaced by either of it’s child nodes.

#### Step 2:

• The node 8 is deleted from the tree.
• As the node is deleted we replace it with either of it’s children nodes.
• Here we replaced the node with the inorder successor , i.e, 9.
• Again we check the balance factor for each node. #### Step 3:

• Now The next element to be deleted is 12.
• If we observe, we can see that the node 12 has a left subtree and a right subtree.
• We again can replace the node by either it’s inorder successor or inorder predecessor.
• In this case we have replaced it by the inorder successor.

#### Step 4:

•  The node 12 is deleted from the tree.
• Since we have replaced the node with the inorder successor, the tree structure looks like shown in the image.
• After removal and replacing check for the balance factor of each node of the tree. #### Step 5:

• The next node to be eliminated is 14.
• It can be seen clearly in the image that 14 is a leaf node.
• Thus it can be eliminated easily from the tree.

#### Step 6:

• As the node 14 is deleted, we check the balance factor of all the nodes.
• We can see the balance factor of the node 13 is 2.
• This violates the terms of the AVL tree thus we need to balance it using the rotation mechanism. #### Step 7:

• In order to balance the tree, we identify the rotation mechanism to be applied.
• Here we need to use LL Rotation.
• The nodes involved in the rotation is shown as follows.

#### Step 8:

• The nodes are rotated and the tree satisfies the conditions of an AVL tree.
• The final structure of the tree is shown as follows.
• We can see all the nodes have their balance factor as ‘0’ , ‘1’ and ‘-1’. ## Code:

#include<bits/stdc++.h>
using namespace std;
struct node
{
int element;
struct node *left,*right;
int ht;

};

node *insert(node *,int);
node *Delete(node *,int);
void preorder(node *);
void inorder(node *);
int heightnode *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int balanceFactor(node *);
int main()
{
node *root=NULL;
int x,n,i,option;
do {
cout << “1. Create AVL Tree\n;
cout << “2. Delete Element\n;
cout << “3. End Program\n;
cout << “Enter your choice “;
cin >> option;
switch(option)
{
case 1: cout << \nEnter no. of elements : “;
cin >> n;
root = NULL;
for( i = 0; i < n;i++) {

cout << “Enter element of tree “;
cin >> x;
root = insert(root,x);
}
cout << \nPreorder sequence:\n;
preorder(root);
cout << \n\nInorder sequence:\n;
inorder(root);
break;

case 2: cout << “Enter a element to be deleted : “;
cin >> x;
root = Delete(root,x);
cout << “Preorder sequence:\n;
preorder(root);
cout << \nInorder sequence:\n;
inorder(root);
break;
}
}while(option!=3);

return 0;

}

node * insert(node *T,int x)
{

if(T == NULL){

T = (node*)malloc(sizeof(node));
-> element = x;
-> left = NULL;
-> right = NULL;
}
else

if(x > T->element)
{

-> right = insert(-> right,x);
if(balanceFactor(T) == –2)
if( x > -> right -> element)
T = RR(T);
else
T = RL(T);

}
else
if(x<T->element)
{
-> left = insert(-> left,x);
if(balanceFactor(T)==2)
if(x < T-> left -> element)
T=LL(T);
else
T=LR(T);

}

-> ht = height(T);

return(T);
}
node * Delete(node *T,int x)
{
node *p;

if(T == NULL)
{
return NULL;
}
else
if(x > T->element)
{
-> right = Delete(-> right,x);
if(balanceFactor(T) == 2)
if(balanceFactor(-> left) >= 0)
T = LL(T);
else
T = LR(T);
}
else
if(x < -> element)
{
-> left = Delete(T->left,x);
if(balanceFactor(T)==-2)
if(balanceFactor(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
if(-> right != NULL)
{
p = -> right;

while(-> left != NULL)
p = -> left;

-> element = -> element;
-> right = Delete(-> right,p->element);

if(balanceFactor(T) == 2)
if(balanceFactor (-> left) >= 0)
T=LL(T);
else
T=LR(T);\
}
else
return(T->left);
}
->ht = height(T);
return(T);
}
int height(node *T)
{

int lh,rh;
if(T == NULL)
return(0);
if-> left == NULL)
lh = 0;
else
lh = 1 + -> left -> ht;

if(-> right == NULL)
rh = 0;
else
rh=1+T->right->ht;

if(lh > rh)
return(lh);

return(rh);
}

node * rotateright(node *x)
{

node *y;
y = -> left;
-> left = -> right;
-> right = x;
-> ht = height(x);
-> ht = height(y);
return(y);
}

node * rotateleft(node *x)
{
node *y;
y = -> right;
-> right = -> left;
-> left = x;
-> ht = height(x);
-> ht = height(y);
return(y);
}

node * RR(node *T)
{

T = rotateleft(T);
return(T);
}

node * LL(node *T)
{
T = rotateright(T);
return(T);
}

node * LR(node *T)
{

-> left = rotateleft(T->left);
T = rotateright(T);
return(T);
}

node * RL(node *T)
{
-> right = rotateright(T->right);
T = rotateleft(T);
return(T);
}
int balanceFactor(node *T)
{
int lh,rh;
if( T == NULL)
return(0);
if(-> left == NULL)
lh = 0;
else
lh = 1 + T->left->ht;
if(-> right == NULL)
rh = 0;
else
rh = 1 + -> right -> ht;
return(lh-rh);
}

void preorder(node *T)
{
if( T != NULL)
{
cout << “Balance factor “ << -> element << ” “ << balanceFactor(T) << endl;
preorder(T->left);
preorder(T->right);
}
}
void inorder(node *T)
{

if( T != NULL)
{
inorder(T->left);
cout << “Balance Factor “<<-> element<<” “ << balanceFactor(T) << endl;
inorder(T->right);
}
}

## Output:

`1. Create AVL Tree2. Delete Element3. End ProgramEnter your choice 1Enter no. of elements : 13Enter element of tree 15Enter element of tree 12Enter element of tree 54Enter element of tree 8Enter element of tree 13Enter element of tree 18Enter element of tree 60Enter element of tree 5Enter element of tree 9Enter element of tree 14Enter element of tree 16Enter element of tree 56Enter element of tree 70Preorder sequence:Balance factor 15 0Balance factor 12 0Balance factor 8 0Balance factor 5 0Balance factor 9 0Balance factor 13 -1Balance factor 14 0Balance factor 54 0Balance factor 18 1Balance factor 16 0Balance factor 60 0Balance factor 56 0Balance factor 70 0Inorder sequence:Balance Factor 5 0Balance Factor 8 0Balance Factor 9 0Balance Factor 12 0Balance Factor 13 -1Balance Factor 14 0Balance Factor 15 0Balance Factor 16 0Balance Factor 18 1Balance Factor 54 0Balance Factor 56 0Balance Factor 60 0Balance Factor 70 01. Create AVL Tree2. Delete Element3. End ProgramEnter your choice 2Enter a element to be deleted : 8Preorder sequence:Balance factor 15 0Balance factor 12 0Balance factor 9 1Balance factor 5 0Balance factor 13 -1Balance factor 14 0Balance factor 54 0Balance factor 18 1Balance factor 16 0Balance factor 60 0Balance factor 56 0Balance factor 70 0Inorder sequence:Balance Factor 5 0Balance Factor 9 1Balance Factor 12 0Balance Factor 13 -1Balance Factor 14 0Balance Factor 15 0Balance Factor 16 0Balance Factor 18 1Balance Factor 54 0Balance Factor 56 0Balance Factor 60 0Balance Factor 70 01. Create AVL Tree2. Delete Element3. End ProgramEnter your choice 1 2Enter a element to be deleted : 12Preorder sequence:Balance factor 15 0Balance factor 13 1Balance factor 9 1Balance factor 5 0Balance factor 14 0Balance factor 54 0Balance factor 18 1Balance factor 16 0Balance factor 60 0Balance factor 56 0Balance factor 70 0Inorder sequence:Balance Factor 5 0Balance Factor 9 1Balance Factor 13 1Balance Factor 14 0Balance Factor 15 0Balance Factor 16 0Balance Factor 18 1Balance Factor 54 0Balance Factor 56 0Balance Factor 60 0Balance Factor 70 01. Create AVL Tree2. Delete Element3. End ProgramEnter your choice 2Enter a element to be deleted : 14Preorder sequence:Balance factor 15 -1Balance factor 9 0Balance factor 5 0Balance factor 13 0Balance factor 54 0Balance factor 18 1Balance factor 16 0Balance factor 60 0Balance factor 56 0Balance factor 70 0Inorder sequence:Balance Factor 5 0Balance Factor 9 0Balance Factor 13 0Balance Factor 15 -1Balance Factor 16 0Balance Factor 18 1Balance Factor 54 0Balance Factor 56 0Balance Factor 60 0Balance Factor 70 01. Create AVL Tree2. Delete Element3. End ProgramEnter your choice 3`

O(n)

O(n)