foundation of computing systems
DESCRIPTION
Foundation of Computing Systems. Lecture 8 Trees: Part V. Height Balanced Binary Trees. Average search time in a binary search tree A binary search tree should be with a minimum value of average search time. t i = Number of comparisons for the i- th element - PowerPoint PPT PresentationTRANSCRIPT
Foundation of Computing Systems
Lecture 8
Trees: Part V
12.08.09 IT 60101: Lecture #8 2
Height Balanced Binary Trees
• Average search time in a binary search tree
• A binary search tree should be with a minimum value of average search time
n
n
ii
1
i = Number of comparisons for the i-th element
n = Total number of elements in the binary search tree
12.08.09 IT 60101: Lecture #8 3
Some Sample Binary Search Trees
A skew ed b ina ry tree ob ta ined from lexicog raph ica l o rde r o f da ta
apraug
decfeb
janju l
junm ar
m aynov
oct
sep
= 6.50
12.08.09 IT 60101: Lecture #8 4
Some Sample Binary Search Trees
= 4.00
jun
ju l
jan
feb
dec
aug
apr
m ar
m ay
nov
oct
sep
A b ina ry search tree ( ha lf skew ed ve rs ion )
jan
feb m ar
apr m ayjun
ju laug
dec
sep
oct
nov
A b ina ry search tree (ob ta ined by inse rting the
da ta in to the o rde r o f m on ths ).
= 3.50
12.08.09 IT 60101: Lecture #8 5
Some Sample Binary Search Trees
= 3.08 = 3.08
jan
feb
m ar
apr
m ayjun
ju laug
dec
sep
oct
nov
B ina ry sea rch tree in the fo rm o f a com p le teb ina ry tree
jan
feb
m ar
apr
m ay
jun
ju l
aug
dec
sep
oct
nov
A b ina ry sea rch tree ob ta ined from a g iven
random o rdering o f da ta .
12.08.09 IT 60101: Lecture #8 6
Some Sample Binary Search Trees
= 3.16
jan
feb
m ar
apr
m ayjun
ju laug
dec
sep
oct
nov
A b ina ry sea rch tree ob ta ined by a spec ia l techn ique
12.08.09 IT 60101: Lecture #8 7
Height Balanced Binary Trees
• How to find a binary search tree with a minimum value of average search time ?
• Solution• Height balanced binary search tree
• Also called AVL tree
• Concept of balance factor of a node
bf = Height of the left sub-tree (hL) – Height of the right sub-tree (hR)
12.08.09 IT 60101: Lecture #8 8
Height Balanced Binary Trees
• Definition
A binary search tree is said to be height balanced binary search tree
if all its nodes have a balance factor of 1, 0 or –1.
That is, for all nodes
|bf| = |hL – hR| ≤ 1
12.08.09 IT 60101: Lecture #8 9
Height Balanced Binary Trees: Example
6
2
1
3
4 7
8
1
0
1
10 0
(a) Height balanced
6
2
1
3
4
5
8
2
0
0
00
0
(b) Height unbalanced
12.08.09 IT 60101: Lecture #8 10
Managing Unbalanced BST
• Steps
• Insert node into a binary search tree
• Compute the balance factors
• Decide the pivot node
• Balance the unbalance tree
12.08.09 IT 60101: Lecture #8 11
Unbalanced to Balanced BST
• AVL Rotations• G.M. Adelson-Velskii and E.M. Lendis (1962)
• Case 1: Left-to-Left rotation• pivotleft-sub treeleft child
• Case 2: Right-to-Right rotation• pivotright-sub treeright child
• Case 3: Left-to-Right rotation• pivotleft-sub treeright child
• Case 4: Right-to-Left rotation• pivotright-sub treeleft child
12.08.09 IT 60101: Lecture #8 12
Left-to-Left Rotation
P
A
A A
P
L R
R
P
A
A
A P
L
R R
A fter ro ta tionA fte r insertion inthe le ft sub-tree o fthe le ft ch ild o f p ivo t node (P )
12.08.09 IT 60101: Lecture #8 13
Left-to-Left Rotation
6
3
4 7
81
1
0
0 0
5
0
0
0
9
0
11
106
3
4 7
81
1
0
0 0
5
0
0
0
9
0
11
10
2
2
1
1
2
0
(b) A fter the insertion o f 2 in to the tree(a) A he ight ba lanced b inary tree as |b f| 1
12.08.09 IT 60101: Lecture #8 14
Left-to-Left Rotation
6
3
4 7
8
50
9
0
11
10
2
P
A
A
A
P
L
R
R
(c) A V L-ro ta tion as per the LE FT-TO -LE FT(C ase 1) insertion
63
4
7
8
5
9 11
10
2
PRA
R
AL
(d) A fter A V L-ro tation
12.08.09 IT 60101: Lecture #8 15
Left-to-Left Rotation
3
4
6
81
1
0
0
0
5
0
0
0
9
0
11
10
2 70
(e) H e ight ba lanced tree w ith new ly inserted node 2 and a fter A VL-ro ta tion
12.08.09 IT 60101: Lecture #8 16
Right-to-Right Rotation
P
B
BB
P
RL
L
P
B
P B
B
L L
R
A fter ro ta tionA fte r inse rtion in the righ t sub -treeo f the righ t ch ild o f the p ivo t node (P )
12.08.09 IT 60101: Lecture #8 17
Right-to-Right Rotation
6
13
4
7
8
-1
1
0
00
5
0
0
9
0
11
10
12
-2
- -2
-1
P
B
P
B
B L
L
R
0
(a ) 12 is inserted wh ich m akes the tree unbalanced
613
4
7
8 0
0
5
0
0
9
0
11
10 12
B
P
B
BL R
0
-1
0
0
PL
P
L
(b) A fte r A V L ro ta tion
12.08.09 IT 60101: Lecture #8 18
Left-to-Right Rotation
• Also called double rotation• Rotation 1 (Right-to-Left)
– Left sub-tree (BL) of the right child (B) of the left child of the pivot node (P) becomes the right sub-tree of the left child (A).
– Left child (A) of the pivot node (P) becomes the left child of B.
• Rotation 2 (Left-to-Right)– Right sub-tree (BR) of the right child (B) of the left child
(A) of the PIVOT node (P) becomes the left sub-tree of P.
– P becomes the right child of B.
12.08.09 IT 60101: Lecture #8 19
Left-to-Right Rotation
P
A
A
B
P
L
R
R
A fte r inse rtion in the righ t sub-treeo f the le ft ch ild o f the p ivo t node P After ro ta tion
B1
2
BL
PA
A B PL R R
B
BL
12.08.09 IT 60101: Lecture #8 20
Left-to-Right Rotation
3
2
81
0
1 00
0
0
9
0
11
10
(a) A he ight ba lanced tree
3
7
81
1
0
0
5
0
0
90
11
10
2
1
(b) Insertion o f 7 m ade the tree as unba lanced
1
5
0
4 6
00-
2
1
-1
00
4 6
-1
0
Balance factors are recom puted
12.08.09 IT 60101: Lecture #8 21
Left-to-Right Rotation
3
7
8
0
5
0
0
9 11
10
2
4 6
0A
L
PR
BR
P
B
A
(c) N ode 8 becom es the P IV O T node
3
2
8
9 11
10
1
5
4 6
AL B
L
BL
BR P
R
(d ) A fte r A V L-ro ta tion
11
12.08.09 IT 60101: Lecture #8 22
Right-to-Left Rotation
• It is also another double rotation• Rotation 1(Left-to-Right)
– Right sub-tree (BR) of the left child (B) of the right child (A) of the pivot node (P) becomes the left sub-tree of A.
– Right child (A) of the pivot node (P) becomes the right child of B.
• Rotation 2 (Right-to-Left)– Left sub-tree (BL) of the right child (B) of the right child
(A) of the pivot rode (P) becomes the right sub-tree of P.– P becomes the left child of B.
12.08.09 IT 60101: Lecture #8 23
Right-to-Left Rotation
A fte r ro ta tion
P A
ABPL RR
B
BL
P
A
A
B
PL
R
R
B 1
2
BL
A fte r inse rtion in the le ft sub -treethe righ t ch ild o f the p ivo t node (P )
12.08.09 IT 60101: Lecture #8 24
Right-to-Left Rotation
7
80
5
0
09
124 6
0
A
L
P
R
B R
P
2
A
B
L 1311
10
0
00
-1-2
-1-2
01
(a ) W hen 10 is inse rted in to the tree
7
8
5
9 124 6
AL R
BR
B
13
11
10
PL
(b ) A fte r A V L ro ta tions
1
12.08.09 IT 60101: Lecture #8 25
Right-to-Left Rotation
(c ) F ina l he igh t ba lanced tree a fte r A V L ro ta tion
7
8
5
9 124 6
13
11
10
-1
0
00
0
0
0
0
-1
0
12.08.09 IT 60101: Lecture #8 26
Implementation
For tricky ways to manage the pointers see the bookClassic Data Structures
Chapter 7PHI, 2nd Edn., 17th Reprint
12.08.09 IT 60101: Lecture #8 27
Property of a HBT
• Maximum height possible in an AVL tree with n number
of nodes is given by
For the proof of this property see the bookClassic Data Structures
Chapter 7PHI, 2nd Edn., 17th Reprint
nh 2max log44.1