You are on page 1of 54

Data Structure and Algorithm

Manmath N. Sahoo
Consider the insertion of following element A,
B, C, , ….,X, Y, Z into the BST

A O(N)
B .

C
.
.

X
Y
Z
Consider the insertion of following element Z,
X, Y, , ….,C, B, A into the BST

Z
X
Y
O(N)
C
B
A
Balanced binary tree
• The disadvantage of a binary search tree is that its
height can be as large as N
• This means that the time needed to perform insertion
and deletion and many other operations can be O(N) in
the worst case
• We want a tree with small height
• A binary tree with N node has height at least O(log N)
• Thus, our goal is to keep the height of a binary search
tree O(log N)
• Such trees are called balanced binary search trees.
Examples are AVL tree, red-black tree.
AVL tree
• An AVL tree is a binary search tree in
which
– for every node in the tree, the height of
the left and right subtrees differ by at
most 1.

– An empty binary tree is an AVL tree


AVL tree
TL left subtree of T
h(TL ) Height of the subtree TL
TR Right subtree of T
h(TR ) Height of the subtree TR
T is an AVL tree iff TL and TR are AVL tree and |
h(TL ) - h(TR ) | <= 1

h(TL ) - h(TR ) is known as balancing factor (BF) and


for an AVL tree the BF of a node can be either 0 ,
1, or -1
AVL Search Tree
(-1)
C

(0) A G (1)

D
(0)
Insertion in AVL search Tree
Insertion into an AVL search tree may affect the
BF of a node, resulting the BST unbalanced.

A technique called Rotation is used to restore


the balance of the search tree
AVL Search Tree
(-1)
C

(0) A G (1)

D (0)

E
AVL Search Tree
(-2)
C

(0) A G (2)

D
(-1)

E (0)
Rotation

To perform rotation – Identify a specific node A


whose BF(A) is neither 0, 1, or -1 and which is
the nearest ancestor to the inserted node on the
path from the inserted node to the root
Rotation
Rebalancing rotation are classified as LL, LR, RR
and RL
LL Rotation: Inserted node is in the left sub-tree of
left sub-tree of node A
RR Rotation: Inserted node is in the right sub-tree
of right sub-tree of node A
LR Rotation: Inserted node is in the right sub-tree
of left sub-tree of node A
RL Rotation: Inserted node is in the left sub-tree of
right sub-tree of node A
LL Rotation
(+1) (+2)
A A
(0) Insert X into BL
B (+1) B
AR AR
h c
h+1 h c

BL BR x
X
BL BR

BL : Left Sub-tree of B Unbalanced AVL


BR : Right Sub-tree of B search tree after
insertion
AR : Right Sub-tree of A
h : Height
LL Rotation
(+2)
(0) B
A (0)
(+1) B LL Rotation A
h+1
AR x
BL
c h AR
h+1 h c
x BR
BL BR
Unbalanced AVL
search tree after Balanced AVL
insertion search tree after
rotation
LL Rotation Example
(+1) (+2)
96 96
(0) (0) Insert 36 (+1) (0)
85 110 85 110
(0) (+1)
(0) (0)
64 90 64 90

(0) 36

Unbalanced AVL search


tree
LL Rotation Example
(+2) (0)
96 85
(0) (0)
(+1) (+1)
85 LL Rotation 64 96
110
(+1) (0)
(0) (0) 36 90 (0) 110
64 90

(0) 36

Unbalanced VAL search Balanced AVL search tree


tree after LL rotation
RR Rotation
(-1)
(-2)
A
Insert X A
into BR (-1)
B (0) B
h
AL
AL h c
c h+1

BL BR x
BL BR
Unbalanced AVL
search tree after
insertion
RR Rotation
(-2)
(0)
A
RR Rotation B
(-1)
B (0)
AL A c
c h+1 x h+1
x h
BL BR BL BR
AL
Unbalanced AVL
search tree after Balanced AVL
insertion search tree after
Rotation
RR Rotation Example
(-1) (-2)
34 34
(0) (0) Insert 65 (0) (-1)
26 44 26 44
(0) (0) (0) (-1)
40 56 40 56
(0)
65
Unbalanced AVL search
tree
RR Rotation Example
(-2) (-2)
34 44
(0) (-1) RR
(0) (-1)
26 Rotation
44 34 56
(0) (-1) (0) (0) (0)
40 56 26 40 65
(0) 65

