You are on page 1of 37

AVL Trees

DR. SHWETA SHARMA


Drawbacks of BST
What is the disadvantage of skewed trees in BST?

• Searching Time increases


• More comparisons

Right Left Skewed


Skewed BST BST

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 2


AVL Trees
 AVL tree got its name after its inventor Georgy Adelson-Velsky and
Landis

 AVL trees are BST with additional property of height balancing

 AVL tree is a self-balancing binary search tree in which each node


maintains extra information called a balance factor whose value is
either -1, 0 or +1

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 3


Balance Factor

 Balance factor of a node in an AVL tree is the difference between the height
of the left subtree and that of the right subtree of that node

 Balance Factor = (Height of Left Subtree - Height of Right Subtree)


= H(L)-H(R)

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 4


Balance Factor
 The self balancing property of an AVL tree is maintained by the balance
factor

 The value of balance factor should always be -1, 0 or +1

 Balance factor of leaf nodes is ZERO as leaf nodes don’t have left or right
child

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 5


Example of AVL Trees

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 6


Question 1: Is this an AVL Tree?

The tree is AVL because the differences between the heights of


left and right subtrees for every node are less than or equal to
1

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 7


Question 2: Is this an AVL Tree?

The tree is not AVL because the differences between the


heights of the left and right subtrees for 8 and 12 are greater
than 1

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 8


Insertion in AVL Tree

 Insertion in AVL tree is performed in the same way as it is performed in


a binary search 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

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 9


AVL Rotations
Perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1
 L L rotation: Inserted node is in the left subtree of left subtree of A
 R R rotation : Inserted node is in the right subtree of right subtree of A
 L R rotation : Inserted node is in the right subtree of left subtree of A
 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
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 10
1 LL Rotation
Node A is inserted into the left subtree of the left subtree of C

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 11


2 RR Rotation
Node C is inserted into the right subtree of the right subtree of A

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 12


3 LR Rotation
Node B is inserted into the right subtree of A od the left subtree of C
LR rotation = RR + LL rotation

L
L

R
L

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 13


4 RL Rotation
Node B is inserted into the left subtree of C of the right subtree of A
RL rotation = LL + RR rotation

R R

L R

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 14


Question 3: Construct an AVL tree from
node 1 to 5

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 15


Question 3.1: Construct an AVL tree from
node 1 to 10

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 16


Question 3.1: Construct an AVL tree from
node 1 to 10

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 17


Question 4: 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

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 18


Question 4: Construct an AVL tree having
the following elements
H, I, J, B, A, E, C, F, D, G, K, L
2. Insert B, A

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 19


Question 4: Construct an AVL tree having
the following elements
H, I, J, B, A, E, C, F, D, G, K, L
3. Insert E

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 20


Question 4: Construct an AVL tree having
the following elements
H, I, J, B, A, E, C, F, D, G, K, L
4. Insert C, F, D

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 21


Question 4: Construct an AVL tree having
the following elements
H, I, J, B, A, E, C, F, D, G, K, L
5. Insert G

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 22


Question 4: Construct an AVL tree having
the following elements
H, I, J, B, A, E, C, F, D, G, K, L
6. Insert K

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 23


Question 4: Construct an AVL tree having
the following elements
H, I, J, B, A, E, C, F, D, G, K, L
6. Insert L

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 24


Deletion from Right in an AVL Tree
1 R 0 rotation
2 R +1 rotation
3 R -1 rotation
4 L 0 rotation
5 L +1 rotation
6 L -1 rotation

NOTE: (1) Rotation from opposite side of deletion (in 6 cases)


(2) If Rotation on Left----> more height on Left as compared to Right ----> PROBLEM
(3) If Rotation on Right----> more height on Right as compared to Left ----> PROBLEM

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 25


Deletion from Right in an AVL Tree
1 R0 rotation: Single Rotation (Delete the node 30 )

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 26


Deletion from Right in an AVL Tree
2 R1 rotation: Single Rotation (Delete the node 55 )

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 27


Deletion from Right in an AVL Tree
3 R -1 rotation: Double Rotation (Delete the node 60 )

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 28


Deletion from Left in an AVL Tree
1 L0 rotation: Single Rotation

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 29


Deletion from Left in an AVL Tree
2 L1 rotation: Double Rotation

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 30


Deletion from Left in an AVL Tree
3 L -1 rotation: Single Rotation

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 31


Question 5
When node 25 will be deleted, what will be resultant AVL tree?

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 32


Solution 5

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 33


Question 6
When node 13 will be deleted, what will be resultant AVL tree?

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 34


Solution 6

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 35


Time Complexity
Operations Best case Average case Worst case

Insertion O(log n) O(log n) O(log n)

Deletion O(log n) O(log n) O(log n)

Search O(log n) O(log n) O(log n)

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 36


Thank you!

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 37

You might also like