# AVL Tree: Deletion

## 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’. ## Implementation of Deletion (C -Code) in an AVL tree

`#include<stdio.h>#include<stdlib.h> typedef struct node{    int element;    struct node *left,*right;    int ht;}node;node *insert(node *,int);node *Delete(node *,int);void preorder(node *);void inorder(node *);int height( node *);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    {        printf("\n  1)Create:");        printf("\n  2)Delete:");        printf("\n  3)Quit:");        printf("\n\tEnter Your Choice : ");        scanf("%d",&option);        switch(option)        {            case 1: printf("\nEnter no. of elements : ");                    scanf("%d",&n);                    root=NULL;                    for(i=0;i<n;i++)                    {                        printf("\tEnter %d Element of tree : ",i+1);                        scanf("%d",&x);                        root=insert(root,x);                    }                    printf("\nPreorder sequence:\n");                    preorder(root);                    printf("\n\nInorder sequence:\n");                    inorder(root);                    printf("\n-----------------------------------------\n\n");                    break;            case 2: printf("\n\tEnter a element to be deleted : ");                    scanf("%d",&x);                    root=Delete(root,x);                    printf("\nPreorder sequence:\n");                    preorder(root);                    printf("\n\nInorder sequence:\n");                    inorder(root);                    printf("\n-----------------------------------------\n\n");                    break;        }    }while(option!=3);    return 0;}node * insert(node *T,int x){    if(T==NULL)    {        T=(node*)malloc(sizeof(node));        T->element=x;        T->left=NULL;        T->right=NULL;    }    else        if(x > T->element)            {            T->right=insert(T->right,x);            if(balanceFactor(T)==-2)                if(x>T->right->element)                    T=RR(T);                else                    T=RL(T);        }        else            if(x<T->element)            {                T->left=insert(T->left,x);                if(balanceFactor(T)==2)                    if(x < T->left->element)                        T=LL(T);                    else                        T=LR(T);            }        T->ht=height(T);        return(T);}node * Delete(node *T,int x){    node *p;    if(T==NULL)    {        return NULL;    }    else        if(x > T->element)            {            T->right=Delete(T->right,x);            if(balanceFactor(T)==2)                if(balanceFactor(T->left)>=0)                    T=LL(T);                else                    T=LR(T);        }        else            if(x<T->element)            {                T->left=Delete(T->left,x);                if(balanceFactor(T)==-2)                        if(balanceFactor(T->right)<=0)                        T=RR(T);                    else                        T=RL(T);            }            else            {                if(T->right!=NULL)                {                    p=T->right;                    while(p->left!= NULL)                        p=p->left;                    T->element=p->element;                    T->right=Delete(T->right,p->element);                    if(balanceFactor(T)==2)                        if(balanceFactor(T->left)>=0)                            T=LL(T);                        else                            T=LR(T);\                }                else                    return(T->left);            }    T->ht=height(T);    return(T);}int height(node *T){    int lh,rh;    if(T==NULL)        return(0);    if(T->left==NULL)        lh=0;    else        lh=1+T->left->ht;    if(T->right==NULL)        rh=0;    else        rh=1+T->right->ht;    if(lh>rh)        return(lh);    return(rh);}node * rotateright(node *x){    node *y;    y=x->left;    x->left=y->right;    y->right=x;    x->ht=height(x);    y->ht=height(y);    return(y);}node * rotateleft(node *x){    node *y;    y=x->right;    x->right=y->left;    y->left=x;    x->ht=height(x);    y->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){    T->left=rotateleft(T->left);    T=rotateright(T);    return(T);}node * RL(node *T){    T->right=rotateright(T->right);    T=rotateleft(T);    return(T);}int balanceFactor(node *T){    int lh,rh;    if(T==NULL)        return(0);    if(T->left==NULL)        lh=0;    else        lh=1+T->left->ht;    if(T->right==NULL)        rh=0;    else        rh=1+T->right->ht;    return(lh-rh);}void preorder(node *T){    if(T!=NULL)    {        printf("\n\t%d(Balance Factor=%d)",T->element,balanceFactor(T));        preorder(T->left);        preorder(T->right);    }}void inorder(node *T){    if(T!=NULL)    {        inorder(T->left);        printf("\n\t%d(Balance Factor=%d)",T->element,balanceFactor(T));        inorder(T->right);    }}`