Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
42 views13 pages

Dsa Mod4 - Part3

Uploaded by

Sonia Devi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views13 pages

Dsa Mod4 - Part3

Uploaded by

Sonia Devi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Data Structures and Applications (CS204) Module 4

4.8 AVL Trees


AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL in honour of
its inventors.

AVL Tree can be defined as height balanced binary search tree in which each node is associated with a
balance factor which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree.

Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the tree will be
unbalanced and need to be balanced.

Balance Factor (k) = height (left(k)) - height (right(k))


If balance factor of any node is 1, it means that the left sub-tree is one level higher than the right sub-tree.

If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal height.

If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right sub-tree.

An AVL tree is given in the following figure. We can see that, balance factor associated with each node is in
between -1 and +1. therefore, it is an example of AVL tree.

Operations on AVL tree


Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are performed in the
same way as they are performed in a binary search tree. Searching and traversing do not lead to the
violation in property of AVL tree. However, insertion and deletion are the operations which can violate
this property and therefore, they need to be revisited.

Operation Description

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 1


Data Structures and Applications (CS204) Module 4

Traversing Same as Binary Search Tree

Search Same as Binary Search Tree

Insertion in AVL tree is performed in the same way as it is performed in a binary search
Insertion tree. However, it may lead to violation in the AVL tree property and therefore the tree may
need balancing. The tree can be balanced by applying rotations.

Deletion can also be performed in the same way as it is performed in a binary search tree.
Deletion Deletion may also disturb the balance of the tree therefore, various types of rotations are
used to rebalance the tree.

Why AVL Tree?


AVL tree controls the height of the binary search tree by not letting it to be skewed.

AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1. There are basically
four types of rotations which are as follows:

1. L L rotation: Inserted node is in the left subtree of left subtree of A


2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L rotation : Inserted node is in the left subtree of right subtree of A

Where node A is the node whose balance Factor is other than -1, 0, 1.

The first two rotations LL and RR are single rotations and the next two rotations LR and RL are double
rotations. For a tree to be unbalanced, minimum height must be at least 2, Let us understand each rotation.
1. RR Rotation
When BST becomes unbalanced, due to a node is inserted into the right subtree of the right subtree of A, then
we perform RR rotation, RR rotation is an anticlockwise rotation, which is applied on the edge below a node
having balance factor -2

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 2


Data Structures and Applications (CS204) Module 4

In above example, node A has balance factor -2 because a node C is inserted in the right subtree of A right
subtree. We perform the RR rotation on the edge below A.
2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left subtree of the left subtree of C, then we
perform LL rotation, LL rotation is clockwise rotation, which is applied on the edge below a node having
balance factor 2.

In above example, node C has balance factor 2 because a node A is inserted in the left subtree of C left subtree.
We perform the LL rotation on the edge below A.

3. LR Rotation
Double rotations are bit tougher than single rotation which has already explained above. LR rotation = RR
rotation + LL rotation, i.e., first RR rotation is performed on subtree and then LL rotation is performed on full
tree, by full tree we mean the first node from the path of inserted node whose balance factor is other than -1, 0, or
1.

Let us understand each and every step very clearly:

State Action

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 3


Data Structures and Applications (CS204) Module 4

A node B has been inserted into the right subtree of A the left subtree of C,
because of which C has become an unbalanced node having balance factor 2.
This case is L R rotation where: Inserted node is in the right subtree of left
subtree of C.

As LR rotation = RR + LL rotation, hence RR (anticlockwise) on subtree


rooted at A is performed first. By doing RR rotation, node A, has become the
left subtree of B.

After performing RR rotation, node C is still unbalanced, i.e., having balance


factor 2, as inserted node A is in the left of left of C

Now we perform LL clockwise rotation on full tree, i.e. on node C.


node C has now become the right subtree of node B, A is left subtree of B

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 4


Data Structures and Applications (CS204) Module 4

Balance factor of each node is now either -1, 0, or 1, i.e. BST is balanced
now.

4. RL Rotation

R L rotation = LL rotation + RR rotation, i.e., first LL rotation is performed on subtree and then RR rotation is
performed on full tree, by full tree we mean the first node from the path of inserted node whose balance factor is
other than -1, 0, or 1.

State Action

A node B has been inserted into the left subtree of C the right subtree of A,
because of which A has become an unbalanced node having balance factor -
2. This case is RL rotation where: Inserted node is in the left subtree of right
subtree of A

As RL rotation = LL rotation + RR rotation, hence, LL (clockwise) on


subtree rooted at C is performed first. By doing RR rotation, node C has
become the right subtree of B.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 5


Data Structures and Applications (CS204) Module 4

After performing LL rotation, node A is still unbalanced, i.e. having balance


factor -2, which is because of the right-subtree of the right-subtree node A.

Now we perform RR rotation (anticlockwise rotation) on full tree, i.e. on


node A. node C has now become the right subtree of node B, and node A has
become the left subtree of B.

Balance factor of each node is now either -1, 0, or 1, i.e., BST is balanced
now.

Q: Construct an AVL tree having the following elements:


