This document discusses balanced binary search trees (BSTs), specifically AVL trees. It explains that AVL trees ensure insertions and deletions are O(log N) by keeping the height difference between left and right subtrees no more than 1. It covers the four types of rotations (LL, RR, LR, RL) used to rebalance the tree after insertions or deletions. Deletions can also cause imbalance and require L, R, L0, L1, R0, R1, L-1, R-1 rotations depending on the balancing factors. Examples are provided for each type of rotation.
Consider the insertionof following
element A, B, C, , ….,X, Y, Z into the
BST
A
B
O(N)
C
X
Y
Z
3.
Consider the insertionof 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 ofa 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
• AnAVL 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 leftsubtree 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
Insertion in AVLsearch 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
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 areclassified 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 Xinto 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
Deletion in AVLsearch 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 Abe 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 onthe 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.
R1 Rotation
(+1)
(+2)
A
Delete nodeX
(+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
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