Unit IV - AVL Tree at CSJMU - 6 Slides Handouts
Unit IV - AVL Tree at CSJMU - 6 Slides Handouts
Dr. Rabins Porwal 1 AVL Trees Unit IV. 2 Dr. Rabins Porwal
1 2
AVL Trees Unit IV. 3 Dr. Rabins Porwal AVL Trees Unit IV. 4 Dr. Rabins Porwal
3 4
Since AVL trees are height balanced trees, While constructing an AVL Tree, a new node
operations like insertion and deletion have can be inserted at any location as per the
low time complexity. property of binary search tree and thus tree
Use of AVL trees increase the efficiency of may become unbalanced.
the programs. To make the tree balanced, some rotations
are required to perform. These rotations are
characterized by the nearest ancestor, A, of
the inserted node y, whose balance factor
becomes ± 2.
AVL Trees Unit IV. 5 Dr. Rabins Porwal AVL Trees Unit IV. 6 Dr. Rabins Porwal
5 6
AVL Trees Unit IV. 7 Dr. Rabins Porwal AVL Trees Unit IV. 8 Dr. Rabins Porwal
7 8
Example RR Rotation
This type of rotation is performed when new
50, 40, 30 node y is inserted in the right subtree of the
right subtree of node A. Unbalanced
-1 -2 0
A A B
0 Insertion -1 RR 0
AL B AL B A B′R
h h h+1
BL BR BL B′R AL BL
h h h h+1 h h
After insertion of the
node in the right subtree
of right subtree of A
AVL Trees Unit IV. 9 Dr. Rabins Porwal AVL Trees Unit IV. 10 Dr. Rabins Porwal
9 10
Example LR Rotation
This type of rotation is performed when new
50, 60, 70 node y is inserted in the right subtree of the
left subtree of node A. Unbalanced
1 2 b
A A C
0 Insertion -1 LR ? ?
B AR B AR B A
h h
b
BL BR BL C BL CL CR AR
h h h h h
CL CR
AVL Trees Unit IV. 11 Dr. Rabins Porwal AVL Trees Unit IV. 12 Dr. Rabins Porwal
11 12
If b = 0, 50, 40, 45
bf(B) = 0 & bf(A) = 0, after rotation
If b = 1,
bf(B) = -1 & bf(A) = 0, after rotation
If b = -1,
bf(B) = 0 & bf(A) = 1, after rotation
AVL Trees Unit IV. 13 Dr. Rabins Porwal AVL Trees Unit IV. 14 Dr. Rabins Porwal
13 14
RL Rotation
This type of rotation is performed when new
node y is inserted in the left subtree of the If b = 0,
right subtree of node A. Unbalanced bf(A) = 0 & bf(B) = 0, after rotation
-1 -2 b
If b = 1,
A A C
bf(A) = -1 & bf(B) = 0, after rotation
0 Insertion 1 RL ? ?
AL B AL B A B If b = -1,
h h
b bf(A) = 0 & bf(B) = 1, after rotation
BL BR C BR AL CL CR BR
h h h h h
CL CR
AVL Trees Unit IV. 15 Dr. Rabins Porwal AVL Trees Unit IV. 16 Dr. Rabins Porwal
15 16
50, 60, 55 50, 40, 35, 58, 48, 42, 60, 30, 33, 25
AVL Trees Unit IV. 17 Dr. Rabins Porwal AVL Trees Unit IV. 18 Dr. Rabins Porwal
17 18