Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

CHAPTER 10

Search Structures

All the programs in this file are selected from


Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
“Fundamentals of Data Structures in C”,
Computer Science Press, 1992.
AVL Trees
• Dynamic tables may also be
maintained as binary search trees.
• Depending on the order of the
symbols putting into the table, the
resulting binary search trees would
be different. Thus the average
comparisons for accessing a symbol is
different.
Binary Search Tree for The
Months of The Year
Input Sequence: JAN, FEB, MAR, APR, MAY, JUNE, JULY, AUG,
SEPT, OCT, NOV, DEC
JA
N

FE M
B AR

AP JU M
R NE AY
AU JU SE
G LY PT
DE OC
C T
Max comparisons: 6 N
Average comparisons: 3.5 OV
A Balanced Binary Search Tree
For The Months of The Year

Input Sequence: JULY, FEB, MAY, AUG, DEC, MAR, OCT, APR, JAN,
JUNE, SEPT, NOV
Max comparisons: 4
JU
LY Average comparisons: 3.1

FE M
B AY

JA
AU M OC
N
G AR T

AP DE JU N SE
R C NE OV PT
Degenerate Binary Search
Tree
AP
R AU Input Sequence: APR, AUG, DEC, FEB,
G DE JAN, JULY, JUNE, MAR, MAY, NOV, OCT,
C FE SEPT
B JA
N JU
LY JU
NE M
AR M
AY N
Max comparisons: 12 OV OC
Average comparisons: 6.5 T SE
PT
Minimize The Search Time of Binary
Search Tree In Dynamic Situation

• From the above three examples, we know that the


average and maximum search time will be
minimized if the binary search tree is maintained
as a complete binary search tree at all times.
• However, to achieve this in a dynamic situation,
we have to pay a high price to restructure the
tree to be a complete binary tree all the time.
• In 1962, Adelson-Velskii and Landis introduced a
binary tree structure that is balanced with
respect to the heights of subtrees. As a result of
the balanced nature of this type of tree, dynamic
retrievals can be performed in O(log n) time if
the tree has n nodes. The resulting tree remains
height-balanced. This is called an AVL tree.
AVL Tree
• Definition: An empty tree is height-balanced. If T
is a nonempty binary tree with TL and TR as its left
and right subtrees respectively, then T is
height-balanced iff
(1) TL and TR are height-balanced, and
(2) |hL – hR| ≤ 1 where hL and hR are the heights of
TL and TR, respectively.
• Definition: The Balance factor, BF(T) , of a node T
is a binary tree is defined to be hL – hR, where hL
and hR, respectively, are the heights of left and
right subtrees of T. For any node T in an AVL
tree, BF(T) = -1, 0, or 1.
Balanced Trees Obtained for
The Months of The Year
-2
0
0 M RR
M M
AR -1
AR AY 0
0
M N
M
(a) Insert MARCH AY 0 OV
AR
N
OV
(c) Insert NOVEMBER
-1
M +1
AR M
0
AY 0
M +1
AY M N
AY OV
(b) Insert MAY 0
AU
G (d) Insert AUGUST
Balanced Trees Obtained for
The Months of The Year
(Cont.)
+2 +1
M LL M
AY 0 AY
+2 0 0
M N AU N
AR OV G OV
+1 0 0
AU AP M
0 G R AR
(e) Insert APRIL
AP 0
+2
R M M
AY 0
AR -1
-1 0
LR M
AU N AU
G OV G AY 0
0 +1 0 0
AP M AP JA N
R AR R N OV
0
JA (f) Insert JANUARY
N
Balanced Trees Obtained for
The Months of The Year
(Cont.)

+1 +1
M M
AR AR -1
-1 -1 -1
AU M AU M
G AY G AY 0
0 +1 0 0 0
AP N AP JA N
JA
R OV R N OV
0
N 0 0
DE DE JU
C C LY

(g) Insert DECEMBER (h) Insert JULY


Balanced Trees Obtained for
The Months of The Year
(Cont.)
+2 +1
M M
AR -2 RL AR -1
-2 0
AU M DE M
G AY 0 C AY 0
0 +1 +1 0
AP JA N AU JA N
R N OV G N OV
-1 0 0 0 0
DE JU AP FE JU
C 0
LY R B LY
FE
B

(i) Insert FEBRUARY


Balanced Trees Obtained for
The Months of The Year
(Cont.)
+2
M
AR LR 0
J
-1 -1
A
DE M
N
C AY +1 0
DE M
+1
A
-1
J N0 C AR
U A O
V +1 0 -1 -1
0 G 0 N -1 A J
J FE M
AP FE U UL
UL B AY
R B J0 G Y J0
Y 0 N0
U AP U O
N R N V
E E

(j) Insert JUNE


Balanced Trees Obtained for
The Months of The Year
(Cont.)
-1 -1
J J
A RR A
+1 N -1 +1 N 0
M M
D D
A A
EC EC
R R
+1 0 -1 -2 +1 0 -1 0
A J M A J N
FE FE
U U A U U O
B B 0
G0 J
0LY Y
-1 N G0 J
0LY 0M V
AP U O
AP U
R N O A C
R N
E 0 OV Y T
E
C
T

(k) Insert OCTOBER


Balanced Trees Obtained for
The Months of The Year
(Cont.)
-1
J
A
N
+1 -1
M
D
A
EC
R
+1A 0 -1 -1
J N
FE
U U O
B
G LY V
0 J
0 0 -1
M O
AP U
A C
R N
Y T
E 0
S
(i) Insert SEPTEMBER EP
T
Rebalancing Rotation of
Binary Search Tree
• LL: new node Y is inserted in the left subtree of
the left subtree of A
• LR: Y is inserted in the right subtree of the left
subtree of A
• RR: Y is inserted in the right subtree of the right
subtree of A
• RL: Y is inserted in the left subtree of the right
subtree of A.
• If a height–balanced binary tree becomes
unbalanced as a result of an insertion, then these
are the only four cases possible for rebalancing.
Rebalancing Rotation LL
LL

+1 +
2 0
A B
A

0 +1 0
B B A
h+2 h+2
AR h AR BL
BL BR BL BR
BR AR

height of BL increases
to h+1
Rebalancing Rotation RR
RR

-1 -
2 0
A B
A

0 -1 0
B B A
h+2 h+2
AL AL BR
BL BR BL BR
AL BL

height of BR increases
to h+1
Rebalancing Rotation LR(a)

+
+1 0
2
A C
A LR(a)
0 -1 0 0
B B B A

0
C
Rebalancing Rotation LR(b)

LR(b)
+ +
1 2 0
A A C

0 -
1 -
B 0
1
B B
h+2 A
+ h+2
0 AR h AR
1
C
C
h BL BL
CL CR CL CR h CL CR
BL AR
Rebalancing Rotation LR(c)

+
0
2
C
A
LR(c)
- +
0
1 1
A
B B
h+2
-
1 AR
C
BL CL CR
h BL AR
CL CR

You might also like