0% found this document useful (0 votes)
18 views

Unit IV - AVL Tree at CSJMU - 6 Slides Handouts

Uploaded by

raghvendrac210
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unit IV - AVL Tree at CSJMU - 6 Slides Handouts

Uploaded by

raghvendrac210
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Unit - IV

AVL (Adelson-Velskii & Landi) Tree


 Also known as Height Balanced Tree
AVL Trees  Def: Let T be a non-empty binary search tree with
TL & TR as its left & right subtree. T is height
balanced iff
 (1) TL & TR are height balanced
 (2) |hL – hR| ≤ 1, where hL & hR are heights of TL & TR
respectively.
Dr. Rabins Porwal
 Balance Factor: The balance factor of a node n in
a binary tree is defined to be hL – hR, where hL &
hR respectively, are the heights of left & right
subtrees of n.

Dr. Rabins Porwal 1 AVL Trees Unit IV. 2 Dr. Rabins Porwal

1 2

Balance Factors: Example


-1
A  Representation:
0 +1 struct AVLNode
{
B C
int data;
0 0 -1 0 struct AVLNode * left, * right;
D E F G int balancefactor;
}
0
H  For any node n in an AVL tree bf(n) = -1, 0
or 1.
 Note: The ht. of null tree is defined as -1.

AVL Trees Unit IV. 3 Dr. Rabins Porwal AVL Trees Unit IV. 4 Dr. Rabins Porwal

3 4

AVL Tree: Advantages Insertion in AVL Trees

 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

Prepared by: Dr. Rabins Porwal 1


Unit - IV

Rotations after insertion in AVL Tree LL Rotation


 This type of rotation is performed when new
 The tree is made balanced by performing node y is inserted in the left subtree of the left
some rotations: subtree of node A. Unbalanced
 Single rotations 1 2 0
A A B
 LL Rotation
0 1 LL 0
Insertion
 RR Rotation B AR B AR B′L A
h h h+1
 Double Rotations BL BR B′L BR BR AR
 LR Rotation h h h+1 h h h
After insertion of the
 RL Rotation node in the left subtree
of left subtree of A

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

After inserting into BR

AVL Trees Unit IV. 11 Dr. Rabins Porwal AVL Trees Unit IV. 12 Dr. Rabins Porwal

11 12

Prepared by: Dr. Rabins Porwal 2


Unit - IV

 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

After inserting into BL

AVL Trees Unit IV. 15 Dr. Rabins Porwal AVL Trees Unit IV. 16 Dr. Rabins Porwal

15 16

Example More Examples

 50, 60, 55  50, 40, 35, 58, 48, 42, 60, 30, 33, 25

 10, 20, 30, 42, 28, 29, 50

 10, 20, 32, 35, 25, 27

AVL Trees Unit IV. 17 Dr. Rabins Porwal AVL Trees Unit IV. 18 Dr. Rabins Porwal

17 18

Prepared by: Dr. Rabins Porwal 3

You might also like