Professional Documents
Culture Documents
Trees
Compiled by :
Surya Prakash Pandit 232/05
Gaurav Gupta 20/05
IIIrd I.T.
Binary Search Trees
(reminder)
Each tree node contains a value.
For every node,
its left subtree contains smaller values, and
its right subtree contains larger values.
The time complexity of a search operation is proportional to
the tree’s depth.
The good case: If the tree is balanced, then every operation
takes O(logn) time.
The bad case: The tree might get very unbalanced.
For example, when inserting ordered numbers to the tree,
the resulting height will be exactly n.
AVL Trees 2
Balanced Search Trees
:
Balanced Tree: A binary tree in which the heights of the two
sub trees of every node differ by at most 1.
Several Varieties
AVL trees
Red-Black trees
B-Trees (used for searching secondary memory)
AVL Trees 3
Balanced Search Trees
(Contd…)
Nodes are added and deleted so that the height of the tree
is kept under control.
AVL Trees 4
AVL Trees :
Motivation: We want to guarantee O(log n) running
time on searching/insertion/deletion operations.
AVL Trees 6
Some AVL Trees :
-1 Balance value
0
0 0 1 -1
0 0 -1
AVL Trees 7
Depth of an AVL
tree
Theorem: Any AVL tree with n nodes has height less
than 1.441 log n.
AVL Trees 12
AVL Trees: Search,
Insertion
AVL tree Search :is the same as BST Search.
Allocate a node and insert the item at the end of the path
(as for BST)
balance factor of new node is 0
AVL Trees 14
Insertion (contd…) :
Let x be the deepest node where an imbalance occurs.
k2 k1
Case 2: The left Case 3:
subtree is higher The
k1 k2
than the right symmetric
subtree, and this is case to case
R R
caused by the 2
P P
right subtree of 16
Q Q
the leftLR
child. RL
1) Simple Insertion :
#'s are
-1 Balance -1
Factors
1 0 1 0
0 1 -1 0 1 0
0
0 0 0 0 0 0
No Rotation Required
AVL Trees 17
2) Insertion with Right
Rotation :
-1 -1
0
LL
1 0 0 0
22 Right Rotation
0 1 0 0 0 1 0
1
0 0 0 0 0 0 0
k2
k1
8 16 4 16
k2
k1 C LL
A
4 10 14 Right Rotation 2 8 14
B C
A B
2 6 1 6 10
1
AVL Trees 19
3) Insertion with Left
Rotation:
-1
-2 0
0 0 RR
-1 0 -1
Left Rotation
0 0 1 0
-1 0 1 0 -1
0 0 0
-1 0 0 0 0
-1
-2 Double Rotation Needed (RL) :
AVL Trees 21
(i) The Right Rotation :
-2
-2
1 1
1 -2
0 -1 -1
0 0 0
0 1 0
1 -1
0
0 0
22
(ii) The Left Rotation :
-2
0
1 -2
1
0
0 0 0
1 0 1 -1
1 -1
0 0 0
0 0
23
In nutshell (RL
: Insertion)
-1 0
-2
1 1
10 0
On balancing
0 0 -1 1 0 -1
-1 1
0 10 0 0 0 0
AVL Trees 24
Example # (LR Insertion)
12 12 12
k3 k3 k2
8 16 8 16 6 16
k2
k1 k1 k3
4 10 14 6 10 14
D k1 D
4 8 14
k2
2 6 4
A
2 5 B 10 D
5 2 5 A
B A B
AVL Trees 25
Left rotation on 4 Right rotation on 8
Extended Example # 1
:
Insert : 3 , 2 , 1 , 4 , 5 , 6 , 7 , 16
1 4
Balancing by 5
Right Rotation
Balancing by
(LL)
Left Rotation
(RR) AVL Trees 26
Extended Example # 1
:
Insert : 3 , 2 , 1 , 4 , 5 , 6 , 7 , 16
1 3
6
5
AVL Trees 27
Extended Example # 1
:
Insert : 3 , 2 , 1 , 4 , 5 , 6 , 7 , 16
1 4
3 5
7
6
Balancing node 2
by Left
AVLRotation
Trees 28
(RR)
Extended Example # 1
:
Insert : 3 , 2 , 1 , 4 , 5 , 6 , 7 , 16
2 5
1 6
3
16
7
Balancing node 5
Ans.
by Left
AVLRotation
Trees 29
(RR)
Extended Example # 2
:
Insert : 64 , 1 , 14 , 26 , 13 , 110 , 98 , 85
64
Since node 64 is
imbalanced, thus by
1 LR Insertion
14
AVL Trees 30
Extended Example # 2
:
Insert : 64 , 1 , 14 , 26 , 13 , 110 , 98 , 85
64
13 14 26 110
31
98
Inserting 85 :
14
1
64
Performing
Right Rotation
(LL)
13 26 110
98
Node 110 becomes
imbalanced.
85 Ans.
32
AVL Trees : Deletion
AVL tree Deletion: same as BST deletion.
AVL Trees 34
1) Deletion by R(0) Rotation :
Delete node 60
Deletion is to be done on the right side of
root node A(46) and BF(B)=0
A(+2)
(+1) A (+2)
(-1) B
46 46
B (0) 20
BB(0)
(0) RightDelete
Rotation
60 R(0) (+1)A
20 20 54
54 18 46
60 60
18 23 7 18 23
23 54
7 7
24 24
AVL Trees 35
2) Deletion by R(1) Rotation :
Delete node 39
Deletion is to be done on the Right side of
root node A(37) and BF(B)=+1
A (+1)
AA(+2)
(+1) (0) B
37 B 37
26
RightDelete
Rotation
B (+1) 39 R(1)
(+1) (0)
B 26 41 26 41
A
18 37
18 28 39 18
16 28 39
28 41
16 16
AVL Trees 36
3) Deletion by R(-1) Rotation :
Delete node 52
Deletion is to be done on the Right side of
root node A(44) and BF(B)=-1
A (+1) A (+1)
44 44
B (-1) B (-1) 28
Right Rotation R(-1)
Delete 52
22 22 48
48 (0) (0)
B
22 44 A
52 18 52
18 28 28
18 29
23 48
23 23
29 29
AVL Trees 37
Deletion Examples :
Delete elements 120,64,130 from the
following search tree.
128 (+1)
(0)
98 (-1)
135
26 85 99 120
(0) (0) (0) (0)
AVL Trees 38
Delete 120 :
128 (+1)
(0)
98 (-1)
135
26 85 99 120
(0) (0) (0)
Ans.
AVL Trees 39
Delete 64 :
128 (+1)
(0)
98 (-1)
135
(0)
(1) 64 110 (-1) 130 (0)
26 85 99
(0) (0) (0)
Ans.
AVL Trees 40
Delete 130 :
128 (+2)
(+1)
(0)
98 (-1)
135
26 99
(0) (0)
Node 128 becomes unbalanced BF(128)=+2
So we Follow R(0) Rotation. 41
Delete 130 :
128 (+2)
98 (-1)
(0) (+1)
R(0) Rotation 98 85 (-1) (+1)
135 128
(1) (0)
85 (-1) (-1)
110 110
26
(0) 135
26 99
(0) 99
(0) (0) Ans.
Node 128 becomes unbalanced BF(128)=+2
So we Follow R(0) Rotation. 42
Example : Insertion +
Deletion
Ques 1(a). Insert the following keys in
order shown to construct an AVL search
tree
A, B, C, D, E
Soln.
AVL Trees 43
Solution.
Insert A, B
AVL Trees 44
Solution.
Now Insert C
C
AVL Trees 45
Solution.
Now Insert C
B
RR Rotation
B
A C
D
AVL Trees 47
Solution.
Now insert E
B
C
A
A D
C E
AVL Trees 49
Solution.
Delete E :
B
A D
C E
AVL Trees 50
Solution.
Delete D :
B
A D
C
Ans.
AVL Trees 51
Special Thanks to:
AVL Trees 52