2. Consider the insertion of following
element A, B, C, , âŠ.,X, Y, Z into the
BST
A
B
O(N)
C
X
Y
Z
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
(+1)
(+2)
A
(0)
BL
B
h
A
AR
Insert X into BL
c
BR
BL : Left Sub-tree of B
BR : Right Sub-tree of B
AR : Right Sub-tree of A
h : Height
(+1) B
h+1
x
BL
h
AR
c
BR
Unbalanced AVL
search tree after
insertion
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.
41. R1 Rotation
(+1)
(+2)
A
Delete node X
(+1) B
h
BL
A
h AR
c
x
h -1
BR
(+1) B
h
BL
h -1 AR
c
BR
h -1
Unbalanced AVL
search tree after
deletion of node
x
42. R1 Rotation
(+2)
(0)
A
(+1) B
h
BL
h -1 AR
c
BR
h -1
B
(0)
A
R1 Rotation
BF(B) == 1, use
R1 rotation
BL
c
BR
h-1 A
R
Balanced AVL
search tree after
rotation
46. R-1 Rotation
(-1)
A (+2)
B
h-1
h -1
C (0)
BL
CL
CR
AR
(0)
B
R -1
h -1
BL
c
(0)
BF(B) == -1,
use R-1 rotation
CL
C
(0)
c
A
h -1
CR AR
Balanced AVL
search tree after
Rotation