0% found this document useful (0 votes)
56 views21 pages

Data Structure and Algorithm (EC-594B) : A. K. Siromoni

The document discusses AVL trees, which are self-balancing binary search trees. It begins by defining an AVL tree as a binary search tree where the heights of any node's two child subtrees differ by at most one, and every subtree is also an AVL tree. The document then explains how insertions can cause imbalance in an AVL tree, and which rotations are required to rebalance the tree - either a single left or right rotation, or a double rotation consisting of a left then right or right then left rotation. Examples of rotations to rebalance the tree after insertions are shown.

Uploaded by

Abhigyan Prakash
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)
56 views21 pages

Data Structure and Algorithm (EC-594B) : A. K. Siromoni

The document discusses AVL trees, which are self-balancing binary search trees. It begins by defining an AVL tree as a binary search tree where the heights of any node's two child subtrees differ by at most one, and every subtree is also an AVL tree. The document then explains how insertions can cause imbalance in an AVL tree, and which rotations are required to rebalance the tree - either a single left or right rotation, or a double rotation consisting of a left then right or right then left rotation. Examples of rotations to rebalance the tree after insertions are shown.

Uploaded by

Abhigyan Prakash
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/ 21

Data Structure And Algorithm (EC-594B)

A. K. Siromoni

Department of Information Technology


Lecture 5

Lecture Objective 1 : Explain the advantages of height balanced tree

Lecture Objective 2 : Create AVL tree with proper rotation


Last Lecture
• Threaded Binary Tree

If sorted data is inserted in a Right Threaded BST

So no advantage of creation of BST with thread


Solution

Height Balanced BST

An AVL tree is a type of balanced binary search tree. Named after their
inventors, Adelson,Velskii and Landis, they were the first dynamically balanced
trees to be proposed.

Definition of an AVL tree

An AVL tree is a binary search tree which has the following properties:

1. The sub-trees of every node differ in height by at most one.

2. Every sub-tree is an AVL tree.

3. balance(node) = height(node.left) – height(node.right)


AVL Tree

According to Definition not an AVL TREE


AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12
0
3
1
3
0 2
3
2
1 Imbalanced as node 3 is having
2
balancing factor -2
Balanced 0
1
Imbalancing in AVL Tree

2
A 2
A
1
B -1
0 B
0
C
C

Left Imbalance in same direction Left Imbalance in opposite direction


Only Right Rotation Left Rotation Then Right Rotation
2
0 A
B 0
1 C
C 0 0
0 C 0
A 0 B A
B
Imbalancing in AVL Tree

-2
-2
A A
-1 1
B B

0 0
C C

Right Imbalance in same direction Right Imbalance in opposite direction


Only Left Rotation Right Rotation Then Left Rotation
-2
0 A
B 0
-1 C
0 0 C
A C 0 A 0
B
0
B
Imbalancing in AVL Tree
X f

p Left Rotation
A
q = right (p) // q = &B
B q Hold = left (q ) // Hold = &Y
left (q ) = p // &B -> left = A
right (p) = Hold //right (p) = &Y
Y C right (f) / left(f) = q //&X -> left = &B

1. q = right (p) // q = &B


4. right (p) = Hold //right (p) = &Y
q= B
B q
2. Hold = left (q ) // Hold = &Y X f

Hold = Y A p C
B q
3. left (q ) = p // &B -> left = A
Y A p C
B q

p A C 5. right (f) / left(f) = q //&X -> left = &B Y


Imbalancing in AVL Tree
X f

p Right Rotation
A
q = left (p) // q = &B
B q Hold = right (q ) // Hold = &Y
right (q ) = p // &B -> right = A
left (p) = Hold //left (p) = &Y
C Y right (f) / left(f) = q //&X -> left = &B

1. q = left (p) // q = &B


4. left (p) = Hold //left (p) = &Y
q= B
B q
2. Hold = right (q ) // Hold = &Y X f

Hold = Y C A p
B q
3. right (q ) = p // &B -> right = A
Y
B q C A p

C A p 5. right (f) / left(f) = q //&X -> left = &B Y


AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12

0
3

1
3
0 2 3
2
1 Imbalanced as node 3 is having
2
balancing factor 2
Balanced 0
1

0
After Right Rotation 2
0 0
1 3
AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12

2 0 -1 -2
2 2
0 0 0 -1 0 -2
1 3 1 3 1 3
0 -1
4 4
-1
2 0
6
0 0
1 4
0 Imbalanced, Rotation should be given
0 with respect to nearest ancestor
3 6
AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12
0
-2 4
2 Imbalanced again, Rotation should be
0 given with respect to nearest ancestor 0 -1
-1 2 6
1 4 0 0 0
0 -1 1 3
3 6 7

7 0 -1
4
0 -2
2 6
0 0
1 -1
3 7
0
Imbalanced again, Rotation should be 9
given with respect to nearest ancestor
AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12

Imbalanced again, Rotation should be


given with respect to nearest ancestor
0
4
-1
0
0 4
2 7 0
0 0 -1
1 0 2
3 6 9 7
0 0
1 -1
3 6 9
0
10
AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12

Imbalanced again, Rotation should be


given with respect to nearest ancestor

-2 -1
4 4
0 0
-2 -1
2 2 7
7
0 0 0 0
0
1 -2 1 3
3 6 9 6 10
-1 0 0
10 9 11
0
11
AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12

Imbalanced again, Rotation should be


given with respect to nearest ancestor
-2 -1
4 4
0 0
-2 0
2 7 2 10
0 0 -1 0 0
0 -1
1 3 1
6 10 3 7 11
0 -1 0
0 0
9 11 6 13
9
0
13
AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12

Imbalanced again in opposite direction,


Rotation should be given with respect to
nearest ancestor -2
4 4
0 0
-1 -1
10 2 10
2
0 0 0 0
-2 0 -2
0 1
1 3 7 11 3 7 11
1 0 -1
0 0 0
6 6 12
9 13 9

0 0
12 13
AVL Tree Insertion

Data : 3 2 1 4 6 7 9 10 11 13 12

Final AVL TREE

-1
4
0
0
2 10
0
0
1 3
0
0
12
7
0 0
0 11 13
6
9
Advantages of AVL Tree
For General BST

Algorithm Average Worst Case


Space O(n) O(n)
Search O(log n) O(n)
Insert O(log n) O(n)
Delete O(log n) O(n)

For AVL Tree

Algorithm Average Worst Case


Space O(n) O(n)
Search O(log n) O(logn)
Insert O(log n) O(logn)
Delete O(log n) O(logn)
Assignment 6

1. Create an AVL tree with following data

14, 77, 50, 45, 12, 17, 78, 81, 66, 55, 89, 52

2. What will be the changes if instead of 52 , last number is 80 ?

( Every Program should be properly documented )

• Covers LO1 and LO2


Self Learning
For Assignment 6

1. Data Structure Using C & C++ : Langsum, Augenstein , Tanenbaum ,


Second Edition, Prentice Hall of India

2. Data Structures : Seymour Lipschutz , Indian Adapted Edition


2006, Tata McGaw Hill Education Pvt Ltd

3. https://www.youtube.com/watch?v=-9sHvAnLN_w
RobEdwardsSDSU – AVL 1 Introduction

4. https://www.youtube.com/watch?v=7m94k2Qhg68
RobEdwardsSDSU - AVL Tree 7 complete example of adding
data to an AVL tree

You might also like