Balanced AVL search tree


after RR rotation
LR Rotation (+2)
A (+1) (-1) A
(0)
B
B (1)
Insert X
into CL C
C (0) h
h
h-1 c
AR
c x
BL CL
BL CL CR AR CR
Unbalanced AVL
search tree after
insertion
LR Rotation (+2)
(+2) (-1) A
(-1) A C
B (1)
(1) B
c
C
AR
x
c
AR CL
BL
x CR
CL CR BL
Unbalanced AVL search AVL tree after Left
tree after insertion rotation at left subtree
LR Rotation (0)
(+2) (0) C
B (-1)
(-1) A
C A
(1)
B x
c
c CL
CR
AR BL
x AR
CL CR Balanced AVL
BL search tree after
LR Rotation
AVL tree after Left
rotation at left subtree
LR Rotation Example
(+1) (+2)
44 44
(0) (0) Insert 37 (-1) (0)
30 76 30 76
(0) (0)
(0) (+1)
16 39 16 39

(0)
37

Unbalanced AVL search


tree
LR Rotation Example
(+2) (0)
44 39
(-1) (0) LR Rotation (0) (-1)
30 76 30 44
(0) (0)
(+1) (0)
16 39 16 37 76
(0)
(0)
37

Balanced AVL search tree


(-1)
RL Rotation
A (0) (-2)

B A (+1)
Insert X
h (0) into CR B
C h (-1)
h C
A h-1 c
L
h
CL CR A h-1 c
BR L
x
CL CR BR

Unbalanced AVL
search tree after
insertion
(-2)
RL Rotation
A (+1) (0)

B C (0)
RL Rotation
h (-1) B
C (+1)
A
h c
ALh-1 c
h-1 x
x h h
CL CR BR CR
CL
AL BR

Balanced AVL search


tree after RL Rotation
RL Rotation Example
(-1) (-2)
34 34
(0) (0) Insert 41 (0) (+1)
26 44 26 44
(0) (0) (-1) (0)
40 56 40 56
(0) 41

Unbalanced AVL search


tree
RL Rotation Example
(-2) (0)
34 40
(0) (+1) RL Rotation (+1) (0)
26 44 34 44
(-1) (0) (0) 26 (0) (0)
40 56 41 56
(0) 41

Balanced AVL search


tree
AVL Tree
Construct an AVL search tree by inserting the
following elements in the order of their
occurrence

64, 1, 14, 26, 13, 110, 98, 85


(+1)
Insert 64, 1 64

(0) 1

(0)
Insert 14 (+2) 14
64 LR (0)
(0)
(-1) 1
1 64
14 (0)
(0)
Insert 26, 13, 110,98 14
(0)
(-1) (0)
1
64
14
(-1)
(-1)
1 (0)
64
13 (+1)
(0) 110
26
(0)
98
Insert 85
(-1)
(-2)
14 LL 14
(-1) (-1)
(-2) (-1)
1 (0) 64 1 (0) 64
(+2) (0)
13 13
(0) 110 (0) 98
26 26 (0)
(+1) (0) 110
98 85

85 (0)
Deletion in AVL search Tree
Deletion in AVL search tree proceed the same
way as the deletion in binary search tree

However, in the event of imbalance due to


deletion, one or more rotation need to be
applied to balance the AVL tree.
AVL deletion
Let A be the closest ancestor node on the path
from X (deleted node) to the root with a
balancing factor +2 or -2

Classify the rotation as L or R depending on


whether the deletion occurred on the left or
right subtree of A
AVL Deletion
Depending on the value of BF(B) where B is the
root of the left or right subtree of A, the R or L
imbalance is further classified as R0, R1 and R -1
or L0, L1 and L-1.
R0 Rotation
(+1) (+2)
A A
(0) Delete node X
B (0) B
h AR AR
x
h c
h c
h -1
BL BR BL BR
Unbalanced AVL
search tree after
deletion of node x
R0 Rotation
(+2)
(-1) B
A (+1)
(0) B R0 Rotation A
AR
BL
c h AR
h c
BF(B) == 0, use
BL BR R0 rotation BR

