2. Consider the insertion of following
element A, B, C, , ….,X, Y, Z into the
BST
A
B
C
X
Y
Z
O(N)
3. Consider the insertion of following
element Z, X, Y, , ….,C, B, A into the
BST
Z
X
Y
C
B
A
O(N)
4. Balanced binary tree
• The disadvantage of a binary search tree is that its
height can be as large as N-1
• 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 (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.
5. AVL tree
Height of a node
• The height of a leaf is 1. The height of
a null pointer is zero.
• The height of an internal node is the
maximum height of its children plus 1
6. 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
7. 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
9. 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 he balance of the search tree
12. 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
13. 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
14. LL Rotation
BL : Left Sub-tree of B
BR : Right Sub-tree of B
AR : Right Sub-tree of A
h : Height
A
B
c
AR
h
BL BR
(0)
(+1)
Insert X into BL
A
B
c
AR
h
BL BR
(+1)
(+2)
h+1
x
Unbalanced AVL
search tree after
insertion
15. LL Rotation
LL Rotation A
AR
c h
BR
(0)
B
BL
(0)
h+1
x
Balanced AVL
search tree after
rotation
A
B
c
AR
h
BL BR
(+1)
(+2)
h+1
x
Unbalanced AVL
search tree after
insertion
19. RR Rotation
RR Rotation B
A
c
BL BR
(0)
(0)
h+1
x
Balanced AVL
search tree after
Rotation
AL
A
B
c
BL BR
(-1)
(-2)
h+1
x
Unbalanced AVL
search tree after
insertion
AL
h
25. LR Rotation Example
LR Rotation(-1)
(+2)
44
30 76
16 39
(+1)
(0)
(0)
(0)
(0)
39
30 44
16 37
(0)
(-1)
(0)
76
Balanced AVL search tree
(0)
37
(0)
26. RL Rotation
Insert X
into CR
h
B
C
cAL
CL CR
(0)
(0)
h
A
(-1)
BR
h
B
C
cAL
CL CR
(-1)
(+1)
h
A
(-2)
x
Unbalanced AVL
search tree after
insertion
BR
27. RL Rotation
RL Rotation
h
B
C
cAL
CL CR
(-1)
(+1)
h
A
(-2)
BR
x
Balanced AVL search
tree after RL Rotation
x h
B
A
c
AL
CL
CR
(+1)
(0)
h
C
(0)
BR
34. 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.
35. 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
36. 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.
38. R0 Rotation
R0 Rotation
Balanced AVL
search tree after
rotation
A
B
c
AR
h
BL BR
(0)
(+2)
Unbalanced AVL
search tree after
deletion of x
A
AR
c h
BR
(+1)
B
BL
(-1)
BF(B) == 0, use
R0 rotation
41. R1 Rotation
A
B
c
AR
h -1
BL BR
(+1)
(+1)
Delete node X
h
x
h
A
B
c
AR
h
BL BR
(+1)
(+2)
Unbalanced AVL
search tree after
deletion of node
x
h -1
h -1
42. R1 Rotation
R1 Rotation
Balanced AVL
search tree after
rotation
A
B
c
AR
h -1
BL BR
(+1)
(+2)
A
AR
c h-1
BR
(0)
B
BL
(0)
BF(B) == 1, use
R1 rotation
h
h -1
45. R-1 Rotation
Delete X
B
C
c
BL CL CR
(0)
(-1)
h-1
A
h
(+1)
AR
x
A
B
C
c
CL CR
(0)
(-1)
Unbalanced AVL
search tree after
deletion
BL
(+2)
AR
h-1
46. R-1 Rotation
R -1
B
C
c
BL CL CR
(0)
(-1)
h-1
A
h -1
(+2)
AR
C
B
A
c
CL
CR
(0)
(0)
Balanced AVL
search tree after
Rotation
BL
(0)
AR
h -1 h -1
BF(B) == -1,
use R-1 rotation