Sei sulla pagina 1di 58

AVL Trees

BCA Sem II

AVL tree

These are self-adjusting, height-balanced binary search trees and are named after the inventors: Adelson-Velskii An AVL tree is a binary search tree in which The heights of the right sub-tree and left sub-tree of the root differ by at most 1 The left sub-tree and the right sub-tree are themselves AVL trees

AVL tree

A node is said to be left-high if the left subtree has greater height , right-high if the right sub-tree has greater height and equal if the heights of the LST and RST are the same. Each node has a balance factor .
Balance factor = height of left tree height of right tree

The balance factor will be 1 for left high , -1 for right high and 0 for balanced node

AVL (Height-Balanced Trees)


A perfectly balanced binary tree is a binary tree such that:
The height of the left and right subtrees of the root are equal The left and right subtrees of the root are perfectly balanced binary trees

AVL Tree Rotations


When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property. This is done using single rotations or double rotations.

AVL Tree Rotations


Reconstruction procedure: rotating tree left rotation and right rotation

Suppose that the rotation occurs at node x


Left rotation: certain nodes from the right subtree of x move to its left sub-tree; the root of the right sub-tree of x becomes the new root of the reconstructed sub-tree Right rotation at x: certain nodes from the left sub-tree of x move to its right sub-tree; the root of the left sub-tree of x becomes the new root of the reconstructed sub-tree

LL Rotation
Inserted node is in the left sub-tree of the left sub-tree of a node
+2 +1 0

LL Rotation Example
+1 96 +2 96

0
85

0 110

Insert 36

+1 85

0 110

0 64 90

+1 64 0 36

0 90

LL Rotation Example
0 85

+1 64 0 36 0 90 96

0
110

RR Rotation

-2 -1 0

RR Rotation Example
-1 -2 34 34

0
26 0 40

0 44

0 26 0 0 56 40 44

-1

-1
56 0 65

RR Rotation Example
0 44

0 34 0

-1
56 0 65

0
26 40

LR Rotation
Inserted node is in the right sub-tree of the left sub-tree of a node
+2
-1 0 +1 0 -1

LR Rotation
44 0 30 0 0 16 16 39 0 37 39 0 76 44

0
30 76

LR Rotation
0
39 0

0
16

30 0 37

44 0

76

RL Rotation
Inserted node is in the left sub-tree of the right sub-tree of a node

RL Rotation Example
(-1) (-2)

34
(0) (0)

34
Insert 41 (0) (+1)

26
(0)

44
(0)

26
(-1)

44
(0)

40

56

40
(0) 41

56 56

Unbalanced AVL search tree

RL Rotation Example
(-2) (0)

34
(0)

40
(+1) RL Rotation (+1) (0)

26
(-1)

44
(0)

34
(0) 26

44
(0)

(0)

40
(0) 41

56

41

56

Balanced AVL search tree

RL Rotation Example 2
-1 G 0 0 D 0 K V 0 M 0 D -1 K T -2 G +1

T
0

0
V

RL Rotation Example 2
0 +1 0 D 0 M K 0 G T

0
V

Single and Double Rotations


LL and RR Rotations are termed as single rotations whereas LR and RL rotations are termed as double rotations

Insert the following nodes in an AVL tree, perform rotations as and when required: 3, 2 , 1,4, 5, 6, 7

AVL Tree Example


Insert 3 3

Insert 2

AVL Tree Example


Insert 1
3 2 Single Rotation 2 1 1 Insert 4 1 2 3

3 4

AVL Tree Example


Insert 5
2 Single Rotation 1 3 1 4 3 5 4 5 2

AVL Tree Example


Insert 6
2 1 Single Rotation 4 5 6 2 1 4 5 6

AVL Tree Example


Insert 7
4 4 5 2 6 1 3 7 1 Single Rotation 2 3 5 6