Unbalanced AVL
search tree after Balanced AVL
deletion of x search tree after
rotation
R0 Rotation Example
(+1) (+2)
46 46
(0) (-1)
Delete 60 (0) (0)
20 54 20
(+1)
54
(-1) (0) (+1)
18 23 60 18 23
(-1)

(0) 7 (0) 24 (0) 24


(0) 7

Unbalanced AVL search


tree after deletion
R0 Rotation Example
(+2) (-1)
46 20
(0) (0)
R0 (+1) (+1)
20 54 18
(+1)
46
(-1) (0) (-1)
18 23 7 23
(0)
54

(0) 7 (0) 24 (0) 24

Balanced AVL search tree


after deletion
R1 Rotation
(+1) (+2)
A A
(+1) B Delete node X
(+1) B
h AR h -1 AR
x
h c
h -1 h c
h -1
BL BR BL BR
Unbalanced AVL
search tree after
deletion of node x
R1 Rotation
(+2)
(0) B
A (0)
(+1) B R1 Rotation A
h -1 AR
BL
c h-1 A
h c
h -1 BF(B) == 1, use R

BL BR R1 rotation BR

Balanced AVL
search tree after
rotation
R1 Rotation Example
(+1) (+2)
37 37
(+1) (+1)
Delete 39 (+1) (0)
26 41 26
(+1)
41
39 (0) (+1)
18 28 18 28
(0)
(0)
(0) 16
(0) 16

Unbalanced AVL search


tree after deletion
R1 Rotation Example
(+2) (0)
37 26
(+1) (0)
R1 Rotation (+1) (0)
26 41 18
(+1)
37
(0) (0)
18 28 (0) 16 28
(0) 41

(0) 16

Balanced AVL search tree


after deletion
R-1 Rotation (+2)
A (+1) (-1) A
(-1)
B
B (0)
Delete X C h-1
C (0) h
h-1
c
h-1 c
x
AR
BL CL
BL CL CR AR CR
Unbalanced AVL
search tree after
deletion
R-1 Rotation (0)
A (+2) (0) C
(-1)
B
B h -1 (0) A
R -1 h -1 h -1
C (0) c
h-1
BL CL
h-1 c
BF(B) == -1, CR AR
BL CL CR AR use R-1 rotation

Balanced AVL
search tree after
Rotation
R-1 Rotation Example
(+1) (+2)
44 44
(-1) (-1) Delete 52 (-1) (0)
22 48 22 48
(0) (0)
(0) 52 (0)
18 28 18 28

23 29 23
(0) 29

Unbalanced AVL search


tree after deletion
R-1 Rotation Example
(+1) (0)
44 28
(-1) (-1) R-1 (0) (0)
22 48 22 44
(0)
(0) (0) (0)
(0) 52 48
18 28 18 23
29
23 29

Balanced AVL search tree


after rotation
L0 Rotation
(-1)
(-2)
A
Delete X A
(0)
B (0) h-1 B
h
AL
x h c
h c
AL BL BR
BL BR
Unbalanced AVL
search tree after
deletion
L0 Rotation
(-1)
(+1)
A
Delete X B
(0)
h -1 B (0) (-1)
h c
AL A
h c BR
h-1
AL BL BR
BL
Balanced AVL
search tree after
deletion
L1 Rotation
(-1)
(-2)
A
Delete X A
(+1)
B (+1) h-1 B
h
(0) AL (0)
c h-1 C h-1c
x C
AL BR BR
h-1
CL CR
Unbalanced AVL
CL CR search tree after
deletion
L1 Rotation
(-2)
(0)
A
C
L1
(0) (0)
B (+1) B
h-1 A
(0)
c h-1 h-1c
C h-1
AL BR BR
h-1 AL CL CR
Unbalanced AVL
CL CR search tree after
deletion
L-1 Rotation
(-1)
(-2)
A
Delete X A
(-1)
B (-1) h-1 B
h h-1 AL h-1
x
c
h c h
AL BL BR
BL BR
Unbalanced AVL
search tree after
deletion
L-1 Rotation
(-2)
(0)
A
Delete X B
(-1)
B (-1)
h-1 h-1 A
c
h c h
AL BL BR h-1 B
AL L BR
Balanced AVL
search tree after
deletion

You might also like