H, I, J, B, A, E, C, F, D, G, K, L

1. Insert H, I, J

On inserting the above elements, especially in the case of H, the BST becomes unbalanced as the Balance
Factor of H is -2. Since the BST is right-skewed, we will perform RR Rotation on node H.

The resultant balance tree is:

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 6


Data Structures and Applications (CS204) Module 4

2. Insert B, A

On inserting the above elements, especially in case of A, the BST becomes unbalanced as the Balance
Factor of H and I is 2, we consider the first node from the last inserted node i.e. H. Since the BST from H
is left-skewed, we will perform LL Rotation on node H.

The resultant balance tree is:

3. Insert E

On inserting E, BST becomes unbalanced as the Balance Factor of I is 2, since if we travel from E to I we
find that it is inserted in the left subtree of right subtree of I, we will perform LR Rotation on node I. LR
= RR + LL rotation

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 7


Data Structures and Applications (CS204) Module 4

3 a) We first perform RR rotation on node B

The resultant tree after RR rotation is:

3b) We first perform LL rotation on the node I

The resultant balanced tree after LL rotation is:

4. Insert C, F, D

On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B and H is -2, since if we travel
from D to B we find that it is inserted in the right subtree of left subtree of B, we will perform RL
Rotation on node I. RL = LL + RR rotation.

4a) We first perform LL rotation on node E - The resultant tree after LL rotation is:

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 8


Data Structures and Applications (CS204) Module 4

4b) We then perform RR rotation on node B

The resultant balanced tree after RR rotation is:

5) Insert G

On inserting G, BST become unbalanced as the Balance Factor of H is 2, since if we travel from
G to H, we find that it is inserted in the left subtree of right subtree of H, we will perform LR
Rotation on node I. LR = RR + LL rotation.

5 a) We first perform RR rotation on node C

The resultant tree after RR rotation is:

5 b) We then perform LL rotation on node H

The resultant balanced tree after LL rotation is:

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 9


Data Structures and Applications (CS204) Module 4

6. Insert K

On inserting K, BST becomes unbalanced as the Balance Factor of I is -2. Since the BST is right-skewed
from I to K, hence we will perform RR Rotation on the node I.

The resultant balanced tree after RR rotation is:

7. Insert L

On inserting the L tree is still balanced as the Balance Factor of each node is now either, -1, 0, +1. Hence
the tree is a Balanced AVL tree

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 10


Data Structures and Applications (CS204) Module 4

Program - AVL Tree Implementation


#include <stdio.h>
#include <stdlib.h>
struct AVL
{
int key;
struct AVL *left;
struct AVL *right;
int height;
};
typedef struct AVL node;
int getHeight(node *n){
if(n==NULL)
return 0;
return n->height;
}
node *createNode(int key)
{
node* n = (node *) malloc(sizeof(node));
n->key = key;
n->left = NULL;
n->right = NULL;
n->height = 1;
return n;
}
int max (int a, int b)
{
return (a>b)?a:b;
}
int getBalanceFactor(node * n)
{
if(n==NULL)
{
return 0;
}
return getHeight(n->left) - getHeight(n->right);
}
node* leftRotate(node* y)
{
node* x = y->left;
node* T2 = x->right;
x->right = y;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 11


Data Structures and Applications (CS204) Module 4

y->left = T2;
x->height = max(getHeight(x->right), getHeight(x->left)) + 1;
y->height = max(getHeight(y->right), getHeight(y->left)) + 1;
return x;
}
node* rightRotate(node* x)
{
node* y = x->right;
node* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(getHeight(x->right), getHeight(x->left)) + 1;
y->height = max(getHeight(y->right), getHeight(y->left)) + 1;
return y;
}
node *insert(node* n, int key)
{
if (n == NULL)
return createNode(key);
if (key < n->key)
n->left = insert(n->left, key);
else if (key > n->key)
n->right = insert(n->right, key);
n->height = 1 + max(getHeight(n->left), getHeight(n->right));
int bf = getBalanceFactor(n);
// Left Left Case
if(bf>1 && key < n->left->key)
{
return leftRotate(n);
}
// Right Right Case
if(bf<-1 && key > n->right->key)
{
return rightRotate(n);
}
// Left Right Case
if(bf>1 && key > n->left->key)
{
n->left = rightRotate(n->left);
return leftRotate(n);
}
// Right Left Case

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 12


Data Structures and Applications (CS204) Module 4

if(bf<-1 && key < n->right->key)


{
n->right = leftRotate(n->right);
return rightRotate(n);
}
return n;
}
void printtree(node *root, int space, int n)
{
int i;
if (root == NULL)
return;
space +=n; // The variable space is incremented by n, the indentation level for the next level of nodes
printtree(root->right, space,n);
printf("\n");
for (i =n; i < space; i++)
printf(" ");
printf("(%d)\n", root->key);
printtree(root->left, space,n);
}
int main()
{
node * root = NULL;
int n,i,key;
printf("enter the number of nodes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the key\n");
scanf("%d",&key);
root = insert(root, key);
}
printtree(root, 0, n) ;
return 0;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 13

You might also like