Deletion
Situation 1: deletion in the right sub-tree of X. The left child of X has a 0 balance. We refer to this situation by R0. Situation 2: deletion in the right sub-tree of X. The left child of X has balance 1. We refer to this situation by R1. Situation 3: deletion in the right sub-tree of X. The left child of X has balance -1. We refer to this situation by R-1.

Deletion
Situation 4 (symmetric to situation 1): deletion in the left sub-tree of X. The right child of X has a 0 balance. We refer to this situation by L0. Situation 5 (symmetric to situation 2): deletion in the left sub-tree of X. The right child of X has a 1 balance. We refer to this situation by L1. Situation 6 (symmetric to situation 3): deletion in the left sub-tree of X. The right child of X has a -1 balance. We refer to this situation by L-1.

An R0 Rotation

Figure 15.7 An R0 rotation (single rotation)

R0 Rotation Example
A 46 B 0 20 +1 BLEFT 18 0 7 24 -1 +1 A _right 54

-1
0 60

B_right 23
0

Delete 60
+2 46 0 20 +1 18 0 7 -1 54

23
0 24

R0 Rotation Example
B -1 BLEFT +1 18 0 7 20 A B_right 46 -1 23

+1
54

0
24

An R1 Rotation

Figure 15.8 An R1 rotation (single rotation)

R1 Rotation
+1 +1 26 +1 18 28 0 37 +1 41

0
39

0 16

Delete 39
+2 +1 26 +1 18 28 37 0 41

0 16

R1 Rotation
0 +1 18 0 16 0 28 41 26 0 37 0

An R-1 Rotation

Figure 15.9 An R-1 rotation (double rotation)

R-1 Rotation
+1 44 -1 22 0 18 0 23 0 29 28 0 -1 48

0
52

Delete 52
+2 44 -1 22 0 18 0 23 0 29 28 0 0 48

R-1 Rotation
+2 28 -1 22 0 18 23 0 44 0 29 0 48 0

Insert the following in an AVL tree: A,B,C,D,E Delete the last two nodes in the order of LIFO

Step 1
0
A

Step 2
-1 A

0 B

Step 3
-2 A

-1 B
0 C

Step 4: Perform Rotation

0 B
0 A 0 C

Step 5

0 B
0 A 0 C

Step 6,7

-2 B
0 A C -1 -2

D 0
E

0 B
0 A 0 C D 0 E 0

0 B
0 A 0 C D 0

RR ROTATION

0 B
0 A C 0

L0 Rotation
(-1)

A
Delete X

(-2)

A
h-1 AL BL B
h (0)

h
x

B (0)

h
BL

AL

BR

BR

Unbalanced AVL search tree after deletion

L0 Rotation
(-2)

A
Delete X

(+1)

B
(-1) (0) h
c

h -1

B (0)

AL A

h
AL BL

BR

h-1 BL

BR

Balanced AVL search tree after deletion

L1 Rotation
(-1)

A
Delete X

(-2)

A
h-1 AL (0) B
(+1)

h
x

B (+1)
(0)

AL

C h-1

h-1

C h-1 c
BR
Unbalanced AVL search tree after deletion

BR CL

CR

CL

CR

L1 Rotation
(-2)

A
L1

(0)

C
(0)

h-1 AL

B (+1)
(0)

A h-1 AL

(0)

C h-1 CL CR

h-1

h-1 c

BR

CL CR

BR

balanced AVL search tree after deletion

L-1 Rotation
(-1)

A
Delete X

(-2)

A
h-1 AL B
h-1
c

h
x

B (-1) h-1
c

(-1)

h
BL

AL

BL

BR

BR

Unbalanced AVL search tree after deletion

L-1 Rotation
(-2)

A
Delete X

(0)

B
(-1)

h-1 AL

B (-1) h-1
c

A
c

h
AL h-1 B
L

BL

BR

BR

Balanced AVL search tree after deletion

Important Observations
LL rotation and R1 rotation is identical LL rotation and R0 rotation differ only in the final balance factor LR and R-1 is identical

Potrebbero piacerti anche