Professional Documents
Culture Documents
WINSEM2019-20 STS5102 SS VL2019205000260 Reference Material I 06-Jan-2020 AVL and Red Blac
WINSEM2019-20 STS5102 SS VL2019205000260 Reference Material I 06-Jan-2020 AVL and Red Blac
5 9
Because difference between heights of left
and right subtrees for every
4 6 1
node is EQUAL TO -1 , 0 , 1
3
Not AVLTree 7
5 9
2
Why AVL Tree?
1. Most of the BST operations like search, max, min, insert, delete etc.. will take
O(h) time where h is the height of the BST.
2. The cost of these operations may become O(n) for a skewed Binary tree.
3. Make sure that height of the tree remains O(Logn) after every insertion and
deletion, then an upper bound of O(Logn) for all these operations are
guaranteed.
4. The height of an AVL tree is always O(Logn) where n is the number of nodes
in the tree.
AVLTree Rotations
To balance itself, an AVL tree may perform the following four kinds of
rotations :
1. Left rotation
2. Right rotation
3. Left-Right rotation
4. Right-Left rotation
Left Rotation
C
Left Rotation
C
Left Rotation
A C
Right Rotation
A
Right Rotation
A
Right Rotation
A C
Left Right Rotation
B
Left Right Rotation
B
Left Right Rotation
A
Left Right Rotation
A
Left Right Rotation
A C
Right Left Rotation
B
Right Left Rotation
B
Right Left Rotation
C
Right Left Rotation
C
Right Left Rotation
C
A
AVL Tree
14
Insert 15
14
15
Insert 16
14
15
16
Left Rotation
14
15
16
Left Rotation 15
14 16
Insert 13 15
14 16
13
Insert 12 15
14 16
13
12
Right Rotation 15
14 16
13
12
Right Rotation 15
13 16
12 14
Insert 11 15
13 16
12 14
11
Right Rotation 15
13 16
12 14
11
Right Rotation 13
12 15
11 14 16
Insert 10 13
12 15
11 14 16
10
Right Rotation 13
12 15
11 14 16
10
Right Rotation 13
11 15
14 16
10 12
AVL Tree
Double rotations : 1, 2, 3, 4
Insert 1
13
11 15
10 12 14 16
1
Insert 2
13
11 15
10 12 14 16
2
Left Right Rotate
13
11 15
10 12 14 16
2
Left Right Rotate
13
11 15
2 12 14 16
1 10
Insert 3 13
11 15
2 12 14 16
1 10
3
Left Right Rotation
13
11 15
2 12 14 16
1 10
3
Left Right Rotation
13
10 15
2 11 14 16
12
1 3
Insert 4 13
10 15
2 11 14 16
12
1 3
4
Right Left Rotation
13
10 15
2 11 14 16
12
1 3
4
Right Left Rotation
10
2 13
1 3 11 15
4 12 14 16
Application
1. Low cost
2. Efficient
3. Easy to access
4. Easy to Understand
Disadvantage
- Search O(log n)
- Deletion O(log n)
Real time problem
Telephone EMP Name City
Number
4) Direct Access Table 8976543634 John Chennai
- Search O(1)
Limitation:
- Deletion O(1) 1)Size of table: (m * 10 ^ n)
m -> size of the pointer to the record
n -> digits in the telephone number