0% found this document useful (0 votes)
46 views20 pages

Data Structures: Sohail Aslam

The document discusses AVL tree balancing through examples of inserting nodes into an AVL tree. It shows how single rotations can restore balance when nodes are inserted into the left-left or right-right subtrees, but not when inserted into the left-right or right-left subtrees. These latter cases require double rotations to rebalance the tree. It identifies the four cases where rebalancing is needed and how single and double rotations can address each case.

Uploaded by

M NOOR MALIK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views20 pages

Data Structures: Sohail Aslam

The document discusses AVL tree balancing through examples of inserting nodes into an AVL tree. It shows how single rotations can restore balance when nodes are inserted into the left-left or right-right subtrees, but not when inserted into the left-right or right-left subtrees. These latter cases require double rotations to rebalance the tree. It identifies the four cases where rebalancing is needed and how single and double rotations can address each case.

Uploaded by

M NOOR MALIK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Data Structures

Lecture 21
Sohail Aslam

1
AVL Tree Building Example

Insert(3) single left rotation


1 -2

2
AVL Tree Building Example

Insert(3)
2

1 3

3
AVL Tree Building Example

Insert(4)
2

1 3

4
AVL Tree Building Example

Insert(5)
2

1 3 -2

5
AVL Tree Building Example

Insert(5)
2

1 4

3 5

6
AVL Tree Building Example

Insert(6)
2 -2

1 4

3 5

7
AVL Tree Building Example

Insert(6)
4

2 5

1 3 6

8
AVL Tree Building Example

Insert(7)
4

2 5 -2

1 3 6

9
AVL Tree Building Example

Insert(7)
4

2 6

1 3 5 7

10
AVL Tree Building Example

Insert(16)
4

2 6

1 3 5 7

16

11
AVL Tree Building Example

Insert(15)
4

2 6

1 3 5 7 -2

16

15

12
AVL Tree Building Example

Insert(15)
4

2 6

1 3 5 16 -2

15

13
Cases for Rotation

 Single rotation does not seem to restore


the balance.
 The problem is the node 15 is in an inner
subtree that is too deep.
 Let us revisit the rotations.

14
Cases for Rotation

 Let us call the node that must be


rebalanced .
 Since any node has at most two children,
and a height imbalance requires that ’s
two subtrees differ by two (or –2), the
violation will occur in four cases:

15
Cases for Rotation

1. An insertion into left subtree of the left


child of .
2. An insertion into right subtree of the left
child of .
3. An insertion into left subtree of the right
child of .
4. An insertion into right subtree of the right
child of .

16
Cases for Rotation

 The insertion occurs on the “outside”


(i.e., left-left or right-right) in cases 1 and
4
 Single rotation can fix the balance in
cases 1 and 4.
 Insertion occurs on the “inside” in cases
2 and 3 which single rotation cannot fix.

17
Cases for Rotation

 Single right rotation to fix case 1.


 k2  k1

k1 k2
Z Level n-2 X

X Y Level n-1 Y Z
new

Level n
new

18
Cases for Rotation

 Single left rotation to fix case 4.


k1 k2

k2 k1
X Level n-2

Z
Y Level n-1
X Y

Z
Level n

19
Cases for Rotation

 Single right rotation fails to fix case 2.


 k2  k1

k1 k2
Z Level n-2 X

X Y Level n-1 Y Z

Level n
new new

20

You might also like