Вы находитесь на странице: 1из 50

Khjlbjh\dZbihbkd

J_p_ilmjguckijZ\hqgbd
LhfZkGbfZg
Thomas Niemann
thomasn@ptld.uswest.net

I_j_\h^kZg]ebckdh]hIG>m[g_j
infoscope@glasnet.ru

1998

H=E:<E?GB?
1.

<<?>?GB?

2.

KHJLBJH<D:

3.

4.

2.1

Khjlbjh\dZ\klZ\dZfb

2.2

Khjlbjh\dZR_eeZ

1.3

;ukljZykhjlbjh\dZ

11

1.4

KjZ\g_gb_f_lh^h\

14

KEH<:JB

15

3.1

O_rlZ[ebpu

15

1.2

Ihbkd\[bgZjguo^_j_\vyo

19

1.3

DjZkghq_jgu_^_j_\vy

22

1.4

JZa^_e_ggu_kibkdb

26

3.5

KjZ\g_gb_f_lh^h\

28

L?DKLUIJH=J:FF

31

4.1

Dh^u^eykhjlbjh\db\klZ\dZfb

31

4.2

Dh^u^eykhjlbjh\dbR_eeZ

32

4.3

Dh^u^ey[ukljh]hihbkdZ nmgdpbb4XLFNVRUW

33

4.4

Dh^u^eyklZg^Zjlghcj_ZebaZpbb[ukljh]hihbkdZ

35

4.5

Dh^u^eyo_rlZ[ebp

36

4.6

Dh^u^ey[bgZjguo^_j_\v_\

38

4.7

Dh^u^eydjZkghq_jguo^_j_\v_\

40

4.8

Dh^u^eyjZa^_e_gguokibkdh\

45

5.

EBL?J:LMJ:

49

6.

KEH<:JV

50

Ij_^bkeh\b_
< wlhc dgb`_qd_ kh^_j`blky bgnhjfZpby h g_kdhevdbo Ze]hjblfZo khjlbjh\db b ihbkdZ Wlm
bgnhjfZpbx fh`gh gZclb \h fgh`_kl\_ dgb]  \ [hevrbgkl\_ ba gbo ij_^iheZ]Z_lky agZgb_
fZl_fZlbq_kdh]h ZgZebaZ b l_hjbb \_jhylghkl_c Ohly nhjfZevgh_ bkke_^h\Zgb_ Ze]hjblfh\ b
^hdZaZl_evkl\h j_amevlZlh\ hibku\Zxsbo bo Zkbfilhlbq_kdb_ k\hckl\Z hq_gv \Z`gu qZklh
\Z`gub\hafh`guqbklhbglmblb\gu_h[tykg_gby
A^_kv\k_Ze]hjblfuh[tykg_gu\gZb[he__ijhklhf\b^_Ij_^iheZ]Z_lkyqlh\u\eZ^__l_Kb
beb IZkdZe_f ih djZcg_c f_j_ gZ gZqZevghf mjh\g_ < qZklghklb \u ^he`gu agZlv qlh lZdh_
fZkkb\ubmdZaZl_ebFZl_jbZeij_^klZ\e_ga^_kv\ihjy^d_hlijhklh]hdqmlv[he__keh`ghfm
G_kfhljygZlhqlhwlhll_dklij_^gZagZq_g^eygZqbgZxsbo\g_f_klvjZa^_eudhlhju_fh]ml
hdZaZlvkybgl_j_kgufbb[he__ijh^\bgmlufqblZl_eyf<hkh[_gghklbwlhhlghkblkydjZa^_eZf
ho_rlZ[ebpZobkdbikibkdZo
LHF:KGBF:G

KZglZDjmaDZebnhjgby
FZjl

AZf_qZgb_i_j_\h^qbdZ
Ijb ql_gbb RU.ALGORITHMS \ jmkkdhf NB>H y qZklh gZludZxkv gZ fZeh]jZfhlgu_ bbeb
g_\_jgu_ml\_j`^_gbyWlhll_dklihdZaZekyfg_bgl_j_kguf^eygZqbgZxsbohgihdjZcg_c
f_j_m[_j_`_lbohlkh\k_fm`g_ijhklbl_evguoaZ[em`^_gbc
I:<?E>M;G?J
Fhkd\Z
N_\jZev

1. <\_^_gb_
Ihbkd, \klZ\dZbm^Ze_gb_dZdba\_klgh hkgh\gu_hi_jZpbbijbjZ[hl_k^ZggufbFugZqg_f
kbkke_^h\Zgbylh]hdZdwlbhi_jZpbbj_ZebamxlkygZ^kZfbba\_klgufbh[t_dlZfbfZkkb\Zfb
b k\yaZggufb kibkdZfb.

FZkkb\u
GZ jbk ihdZaZg fZkkb\ ba k_fb we_f_glh\ k qbkeh\ufb agZq_gbyfb Qlh[u gZclb \ g_f
gm`gh_gZfqbkehfufh`_fbkihevah\Zlvebg_cgucihbkd_]hZe]hjblfijb\_^_ggZjbk.
FZdkbfZevgh_qbkehkjZ\g_gbcijblZdhfihbkd_hgh^hklb]Z_lky\kemqZ_dh]^Zgm`gh_gZf
agZq_gb_gZoh^blky\we_f_gl_ A[6].?kebba\_klghqlh^Zggu_hlkhjlbjh\Zgufh`ghijbf_gblv
^\hbqguc ihbkd jbk   I_j_f_ggu_ Lb b Ub kh^_j`Zl khhl\_lkl\_ggh e_\mx b ijZ\mx
]jZgbpu hlj_adZ fZkkb\Z ]^_ gZoh^blky gm`guc gZf we_f_gl Fu gZqbgZ_f \k_]^Z k
bkke_^h\Zgby kj_^g_]h we_f_glZ hlj_adZ ?keb bkdhfh_ agZq_gb_ f_gvr_ kj_^g_]h we_f_glZ fu
i_j_oh^bf d ihbkdm \ \_jog_c iheh\bg_ hlj_adZ ]^_ \k_ we_f_glu f_gvr_ lhevdh qlh
ijh\_j_ggh]h>jm]bfbkeh\ZfbagZq_gb_fUb klZgh\blky (M 1) bgZke_^mxs_cbl_jZpbbfu
jZ[hlZ_f k iheh\bghc fZkkb\Z LZdbf h[jZahf \ j_amevlZl_ dZ`^hc ijh\_jdb fu ^\h_ km`Z_f
h[eZklv ihbkdZ LZd \ gZr_f ijbf_j_ ihke_ i_j\hc bl_jZpbb h[eZklv ihbkdZ  \k_]h ebrv ljb
we_f_glZ ihke_ \lhjhc hklZ_lky \k_]h ebrv h^bg we_f_gl LZdbf h[jZahf _keb ^ebgZ fZkkb\Z
jZ\gZgZf^hklZlhqghlj_obl_jZpbcqlh[ugZclbgm`gh_qbkeh
0

16

20

37

38

43

Lb

Ub

Jbk0.1: FZkkb\
>\hbqguc ihbkd  hq_gv fhsguc f_lh^ ?keb gZijbf_j ^ebgZ fZkkb\Z jZ\gZ  ihke_
i_j\h]hkjZ\g_gbyh[eZklvkm`Z_lky^hwe_f_glh\Zihke_\lhjhc^hE_]dhihkqblZlv
qlh^eyihbkdZ\fZkkb\_bawe_f_glh\^hklZlhqghkjZ\g_gbc
Djhf_ihbkdZgZfg_h[oh^bfh[u\Z_l\klZ\eylvbm^Zeylvwe_f_gluDkh`Ze_gbxfZkkb\iehoh
ijbkihkh[e_g ^ey \uiheg_gby wlbo hi_jZpbc GZijbf_j qlh[u \klZ\blv qbkeh  \ fZkkb\ gZ
jbk  gZf ihgZ^h[blky k^\bgmlv we_f_glu A[3]A[6] \gba  ebrv ihke_ wlh]h fu kfh`_f
aZibkZlvqbkeh\we_f_gl A[3]. :gZeh]bqgZy ijh[e_fZ\hagbdZ_lijbm^Ze_gbbwe_f_glh\>ey
ih\ur_gbywnn_dlb\ghklbhi_jZpbc\klZ\dbm^Ze_gbyij_^eh`_guk\yaZggu_kibkdb

int function SequentialSearch (Array A , int Lb , int Ub , int Key );


begin
for i = Lb to Ub do
if A ( i ) = Key then
return i ;
return 1;
end;
Jbk0.2: Ebg_cgucihbkd
int function BinarySearch (Array A , int Lb , int Ub , int Key );
begin
do forever
M = ( Lb + Ub )/2;
if ( Key < A[M]) then
Ub = M 1;
else if (Key > A[M]) then
Lb = M + 1;
else
return M ;
if (Lb > Ub ) then
return 1;
end;
Jbk0.3: >\hbqgucihbkd

H^ghk\yagu_kibkdb
GZjbkl_`_qbkeZqlhbjZgvr_ojZgylky\\b^_k\yaZggh]hkibkdZkeh\hk\yaZggucqZklh
himkdZxl Ij_^iheZ]Zy qlh X b P y\eyxlky mdZaZl_eyfb qbkeh  fh`gh \klZ\blv \ lZdhc
kibkhdke_^mxsbfh[jZahf
X->Next = P->Next;
P->Next = X;

Kibkdb iha\heyxl hkms_kl\blv \klZ\dm b m^Ze_gb_ hq_gv wnn_dlb\gh Ihbgl_j_km_fky h^gZdh


dZdgZclbf_klhdm^Zfu[m^_f\klZ\eylvgh\ucwe_f_gll_dZdbfh[jZahfijbk\hblvgm`gh_
agZq_gb_ mdZaZl_ex P M\u ^ey ihbkdZ gm`ghc lhqdb ijb^_lky ijhclbkv ih we_f_glZf kibkdZ
LZdbfh[jZahfi_j_oh^dkibkdZfiha\hey_lmf_gvrblv\j_fy\klZ\dbm^Ze_gbywe_f_glZaZkq_l
m\_ebq_gby\j_f_gbihbkdZ

X
18

#
4

16

20

37

38

43

Jbk0.4H^ghk\yaguckibkhd

Hp_gdb\j_f_gbbkiheg_gby
>ey hp_gdb ijhba\h^bl_evghklb Ze]hjblfh\ fh`gh bkihevah\Zlv jZagu_ ih^oh^u KZfuc
[_kobljhklguc ijhklh aZimklblv dZ`^uc Ze]hjblf gZ g_kdhevdbo aZ^ZqZo b kjZ\gblv \j_fy
bkiheg_gby >jm]hc kihkh[  hp_gblv \j_fy bkiheg_gby GZijbf_j fu fh`_f ml\_j`^Zlv qlh
\j_fy ihbkdZ _klv O(n) qblZ_lky lZd h [hevrh_ hl n  Wlh hagZqZ_l qlh ijb [hevrbo n \j_fy
ihbkdZg_kbevgh[hevr_q_fdhebq_kl\hwe_f_glh\Dh]^Zbkihevamxlh[hagZq_gb_O()bf_xl\
\b^m g_ lhqgh_ \j_fy bkiheg_gby Z lhevdh _]h ij_^_e k\_jom ijbq_f k lhqghklvx ^h
ihklhyggh]hfgh`bl_eyDh]^Z]h\hjylgZijbf_jqlhZe]hjblfmlj_[m_lky\j_fyihjy^dZ O(n2),
bf_xl\\b^mqlh\j_fybkiheg_gbyaZ^ZqbjZkl_lg_[uklj__q_fd\Z^jZldhebq_kl\Zwe_f_glh\
Qlh[u ihqm\kl\h\Zlv qlh wlh lZdh_ ihkfhljbl_ lZ[ebpm  ]^_ ijb\_^_gu qbkeZ
beexkljbjmxsb_ kdhjhklv jhklZ ^ey g_kdhevdbo jZaguo nmgdpbc Kdhjhklv jhklZ O(lg n)
oZjZdl_jbam_lZe]hjblfulbiZ^\hbqgh]hihbkdZEh]Zjbnfihhkgh\Zgbxlg,m\_ebqb\Z_lkygZ
 dh]^Z n m^\Zb\Z_lky <kihfgbl_  dZ`^h_ gh\h_ kjZ\g_gb_ iha\hey_l gZf bkdZlv \ \^\h_
[hevr_fkibkd_Bf_gghihwlhfm]h\hjylqlh\j_fyjZ[hluijb^\hbqghfihbkd_jZkl_ldZdlg n.
n
1
16
256
4,096
65,536
1,048,476
16,775,616

lg n
0
4
8
12
16
20
24

n1.25
1
32
1,024
32,768
1,048,476
33,554,432
1,073,613,825

n lg n
0
64
2,048
49,152
1,048,565
20,969,520
402,614,784

n2
1
256
65,536
16,777,216
4,294,967,296
1,099,301,922,576
281,421,292,179,456

LZ[ebpZ0.1KdhjhklvjhklZg_kdhevdbonmgdpbc
?keb kqblZlv qlh qbkeZ \ lZ[ebp_  khhl\_lkl\mxl fbdjhk_dmg^Zf lh ^ey aZ^Zqb k 1048476
we_f_glZfb Ze]hjblfm k \j_f_g_f jZ[hlu O(lg n) ihlj_[m_lky  fbdjhk_dmg^ Ze]hjblfm k
\j_f_g_f jZ[hlu O(n1.25)  ihjy^dZ  k_dmg^ Ze]hjblfm k \j_f_g_f jZ[hlu O(n2)  [he__ 
^g_c < gb`_ke_^mxs_f l_dkl_ ^ey dZ`^h]h Ze]hjblfZ ijb\_^_gu khhl\_lkl\mxsb_ Ohp_gdb
;he__lhqgu_nhjfmebjh\dbb^hdZaZl_evkl\Zfh`ghgZclb\ijb\h^bfuoebl_jZlmjguokkuedZo

BlZd
DZdfu\b^_eb_kebfZkkb\hlkhjlbjh\ZglhbkdZlv_]hwe_f_glugm`ghkihfhsvx^\hbqgh]h
ihbkdZ H^gZdh g_ aZ[m^_f fZkkb\ dlhlh ^he`_g hlkhjlbjh\Zlv < ke_^mxs_f jZa^_e_ fu
6

bkke_^m_l jZagu_ kihkh[u khjlbjh\db fZkkb\Z HdZau\Z_lky wlZ aZ^ZqZ \klj_qZ_lky ^hklZlhqgh
qZklh b lj_[m_l aZf_lguo \uqbkebl_evguo j_kmjkh\ ihwlhfm khjlbjmxsb_ Ze]hjblfu
bkke_^h\Zgu \^hev b ihi_j_d ba\_klgu Ze]hjblfu wnn_dlb\ghklv dhlhjuo ^hklb]eZ
l_hj_lbq_kdh]hij_^_eZ
K\yaZggu_ kibkdb iha\heyxl wnn_dlb\gh \klZ\eylv b m^Zeylv we_f_glu gh ihbkd \ gbo
ihke_^h\Zl_e_g b ihlhfm hlgbfZ_l fgh]h \j_f_gb Bf_xlky Ze]hjblfu iha\heyxsb_
wnn_dlb\gh\uihegylv\k_ljbhi_jZpbbfuh[km^bfba\jZa^_e_hkeh\Zjyo

2. Khjlbjh\dZ
2.1Khjlbjh\dZ\klZ\dZfb
H^bg ba ijhkl_crbo kihkh[h\ hlkhjlbjh\Zlv fZkkb\  khjlbjh\dZ \klZ\dZfb < h[uqghc `bagb
fuklZedb\Z_fkykwlbff_lh^hfijbb]j_\dZjluQlh[uhlkhjlbjh\Zlvbf_xsb_ky\\ZkdZjlu
\u \ugbfZ_l_ dZjlm k^\b]Z_l_ hklZ\rb_ky dZjlu Z aZl_f \klZ\ey_l_ dZjlm gZ gm`gh_ f_klh
Ijhp_kkih\lhjy_lky^hl_oihjihdZohlvh^gZdZjlZgZoh^blkyg_gZf_kl_DZdkj_^g__lZdb
om^r__\j_fy^eywlh]hZe]hjblfZO(n2).>Zevg_crmxbgnhjfZpbxfh`ghihemqblv\dgb`d_
DgmlZ [1].

L_hjby
GZ jbk(a) fu\ugbfZ_fwe_f_glAZl_fwe_f_glujZkiheh`_ggu_\ur_k^\b]Z_f\gba^h
l_o ihj ihdZ g_ gZc^_f f_klh dm^Z gm`gh \klZ\blv  Wlh ijhp_kk ijh^he`Z_lky gZ jbkJbk
0.1(b)^eyqbkeZGZdhg_pgZjbk (c) fuaZ\_jrZ_fkhjlbjh\dm, ihf_klb\gZgm`gh_f_klh
?keb ^ebgZ gZr_]h fZkkb\Z jZ\gZ n gZf gm`gh ijhclbkv ih n 1 we_f_glZf DZ`^uc jZa gZf
fh`_lihgZ^h[blvkyk^\bgmlvn 1^jm]bowe_f_glh\<hlihq_fmwlhlf_lh^lj_[m_l^h\hevgh
lZdbfgh]h\j_f_gb
Khjlbjh\dZ\klZ\dZfbhlghkblkydqbkemf_lh^h\khjlbjh\dbihf_klm>jm]bfbkeh\Zfb_cg_
lj_[m_lky \kihfh]Zl_evgZy iZfylv fu khjlbjm_f we_f_glu fZkkb\Z bkihevamy lhevdh iZfylv
aZgbfZ_fmx kZfbf fZkkb\hf Djhf_ lh]h hgZ y\ey_lky mklhcqb\hc  _keb kj_^b khjlbjm_fuo
dexq_cbf_xlkyh^bgZdh\u_ihke_khjlbjh\dbhgbhklZxlky\bkoh^ghfihjy^d_

(a)

(b)

(c)

Jbk0.1Khjlbjh\dZ\klZ\dZfb

J_ZebaZpby
J_ZebaZpbxkhjlbjh\db\klZ\dZfbgZKb\ugZc^_l_\jZa^_e_Hi_jZlhjtypedef Tbhi_jZlhj
kjZ\g_gby compGT ke_^m_l baf_gblv lZd qlh[u hgb khhl\_lkl\h\Zeb ^Zgguf ojZgbfuf \
lZ[ebp_

2.2Khjlbjh\dZR_eeZ
F_lh^ ij_^eh`_gguc >hgZev^hf E R_eehf y\ey_lky g_mklhcqb\hc khjlbjh\dhc ih f_klm
Wnn_dlb\ghklv f_lh^Z R_eeZ h[tykgy_lky l_f qlh k^\b]Z_fu_ we_f_glu [ukljh ihiZ^Zxl gZ
gm`gu_ f_klZ Kj_^g__ \j_fy ^ey khjlbjh\db R_eeZ jZ\gy_lky O(n1.25) ^ey om^r_]h kemqZy
hp_gdhcy\ey_lky O(n1.5).>Zevg_crb_kkuedbkf\dgb`d_DgmlZ [1].

L_hjby
GZ jbk  a) [ue ijb\_^_g ijbf_j khjlbjh\db \klZ\dZfb Fu kgZqZeZ \ugbfZeb  aZl_f
k^\b]ZebbgZh^gmihabpbx\gbaihke_q_]h\klZ\eyebLZdbfh[jZahfgZflj_[h\Zebkv^\Z
k^\b]Z<ke_^mxsbcjZagZflj_[h\Zehkv^\Zk^\b]Zqlh[u\klZ\blvgZgm`gh_f_klhGZ\_kv
ijhp_kkgZflj_[h\Zehkv k^\b]h\
GZjbk b)beexkljbjm_lkykhjlbjh\dZR_eeZFugZqbgZ_fijhba\h^ykhjlbjh\dm\klZ\dZfb
krZ]hfKgZqZeZfujZkkfZljb\Z_fqbkeZbba\e_dZ_fk^\b]Z_fgZihabpbxkrZ]hf
\klZ\ey_fAZl_fih\lhjy_flh`_^eyqbk_ebba\e_dZ_fk^\b]Z_f\gba\klZ\ey_f
B l^ AZdhgqb\ khjlbjh\dm k rZ]hf  ijhba\h^bf __ k rZ]hf  l_ \uihegy_f h[uqgmx

khjlbjh\dm \klZ\dZfb <k_]h ijb wlhf gZf ihgZ^h[blky       k^\b]Z LZdbf h[jZahf
bkihevah\Z\\gZqZe_rZ][hevrbcfu^h[bebkvf_gvr_]hqbkeZk^\b]h\
2s

(a)

2s
1

1s

(b)

1s

1s

1s

Jbk0.2Khjlbjh\dZR_eeZ
Fh`gh bkihevah\Zlv kZfu_ jZagu_ ko_fu \u[hjZ rZ]h\ DZd ijZ\beh kgZqZeZ fu khjlbjm_f
fZkkb\ k [hevrbf rZ]hf aZl_f mf_gvrZ_f rZ] b ih\lhjy_f khjlbjh\dm < kZfhf dhgp_
khjlbjm_fkrZ]hfOhlywlhlf_lh^e_]dhh[tykgblv_]hnhjfZevgucZgZeba^h\hevghljm^_g
< qZklghklb l_hj_lbdZf g_ m^Zehkv gZclb hilbfZevgmx ko_fm \u[hjZ rZ]h\ Dgml[1] ijh\_e
fgh`_kl\hwdki_jbf_glh\bke_^mxsmxnhjfmem\u[hjZrZ]h\(h) ^eyfZkkb\Z^ebguN:
\ihke_^h\Zl_evghklbK1 = 1 KV + 1 = 3KV + 1 + 1 \aylvKW  _kebKW + 2 1

<hlg_kdhevdhi_j\uoagZq_gbc h:
h1 = 1
h2 = (3 1) + 1 = 4
h3 = (3 4 ) + 1 = 13
h4 = (3 13) + 1 = 40
h5 = (3 40) + 1 = 121

Qlh[u hlkhjlbjh\Zlv fZkkb\ ^ebghc  ij_`^_ \k_]h gZc^_f ghf_j s, ^ey dhlhjh]h hs 100.
Kh]eZkgh ijb\_^_gguf pbnjZf s = 5 Gm`gh_ gZf agZq_gb_ gZoh^blky ^\mfy kljhqdZfb \ur_
LZdbfh[jZahfihke_^h\Zl_evghklvrZ]h\ijbkhjlbjh\d_[m^_llZdhc13-4-1.Gmdhg_qghgZf
g_ gm`gh ojZgblv wlm ihke_^h\Zl_evghklv hq_j_^gh_ agZq_gb_ h gZoh^blky ba ij_^u^ms_]h ih
nhjfme_
hs1 = hs / 3

J_ZebaZpby

10

J_ZebaZpbx khjlbjh\db R_eeZ gZ Kb \u gZc^_l_ \ jZa^_e_  Hi_jZlhj typedef T b hi_jZlhj
kjZ\g_gby compGT ke_^m_l baf_gblv lZd qlh[u hgb khhl\_lkl\h\Zeb ^Zgguf ojZgbfuf \
fZkkb\_Hkgh\gZyqZklvZe]hjblfZkhjlbjh\dZ\klZ\dZfbkrZ]hfh.

2.3;ukljZykhjlbjh\dZ
Ohly b^_y R_eeZ agZqbl_evgh memqrZ_l khjlbjh\dm \klZ\dZfb j_a_j\u _s_ hklZxlky H^bg ba
gZb[he__ ba\_klguo Ze]hjblfh\ khjlbjh\db  [ukljZy khjlbjh\dZ ij_^eh`_ggZy QOhhjhf
F_lh^ b \ kZfhf ^_e_ hq_gv [uklj g_^Zjhf ihZg]ebckdb _]h lZd b \_ebqZxl QuickSort d
g_m^h\hevkl\bx\k_oki_eeq_d_jh\ Rbrdh\ijhklbg_agZxdZdi_j_\_klb 
Wlhfm f_lh^m lj_[m_lky O(n lg n) \ kj_^g_f b O(n2) \ om^r_f kemqZ_ D kqZklvx _keb ijbgylv
Z^_d\Zlgu_ ij_^hklhjh`ghklb gZbom^rbc kemqZc djZcg_ fZeh\_jhyl_g ;ukljuc ihbkd g_
y\ey_lkymklhcqb\ufDjhf_lh]h_fmlj_[m_lkykl_dl_hgg_y\ey_lkybf_lh^hfkhjlbjh\db
gZf_kl_>Zevg_crmxbgnhjfZpbxfh`ghihemqblv\jZ[hl_Dhjf_gZ [2].

L_hjby
:e]hjblfjZa[b\Z_lkhjlbjm_fucfZkkb\gZjZa^_euaZl_fj_dmjkb\ghkhjlbjm_ldZ`^ucjZa^_e
< nmgdpbb Partition (Jbk ) h^bg ba we_f_glh\ fZkkb\Z \u[bjZ_lky \ dZq_kl\_ p_gljZevgh]h.
Dexqbf_gvrb_p_gljZevgh]hke_^m_ljZkiheh`blvke_\Zhlg_]hl_dhlhju_[hevr_kijZ\Z
int function Partition (Array A, int Lb, int Ub);
begin
select a pivot from A[Lb]A[Ub];
reorder A[Lb]A[Ub] such that:
all values to the left of the pivot are pivot
all values to the right of the pivot are pivot
return pivot position;
end;
procedure QuickSort (Array A, int Lb, int Ub);
begin
if Lb < Ub then
M = Partition (A, Lb, Ub);
QuickSort (A, Lb, M 1);
QuickSort (A, M + 1, Ub);
end;
Jbk0.3;ukljucihbkd
GZjbk(a)\dZq_kl\_p_gljZevgh]h\u[jZgwe_f_glBg^_dkugZqbgZxlbaf_gylvkykdhgph\
fZkkb\Z Bg^_dk i gZqbgZ_lky ke_\Z b bkihevam_lky ^ey \u[hjZ we_f_glh\ dhlhju_ [hevr_
p_gljZevgh]hbg^_dkjgZqbgZ_lkykijZ\Zbbkihevam_lky^ey\u[hjZwe_f_glh\dhlhju_f_gvr_
p_gljZevgh]hWlbwe_f_gluf_gyxlkyf_klZfbkfjbk(b)Ijhp_^mjZQuickSort j_dmjkb\gh
khjlbjm_l^\Zih^fZkkb\Z\j_amevlZl_ihemqZ_lkyfZkkb\ij_^klZ\e_ggucgZjbk(c).

11

Lb

(a)

Ub
2

pivot

Lb

Lb

(b)

(c)

Jbk0.4Ijbf_jjZ[hluZe]hjblfZ4XLFNVRUW
< ijhp_kk_ khjlbjh\db fh`_l ihlj_[h\Zlvky i_j_^\bgmlv p_gljZevguc we_f_gl ?keb gZf
ih\_a_l \u[jZgguc we_f_gl hdZ`_lky f_^bZghc agZq_gbc fZkkb\Z l_ jZa^_ebl _]h ihiheZf
Ij_^iheh`bf gZ fbgmldm qlh wlh b \ kZfhf ^_e_ lZd Ihkdhevdm gZ dZ`^hf rZ]m fu ^_ebf
fZkkb\ ihiheZf Z nmgdpby Partition \ dhgp_ dhgph\ ijhkfhljbl \k_ n we_f_glh\ \j_fy jZ[hlu
Ze]hjblfZ_klvO(n lg n).
<dZq_kl\_p_gljZevgh]hnmgdpbyPartition fh`_lihijhklm[jZlvi_j\ucwe_f_gl (A[Lb]).<k_
hklZevgu_ we_f_glu fZkkb\Z fu kjZ\gb\Z_f k p_gljZevguf b i_j_^\b]Z_f eb[h \e_\h hl g_]h
eb[h \ijZ\h ?klv h^gZdh h^bg kemqZc dhlhjuc [_a`Zehklgh jZajmrZ_l wlm ij_djZkgmx
ijhklhlmIj_^iheh`bfqlhgZrfZkkb\kkZfh]hgZqZeZhlkhjlbjh\ZgNmgdpbyPartition \k_]^Z
[m^_l ihemqZlv \ dZq_kl\_ p_gljZevgh]h fbgbfZevguc we_f_gl b ihlhfm jZa^_ebl fZkkb\
gZbom^rbf kihkh[hf \ e_\hf jZa^_e_ hdZ`_lky h^bg we_f_gl khhl\_lkl\_ggh \ ijZ\hf
hklZg_lky Ub Lb we_f_glh\ LZdbf h[jZahf dZ`^uc j_dmjkb\guc \uah\ ijhp_^mju quicksort
\k_]hebrvmf_gvrbl^ebgmkhjlbjm_fh]hfZkkb\ZgZ<j_amevlZl_^ey\uiheg_gbykhjlbjh\db
ihgZ^h[blky n j_dmjkb\guo \uah\h\ qlh ijb\h^bl d \j_f_gb jZ[hlu Ze]hjblfZ ihjy^dZ O(n2).
H^bgbakihkh[h\ih[hjhlvwlmijh[e_fmkemqZcgh\u[bjZlvp_gljZevgucwe_f_glWlhk^_eZ_l
gZbom^rbckemqZcqj_a\uqZcghfZeh\_jhylguf

J_ZebaZpby
J_ZebaZpby Ze]hjblfZ gZ Kb gZoh^blky \ jZa^_e_  Hi_jZlhju typedef T b compGT ke_^m_l
baf_gblv lZd qlh[u hgb khhl\_lkl\h\Zeb ^Zgguf ojZgbfuf \ fZkkb\_ Ih kjZ\g_gbx k
hkgh\gufZe]hjblfhfbf_xlkyg_dhlhju_memqr_gby:

< dZq_kl\_ p_gljZevgh]h \ nmgdpbb partition \u[bjZ_lky we_f_gl jZkiheh`_gguc \


k_j_^bg_ LZdhc \u[hj memqrZ_l hp_gdm kj_^g_]h \j_f_gb jZ[hlu _keb fZkkb\
mihjy^hq_gebrvqZklbqghGZbom^rZy^eywlhcj_ZebaZpbbkblmZpby\hagbdZ_l\kemqZ_
dh]^Z dZ`^uc jZa ijb jZ[hl_ partition.\ dZq_kl\_ p_gljZevgh]h \u[bjZ_lky
fZdkbfZevgucbebfbgbfZevgucwe_f_gl.

>ey dhjhldbo fZkkb\h\ \uau\Z_lky insertSort. BaaZ j_dmjkbb b ^jm]bo gZdeZ^guo


jZkoh^h\ [ukljuc ihbkd hdZau\Z_lky g_ klhev m` [ukljuf ^ey dhjhldbo fZkkb\h\
Ihwlhfm _keb \ fZkkb\_ f_gvr_  we_f_glh\ \uau\Z_lky khjlbjh\dZ \klZ\dZfb
Ihjh]h\h_agZq_gb_g_djblbqghhghkbevghaZ\bkblhldZq_kl\Z]_g_jbjm_fh]hdh^Z

12

?keb ihke_^gbc hi_jZlhj nmgdpbb y\ey_lky \uah\hf wlhc nmgdpbb ]h\hjyl h o\hklh\hc
j_dmjkbb?_bf__lkfukeaZf_gylvgZbl_jZpbb\wlhfkemqZ_emqr_bkihevam_lkykl_d
Wlhk^_eZghijb\lhjhf\uah\_ QuickSort gZ jbk

Ihke_ jZa[b_gby kgZqZeZ khjlbjm_lky f_gvrbc jZa^_e Wlh lZd`_ ijb\h^bl d emqr_fm
bkihevah\Zgbxkl_dZihkdhevdmdhjhldb_jZa^_eukhjlbjmxlky[uklj__bbfgm`_g[he__
dhjhldbckl_d.

<jZa^_e_\ugZc^_l_lZd`_qsort nmgdpbxbaklZg^Zjlghc[b[ebhl_dbKbdhlhjZy\
khhl\_lkl\bb gZa\Zgb_f hkgh\ZgZ gZ Ze]hjblf_ quicksort. >ey wlhc j_ZebaZpbb j_dmjkby [ueZ
aZf_g_gZgZbl_jZpbb<lZ[ebp_ijb\h^blky\j_fybjZaf_jkl_dZaZljZqb\Z_fu_^hbihke_
hibkZgguomemqr_gbc.

count
16
256
4,096
65,536

Time (s)
before
after
103
51
1,630
911
34,183
20,016
658,003
470,737

stacksize
before
540
912
1,908
2,436

after
28
112
168
252

LZ[ebpZ0.1<ebygb_memqr_gbcgZkdhjhklvjZ[hlubjZaf_jkl_dZ

2.4

13

KjZ\g_gb_f_lh^h\
< ^Zgghf jZa^_e_ fu kjZ\gbf hibkZggu_ Ze]hjblfu khjlbjh\db \klZ\dZfb R_eeZ b [ukljmx
khjlbjh\dm ?klv g_kdhevdh nZdlhjh\ \ebyxsbo gZ \u[hj Ze]hjblfZ \ dZ`^hc dhgdj_lghc
kblmZpbb

Mklhcqb\hklvGZihfgbfqlhmklhcqb\Zykhjlbjh\dZg_f_gy_l\aZbfgh]hjZkiheh`_gby
we_f_glh\kjZ\gufbdexqZfbKhjlbjh\dZ\klZ\dZfb_^bgkl\_ggucbajZkkfhlj_gguo
Ze]hjblfh\h[eZ^Zxsbowlbfk\hckl\hf

IZfylv Khjlbjh\d_ gZ f_kl_ g_ lj_[m_lky ^hihegbl_evgZy iZfylv Khjlbjh\dZ


\klZ\dZfbbR_eeZm^h\e_l\hjyxlwlhfmmkeh\bx;ukljhckhjlbjh\d_lj_[m_lkykl_d^ey
hj]ZgbaZpbb j_dmjkbb H^gZdh lj_[m_fh_ wlhfm Ze]hjblfm f_klh fh`gh kbevgh
mf_gvrblvih\hab\rbkvkZe]hjblfhf

<j_fy<j_fygm`gh_^eykhjlbjh\dbgZrbo^Zgguoe_]dhklZgh\blkyZkljhghfbq_kdbf
kf lZ[ebpm   LZ[ebpZ  iha\hey_l kjZ\gblv \j_f_ggu_ aZljZlu dZ`^h]h ba
Ze]hjblfh\ihdhebq_kl\mbkihegy_fuohi_jZlhjh\.
LZ[ebpZ0.2:KjZ\g_gb_f_lh^h\khjlbjh\db

<j_fy aZljZq_ggh_ dZ`^uf ba Ze]hjblfh\ gZ khjlbjh\dm kemqZcgh]h gZ[hjZ ^Zgguo
ij_^klZ\e_gh\lZ[ebp_.
Method
insertion sort
shell sort
quicksort
count
16
256
4,096
65,536

statements
9
17
21

average time
O(n2)
O(n1.25)
O(n lg n)

insertion
39 s
4,969 s
1.315 sec
416.437 sec

shell
45 s
1,230 s
.033 sec
1.254 sec

worst-case time
O(n2)
O(n1.5)
O(n2)
quicksort
51 s
911 s
.020 sec
.461 sec

LZ[ebpZ0.3: <j_fykhjlbjh\db

14

3. Keh\Zjb
3.1O_rlZ[ebpu
>ey jZ[hlu k keh\Zj_f lj_[mxlky ihbkd, \klZ\dZ b m^Ze_gb_ H^bg ba gZb[he__ wnn_dlb\guo
kihkh[h\ j_ZebaZpbb keh\Zjy  o_rlZ[ebpu Kj_^g__ \j_fy ihbkdZ we_f_glZ \ gbo _klv O(1),
\j_fy^eygZbom^r_]hkemqZyO(n).Ij_djZkgh_baeh`_gb_o_rbjh\Zgbyfh`ghgZclb\jZ[hlZo
Dhjf_gZ[2] bDgmlZ[1].Qlh[uqblZlvklZlvbgZwlml_fm\ZfihgZ^h[blky\eZ^_lvkhhl\_lkl\mxs_c
l_jfbgheh]b_c A^_kv hibkZg f_lh^ ba\_klguc dZd k\yau\Zgb_ beb hldjulh_ o_rbjh\Zgb_[3].
>jm]hc f_lh^ ba\_klguc dZd aZfdgmlh_ o_rbjh\Zgb_[3] beb aZdjulZy Z^j_kZpby[1], a^_kv g_
h[km`^ZxlkyGmdZd"

L_hjby
O_rlZ[ebpZ  wlh h[uqguc fZkkb\ k g_h[uqghc Z^j_kZpb_c aZ^Z\Z_fhc o_rnmgdpb_c
GZijbf_j gZ hashTable jbk   wlh fZkkb\ ba  we_f_glh\ DZ`^uc we_f_gl ij_^klZ\ey_l
kh[hcmdZaZl_evgZebg_cguckibkhdojZgysbcqbkeZO_rnmgdpby\wlhfijbf_j_ijhklh^_ebl
dexqgZbbkihevam_lhklZlhddZdbg^_dk\lZ[ebp_Wlh^Z_lgZfqbkeZhl^hIhkdhevdm^ey
Z^j_kZpbb \ hashTable gZf b gm`gu qbkeZ hl  ^h  Ze]hjblf ]ZjZglbjm_l ^himklbfu_
agZq_gbybg^_dkh\
HashTable
0

16

11

27

6
7

19

#
#

22

Jbk0.1: O_rlZ[ebpZ
Qlh[u \klZ\blv \ lZ[ebpm gh\uc we_f_gl fu o_rbjm_f dexq qlh[u hij_^_eblv kibkhd \
dhlhjuc _]h gm`gh ^h[Z\blv aZl_f \klZ\ey_f we_f_gl \ gZqZeh wlh]h kibkdZ GZijbf_j qlh[u
^h[Z\blv  fu ^_ebf  gZ  b ihemqZ_f hklZlhd  LZdbf h[jZahf  ke_^m_l jZaf_klblv \
kibkd_ gZ gZqZeh dhlhjh]h mdZau\Z_l hashTable[3] Qlh[u gZclb qbkeh fu _]h o_rbjm_f b
ijhoh^bfihkhhl\_lkl\mxs_fmkibkdmQlh[um^ZeblvqbkehfugZoh^bf_]hbm^Zey_fwe_f_gl
kibkdZ_]hkh^_j`Zsbc
?kebo_rnmgdpbyjZkij_^_ey_lkh\hdmighklv\hafh`guodexq_cjZ\ghf_jghihfgh`_kl\m
bg^_dkh\lho_rbjh\Zgb_wnn_dlb\ghjZa[b\Z_lfgh`_kl\hdexq_cGZbom^rbckemqZcdh]^Z
\k_ dexqb o_rbjmxlky \ h^bg bg^_dk Ijb wlhf fu jZ[hlZ_f k h^gbf ebg_cguf kibkdhf
dhlhjuc b \ugm`^_gu ihke_^h\Zl_evgh kdZgbjh\Zlv dZ`^uc jZa dh]^Z qlhgb[m^v ^_eZ_f
Hlkx^Z \b^gh dZd \Z`gZ ohjhrZy o_rnmgdpby A^_kv fu jZkkfhljbf ebrv g_kdhevdh ba

15

\hafh`guoih^oh^h\IjbbeexkljZpbbf_lh^h\ij_^iheZ]Z_lkyqlhunsigned char jZkiheZ]Z_lky


\ 8 [blZo, unsigned short int \16, unsigned long int \32.

>_e_gb_ jZaf_jlZ[ebpuhashTableSize ijhklh_qbkeh . Wlhlf_lh^bkihevah\Zg\


ihke_^g_f ijbf_j_ O_rbjmxs__ agZq_gb_ hashValue, baf_gyxs__ky hl 0 ^h
(hashTableSize - 1), jZ\ghhklZldmhl^_e_gbydexqZgZjZaf_jo_rlZ[ebpu <hldZd
wlhfh`_l\u]ey^_lv:
typedef int hashIndexType;
hashIndexType hash(int Key) {
return Key % hashTableSize;
}

>ey mki_oZ wlh]h f_lh^Z hq_gv \Z`_g \u[hj ih^oh^ys_]h agZq_gby hashTableSize.
?keb gZijbf_j hashTableSize jZ\gy_lky ^\mf lh ^ey q_lguo dexq_c o_ragZq_gby
[m^ml q_lgufb ^ey g_q_lguo  g_q_lgufb Ykgh qlh wlh g_`_eZl_evgh  \_^v _keb \k_
dexqb hdZ`mlky q_lgufb hgb ihiZ^ml \ h^bg we_f_gl lZ[ebpu :gZeh]bqgh _keb \k_
dexqb hdZ`mlky q_lgufb lh hashTableSize jZ\gh_ kl_i_gb ^\mo ihijhklm \havf_l
qZklv [blh\ Key \ dZq_kl\_ bg^_dkZ Qlh[u ihemqblv [he__ kemqZcgh_ jZkij_^_e_gb_
dexq_c \ dZq_kl\_ hashTableSize gm`gh [jZlv ijhklh_ qbkeh g_ kebrdhf [ebadh_ d
kl_i_gb^\mo

FmevlbiebdZlb\guc f_lh^ jZaf_j lZ[ebpu hashTableSize _klv kl_i_gv 2n).


AgZq_gb_ Key mfgh`Z_lky gZ dhgklZglm aZl_f hl j_amevlZlZ [_j_lky g_h[oh^bfh_ qbkeh
[blh\ < dZq_kl\_ lZdhc dhgklZglu Dgml[1] j_dhf_g^m_l ahehlh_ k_q_gb_ 5 1 / 2 =

0.6180339887499ImklvgZijbf_jfujZ[hlZ_fk[ZclZfbMfgh`b\ahehlh_k_q_gb_gZ
28 ihemqZ_f  I_j_fgh`bf [blh\uc dexq b  ihemqZ_f [blh\h_ p_eh_ >ey
lZ[ebpu ^ebghc 5 \ dZq_kl\_ o_rbjmxs_]h agZq_gby [_j_f  feZ^rbo [blh\ feZ^r_]h
keh\Zkh^_j`Zs_]hlZdh_ijhba\_^_gb_<hldZdfh`ghj_Zebah\Zlvwlhlf_lh^
/* 8-bit index */
typedef unsigned char hashIndexType;
static const hashIndexType K = 158;
/* 16-bit index */
typedef unsigned short int hashIndexType;
static const hashIndexType K = 40503;
/* 32-bit index */
typedef unsigned long int hashIndexType;
static const hashIndexType K = 2654435769;
/* w=bitwidth(hashIndexType), size of table=2**m */
static const int S = w - m;
hashIndexType hashValue = (hashIndexType)(K * Key) >> S;

16

Imklv gZijbf_j jZaf_j lZ[ebpu hashTableSize jZ\_g 1024 (210) Lh]^Z gZf ^hklZlhq_g
16-[blgucbg^_dkb S [m^_lijbk\h_ghagZq_gb_ 16 10 = 6. <blh]_ihemqZ_f
typedef unsigned short int hashIndexType;
hashIndexType hash(int Key) {
static const hashIndexType K = 40503;
static const int S = 6;
return (hashIndexType)(K * Key) >> S;
}

:^^blb\gucf_lh^^eykljhdi_j_f_gghc^ebgu (jZaf_jlZ[ebpujZ\_g 256). >eykljhd


i_j_f_gghc ^ebgu \iheg_ jZamfgu_ j_amevlZlu ^Z_l keh`_gb_ ih fh^mex  < wlhf
kemqZ_j_amevlZlhashValueaZdexq_gf_`^mb
typedef unsigned char hashIndexType;
hashIndexType hash(char *str) {
hashIndexType h = 0;
while (*str) h += *str++;
return h;
}

BkdexqZxs__BEB^eykljhdi_j_f_gghc^ebgu (jZaf_jlZ[ebpujZ\_g 256). Wlhlf_lh^


ZgZeh]bq_g Z^^blb\ghfm gh mki_rgh jZaebqZ_l koh`b_ keh\Z b ZgZ]jZffu Z^^blb\guc
f_lh^^Zklh^ghagZq_gb_^eyXYbYX).F_lh^dZde_]dh^h]Z^ZlvkyaZdexqZ_lky\lhf
qlh d we_f_glZf kljhdb ihke_^h\Zl_evgh ijbf_gy_lky hi_jZpby bkdexqZxs__ beb <
gb`_ke_^mxs_f Ze]hjblf_ ^h[Z\ey_lky kemqZcgZy dhfihg_glZ qlh[u _s_ memqrblv
j_amevlZl
typedef unsigned char hashIndexType;
unsigned char Rand8[256];
hashIndexType hash(char *str) {
unsigned char h = 0;
while (*str) h = Rand8[h ^ *str++];
return h;
}

A^_kv Rand8  lZ[ebpZ ba  \hkvfb[blh\uo kemqZcguo qbk_e Bo lhqguc ihjy^hd g_
djblbq_gDhjgbwlh]hf_lh^Ze_`Zl\djbilh]jZnbbhghdZaZeky\iheg_wnn_dlb\guf [4].

BkdexqZxs__ BEB ^ey kljhd i_j_f_gghc ^ebgu (jZaf_j lZ[ebpu 65536). ?keb fu
o_rbjm_fkljhdm^\Z`^ufuihemqbfo_ragZq_gb_^eylZ[ebpuex[hc^ebgu^h65536.
Dh]^ZkljhdZo_rbjm_lky\h\lhjhcjZadi_j\hfmkbf\hemijb[Z\ey_lkyIhemqZ_fu_
^\Z[blh\uoqbkeZh[t_^bgyxlky\h^gh[blh\h_
typedef unsigned short int hashIndexType;
unsigned char Rand8[256];
hashIndexType hash(char *str) {
hashIndexType h;
unsigned char h1, h2;
if (*str == 0) return 0;
h1 = *str; h2 = *str + 1;
str++;

17

while (*str) {
h1 = Rand8[h1 ^ *str];
h2 = Rand8[h2 ^ *str];
str++;
}
/* h is in range 0..65535 */
h = ((hashIndexType)h1 << 8)|(hashIndexType)h2;
/* use division method to scale */
return h % hashTableSize
}

JZaf_jo_rlZ[ebpu^he`_g[ulv^hklZlhqgh\_ebdqlh[u\g_chklZ\ZehkvjZamfgh_qbkeh
imkluo f_kl DZd \b^gh ba lZ[ebpu  q_f f_gvr_ lZ[ebpZ l_f [hevr_ kj_^g__ \j_fy ihbkdZ
dexqZ \ g_c O_rlZ[ebpm fh`gh jZkkfZljb\Zlv dZd kh\hdmighklv k\yaZgguo kibkdh\ Ih f_j_
lh]h dZd lZ[ebpZ jZkl_l m\_ebqb\Z_lky dhebq_kl\h kibkdh\ b khhl\_lkl\_ggh kj_^g__ qbkeh
maeh\\dZ`^hfkibkd_mf_gvrZ_lkyImklvdhebq_kl\hwe_f_glh\jZ\ghn.?kebjZaf_jlZ[ebpu
jZ\_g  lh lZ[ebpZ \ujh`^Z_lky \ h^bg kibkhd ^ebgu n. ?keb jZaf_j lZ[ebpu jZ\_g  b
o_rbjh\Zgb_ b^_Zevgh lh gZf ijb^_lky bf_lv ^_eh k ^\mfy kibkdZfb ih n/100 we_f_glh\ \
dZ`^hfDZdfu\b^bf\lZ[ebp_bf__lkyagZqbl_evgZyk\h[h^u\\u[hj_^ebgulZ[ebpu
size
1
2
4
8
16
32
64

time
869
432
214
106
54
28
15

size
128
256
512
1024
2048
4096
8192

time
9
6
4
4
3
3
3

LZ[ebpZ0.1: AZ\bkbfhklvkj_^g_]h\j_f_gbihbkdZ V hl hashTableSize.


Khjlbjmxlkywe_f_glh\

18

J_ZebaZpby
J_ZebaZpby Ze]hjblfZ gZ Kb gZoh^blky \ jZa^_e_  Hi_jZlhju typedef T b compGT ke_^m_l
baf_gblv lZd qlh[u hgb khhl\_lkl\h\Zeb ^Zgguf ojZgbfuf \ fZkkb\_ >ey jZ[hlu ijh]jZffu
ke_^m_l lZd`_ hij_^_eblv hashTableSize b hl\_klb f_klh ih^ hashTable. < o_rnmgdpbb
hash bkihevah\Zg f_lh^ ^_e_gby Nmgdpby insertNode hl\h^bl iZfylv ih^ gh\uc ma_e b
\klZ\ey_l _]h \ lZ[ebpm Nmgdpby deleteNode m^Zey_l ma_e b hk\h[h`^Z_l iZfylv ]^_ hg
jZkiheZ]ZekyNmgdpbyfindNode bs_l\lZ[ebp_aZ^Zggucdexq.

3.2Ihbkd\[bgZjguo^_j_\vyo
< jZa^_e_  fu bkihevah\Zeb ^\hbqguc ihbkd ^ey ihbkdZ ^Zgguo \ fZkkb\_. Wlhl f_lh^
qj_a\uqZcgh wnn_dlb\_g ihkdhevdm dZ`^Zy bl_jZpby \^\h_ mf_gvrZ_l qbkeh we_f_glh\ kj_^b
dhlhjuogZfgm`ghijh^he`ZlvihbkdH^gZdhihkdhevdm^Zggu_ojZgylky\fZkkb\_hi_jZpbb
\klZ\db b m^Ze_gby we_f_glh\ g_ klhev wnn_dlb\gu >\hbqgu_ ^_j_\vy iha\heyxl khojZgblv
wnn_dlb\ghklv \k_o lj_o hi_jZpbc  _keb jZ[hlZ b^_l k kemqZcgufb ^Zggufb < wlhf kemqZ_
\j_fy ihbkdZ hp_gb\Z_lky dZd O(lg n). GZbom^rbc kemqZc  dh]^Z \klZ\eyxlky mihjy^hq_ggu_
^Zggu_<wlhfkemqZ_hp_gdZ\j_fyihbkdZO(n).Ih^jh[ghklb\ugZc^_l_\jZ[hl_Dhjf_gZ [2].

L_hjby
>\hbqgh_^_j_\hwlh^_j_\hmdhlhjh]hdZ`^ucma_ebf__lg_[he__^\mogZke_^gbdh\Ijbf_j
[bgZjgh]h ^_j_\Z ijb\_^_g gZ jbk  Ij_^iheZ]Zy qlh Key kh^_j`bl agZq_gb_ ojZgbfh_ \
^Zgghf mae_ fu fh`_f kdZaZlv qlh [bgZjgh_ ^_j_\h h[eZ^Z_l ke_^mxsbf k\hckl\hf m \k_o
maeh\jZkiheh`_gguoke_\Zhl^Zggh]hmaeZagZq_gb_khhl\_lkl\mxs_]hiheyf_gvr_q_fKey,
m \k_o maeh\ jZkiheh`_gguo kijZ\Z hl g_]h  [hevr_ <_jrbgm ^_j_\Z gZau\Zxl _]h dhjg_f,
maeu m dhlhjuo hlkmlkl\mxl h[Z gZke_^gbdZ gZau\Zxlky ebklvyfb Dhj_gv ^_j_\Z gZ jbk 
kh^_j`bl  Z ebklvy  4, 16, 37 b 43. <ukhlZ ^_j_\Z  wlh ^ebgZ gZb^ebgg_cr_]h ba iml_c hl
dhjgydebklvyf<gZr_fijbf_j_\ukhlZ^_j_\ZjZ\gZ
20

38

16

37

43

Jbk0.2>\hbqgh_^_j_\h
Qlh[ugZclb\^_j_\_dZdh_lhagZq_gb_fuklZjlm_f ba dhjgy b ^\b`_fky \gba GZijbf_j
^eyihbkdZqbkeZfuaZf_qZ_fqlh16 < 20bihlhfmb^_f\e_\hIjb\lhjhfkjZ\g_gbbbf__f
16 > 7, lZdqlhfu^\b`_fky\ijZ\hLj_lvyihiuldZmki_rgZfugZoh^bfwe_f_glkdexqhf
jZ\guf
DZ`^h_ kjZ\g_gb_ \^\h_ mf_gvrZ_l dhebq_kl\h hklZ\rboky we_f_glh\ < wlhf hlghr_gbb
Ze]hjblfihoh`gZ^\hbqgucihbkd\fZkkb\_H^gZdh\k_wlh\_jghlhevdh\kemqZyodh]^ZgZr_
^_j_\h k[ZeZgkbjh\Zgh GZ jbk  ihdZaZgh ^jm]h_ ^_j_\h kh^_j`Zs__ l_ `_ we_f_glu

19

G_kfhljygZlhqlhwlh^_j_\hlh`_[bgZjgh_ihbkd\g_fihoh`kdhj__gZihbkd\h^ghk\yaghf
kibkd_\j_fyihbkdZm\_ebqb\Z_lkyijhihjpbhgZevghqbkemaZihfbgZ_fuowe_f_glh\

4
7
16
20
37
38
43

Jbk0.3G_k[ZeZgkbjh\Zggh_[bgZjgh_^_j_\h

20

<klZ\dZbm^Ze_gb_
Qlh[uemqr_ihgylvdZd^_j_\hklZgh\blkyg_k[ZeZgkbjh\ZggufihkfhljbfgZijhp_kk\klZ\db
ijbklZevg__ Qlh[u \klZ\blv  \ ^_j_\h gZ jbk  fu bs_f wlh qbkeh Ihbkd ijb\h^bl gZk \
ma_e  ]^_ [eZ]hihemqgh aZ\_jrZ_lky Ihkdhevdm 18 > 16, fu ihijhklm ^h[Z\ey_l ma_e  \
dZq_kl\_ijZ\h]hihlhfdZmaeZ jbk 
L_i_jv fu \b^bf dZd \hagbdZ_l g_k[ZeZgkbjh\Zgghklv ^_j_\Z ?keb ^Zggu_ ihklmiZxl \
\hajZklZxs_fihjy^d_dZ`^ucgh\ucma_e^h[Z\ey_lkykijZ\Zhlihke_^g_]h\klZ\e_ggh]hWlh
ijb\h^bl d h^ghfm ^ebgghfm kibkdm H[jZlbl_ \gbfZgb_ q_f [he__ kemqZcgu ihklmiZxsb_
^Zggu_l_f[he__k[ZeZgkbjh\ZggufihemqZ_lky^_j_\h
M^Ze_gby ijhba\h^ylky ijbf_jgh lZd `_  g_h[oh^bfh lhevdh ihaZ[hlblvky h khojZg_gbb
kljmdlmju ^_j_\Z GZijbf_j _keb ba ^_j_\Z gZ jbk  m^Zey_lky ma_e  _]h kgZqZeZ gm`gh
aZf_gblv gZ ma_e  Wlh ^Zkl ^_j_\h bah[jZ`_ggh_ gZ jbk  JZkkm`^_gby a^_kv ijbf_jgh
ke_^mxsb_GZfgm`ghgZclbihlhfdZmaeZkijZ\Zhldhlhjh]hjZkiheh`_gumaeuk[hevrbfb
agZq_gbyfb LZdbf h[jZahf gZf gm`gh \u[jZlv ma_e k gZbf_gvrbf agZq_gb_f jZkiheh`_gguc
kijZ\ZhlmaeZQlh[ugZclb_]hgZfbgm`ghkgZqZeZkimklblvkygZrZ]\ijZ\h ihiZ^Z_f\
ma_e   Z aZl_f gZ rZ] \e_\h ma_e   wlb ^\morZ]h\u_ kimkdb ijh^he`Zxlky ihdZ fu g_
ijb^_f\dhgp_\hcma_eebkl^_j_\Z
20

38

16

37

43

18

Jbk0.4;bgZjgh_^_j_\hihke_^h[Z\e_gbymaeZ

37

38

16

43

18

Jbk0.5;bgZjgh_^_j_\hihke_m^Ze_gbymaeZ

21

J_ZebaZpby
J_ZebaZpby Ze]hjblfZ gZ Kb gZoh^blky \ jZa^_e_  Hi_jZlhju typedef T b compGT ke_^m_l
baf_gblv lZd qlh[u hgb khhl\_lkl\h\Zeb ^Zgguf ojZgbfuf \ ^_j_\_ DZ`^uc ma_e Node
kh^_j`bllZd`_mdZaZl_ebleft, right gZe_\h]hbijZ\h]hihlhfdh\ZlZd`_mdZaZl_evparent
gZ ij_^dZ Kh[kl\_ggh ^Zggu_ ojZgylky \ ihe_ data :^j_k maeZ y\eyxs_]hky dhjg_f ^_j_\Z
ojZgblky \ mdaZl_e_ root agZq_gb_ dhlhjh]h \ kZfhf gZqZe_ _kl_kl\_ggh NULL Nmgdpby
insertNode aZijZrb\Z_l iZfylv ih^ gh\uc ma_e b \klZ\ey_l ma_e \ ^_j_\h l_ mklZgZ\eb\Z_l
gm`gu_ agZq_gby gm`guo mdZaZl_e_c Nmgdpby deleteNode gZijhlb\ m^Zey_l ma_e ba ^_j_\Z
l_ mklZgZ\eb\Z_l gm`gu_ agZq_gby gm`guo mdZaZl_e_c  Z aZl_f hk\h[h`^Z_l iZfylv dhlhjmx
aZgbfZema_eNmgdpbyfindNode bs_l\^_j_\_ma_ekh^_j`ZsbcaZ^Zggh_agZq_gb_

3.3DjZkghq_jgu_^_j_\vy
>\hbqgu_^_j_\vyjZ[hlZxlemqr_\k_]hdh]^Zhgbk[ZeZgkbjh\Zgudh]^Z^ebgZimlbhldhjgy
^h ex[h]h ba ebklv_\ gZoh^blky \ hij_^_e_gguo ij_^_eZo k\yaZgguo k qbkehf maeh\ DjZkgh
q_jgu_^_j_\vyh^bgbakihkh[h\[ZeZgkbjh\db^_j_\v_\GZa\Zgb_ijhbkoh^blhlklZg^Zjlghc
jZkdjZkdb maeh\ lZdbo ^_j_\v_\ \ djZkguc b q_jguc p\_lZ P\_lZ maeh\ bkihevamxlky ijb
[ZeZgkbjh\d_ ^_j_\Z <h \j_fy hi_jZpbc \klZ\db b m^Ze_gby ih^^_j_\vy fh`_l ihgZ^h[blvky
ih\_jgmlv qlh[u ^hklb]gmlv k[ZeZgkbjh\Zgghklb ^_j_\Z Hp_gdhc dZd kj_^g_]h \j_fy lZd b
gZbom^r_]hy\ey_lky O(lg n).
Wlhl jZa^_e  h^bg ba gZb[he__ ljm^guo \ ^Zgghc dgb`d_ ?keb \u hrZe__l_ hl \jZs_gbc
^_j_\v_\ ihijh[mcl_ i_j_clb d ke_^mxs_fm jZa^_em h jZa^_e_gguo kibkdZo Ij_djZkgh
gZibkZggu_jZa^_euhdjZkghq_jguo^_j_\vyo\ugZc^_l_mDhjf_gZ[2].

L_hjby
DjZkghq_jgh_^_j_\hwlh[bgZjgh_^_j_\hkke_^mxsbfbk\hckl\Zfb[2]:
1. DZ`^ucma_eihdjZr_geb[h\q_jguceb[h\djZkgucp\_l.
2. Ebklvyfb h[ty\eyxlky NILmaeu (l_ \bjlmZevgu_ maeu gZke_^gbdb maeh\ dhlhju_
h[uqgh gZau\Zxl ebklvyfb gZ gbo mdZau\Zxl NULL mdZaZl_eb  Ebklvy ihdjZr_gu \
q_jgucp\_l.
3. ?kebma_edjZkguclhh[Z_]hihlhfdZq_jgu.
4. GZ\k_o\_l\yo^_j_\Z\_^msbohl_]hdhjgydebklvyfqbkehq_jguomaeh\h^bgZdh\h.
Dhebq_kl\h q_jguo maeh\ gZ \_l\b hl dhjgy ^h ebklZ gZau\Z_lky q_jghc \ukhlhc ^_j_\Z
I_j_qbke_ggu_ k\hckl\Z ]ZjZglbjmxl qlh kZfZy ^ebggZy \_l\v hl dhjgy d ebklm g_ [he__ q_f
\^\h_^ebgg__ex[hc^jm]hc\_l\bhldhjgydebklmQlh[uihgylvihq_fmwlhlZdjZkkfhljbf
^_j_\h k q_jghc \ukhlhc  DjZlqZcr__ \hafh`gh_ jZkklhygb_ hl dhjgy ^h ebklZ jZ\gh ^\mf
dh]^Zh[ZmaeZq_jgu_>ebgg_cr__jZkklhygb_hldhjgy^hebklZjZ\ghq_luj_f maeu ijbwlhf
ihdjZr_gu hldhjgydebklm lZddjZkgucq_jgucdjZkgucq_jgucKx^Zg_evay^h[Z\blv
q_jgu_ maeu ihkdhevdm ijb wlhf gZjmrblky k\hckl\h  ba dhlhjh]h \ul_dZ_l dhjj_dlghklv
ihgylby q_jghc \ukhlu Ihkdhevdm kh]eZkgh k\hckl\m  m djZkguo maeh\ g_ij_f_ggh q_jgu_
gZke_^gbdb \ ih^h[ghc ihke_^h\Zl_evghklb g_^himklbfu b ^\Z djZkguo maeZ ih^jy^ LZdbf
h[jZahf^ebgg_crbcimlvdhlhjucfufh`_fkdhgkljmbjh\Zlvkhklhblbaq_j_^h\ZgbydjZkguo
b q_jguo maeh\ qlh b ijb\h^bl gZk d m^\h_gghc ^ebg_ imlb ijhoh^ys_]h lhevdh q_j_a q_jgu_
maeu <k_ hi_jZpbb gZ^ ^_j_\hf ^he`gu mf_lv jZ[hlZlv k i_j_qbke_ggufb k\hckl\Zfb <
qZklghklbijb\klZ\d_bm^Ze_gbbwlbk\hckl\Z^he`gukhojZgblvky

22

<klZ\dZ
Qlh[u \klZ\blv ma_e fu kgZqZeZ bs_f \ ^_j_\_ f_klh dm^Z _]h ke_^m_l ^h[Z\blv Gh\uc ma_e
\k_]^Z ^h[Z\ey_lky dZd ebkl ihwlhfm h[Z _]h ihlhfdZ y\eyxlky NILmaeZfb b ij_^iheZ]Zxlky
q_jgufbIhke_\klZ\dbdjZkbfma_e\djZkgucp\_lIhke_wlh]hkfhljbfgZij_^dZbijh\_jy_f
g_ gZjmrZ_lky eb djZkghq_jgh_ k\hckl\h ?keb g_h[oh^bfh fu i_j_djZrb\Z_f ma_e b
ijhba\h^bfih\hjhlqlh[uk[ZeZgkbjh\Zlv^_j_\h
<klZ\b\djZkgucma_efukhojZgy_fk\hckl\hq_jghc\ukhlu k\hckl\h H^gZdhijbwlhf
fh`_lhdZaZlvkygZjmr_ggufk\hckl\hWlhk\hckl\hkhklhbl\lhfqlhh[ZihlhfdZdjZkgh]h
maeZ h[yaZl_evgh q_jgu < gZr_f kemqZ_ h[Z ihlhfdZ gh\h]h maeZ q_jgu ih hij_^_e_gbx
ihkdhevdm hgb y\eyxlky NILmaeZfb  lZd qlh jZkkfhljbf kblmZpbx dh]^Z ij_^hd gh\h]h maeZ
djZkgucijbwlhf[m^_lgZjmr_ghk\hckl\h>hklZlhqghjZkkfhlj_lvke_^mxsb_^\ZkemqZy

DjZkguc ij_^hd djZkguc ^y^y KblmZpbx djZkgucdjZkguc beexkljbjm_l jbk  M


gh\h]hmaeZ Xij_^hdb^y^y hdZaZebkv djZkgufb Ijhklh_ i_j_djZrb\Zgb_ ba[Z\ey_l
gZkhldjZkghdjZkgh]hgZjmr_gbyIhke_i_j_djZkdbgm`ghijh\_jblv^_^mrdmgh\h]h
maeZ ma_e B) ihkdhevdm hg fh`_l hdZaZlvky djZkguf H[jZlbl_ \gbfZgb_ gZ
jZkijhkljZg_gb_\ebygbydjZkgh]hmaeZgZ\_jogb_maeu^_j_\Z<kZfhfdhgp_dhj_gvfu
djZkbf \ q_jguc p\_l dhj_gv ^_j_\Z ?keb hg [ue djZkguf lh ijb wlhf m\_ebqb\Z_lky
q_jgZy\ukhlZ^_j_\Z

DjZkgucij_^hdq_jguc^y^yGZjbkij_^klZ\e_g^jm]hc\ZjbZgldjZkghdjZkgh]h
gZjmr_gby  ^y^y gh\h]h maeZ hdZaZeky q_jguf A^_kv maeu fh`_l ihgZ^h[blvky
\jZsZlv qlh[u kdhjj_dlbjh\Zlv ih^^_j_\vy < wlhf f_kl_ Ze]hjblf fh`_l hklZgh\blvky
baaZ hlkmlkl\by djZkghdjZkguo dhgnebdlh\ b \_jrbgZ ^_j_\Z ma_e A) hdjZrb\Z_lky \
q_jguc p\_l H[jZlbl_ \gbfZgb_ qlh _keb ma_e X [ue \ gZqZe_ ijZ\uf ihlhfdhf lh
i_j\ufijbf_gy_lkye_\h_\jZs_gb_dhlhjh_^_eZ_lwlhlma_ee_\ufihlhfdhf

DZ`^Zydhjj_dlbjh\dZijhba\h^bfZyijb\klZ\d_maeZaZklZ\ey_lgZkih^gylvky\^_j_\_gZh^bg
rZ]<wlhfkemqZ_^hhklZgh\dbZe]hjblfZ[m^_lk^_eZgh\jZs_gb_ _kebma_e[ueijZ\uf
ihlhfdhf F_lh^m^Ze_gbyZgZeh]bq_g

23

B
black

C
red

red

parent

uncle

X
red

B
red

C
black

black

X
red

Jbk0.6<klZ\dZDjZkgucij_^hddjZkguc^y^y

24

B
black

C
red
parent

black

uncle

X
red

A
black

X
red

red

C
black

Jbk0.7<klZ\dZdjZkgucij_^hdq_jguc^y^y

25

J_ZebaZpby
J_ZebaZpbyjZ[hlukdjZkghq_jgufb^_j_\vyfbgZKbgZoh^blky\jZa^_e_Hi_jZlhjutypedef
T Z lZd`_ kjZ\gb\Zxsb_ compLT b compEQ ke_^m_l baf_gblv lZd qlh[u hgb khhl\_lkl\h\Zeb
^ZggufojZgbfuf\maeZo^_j_\Z<dZ`^hfmae_Node ojZgylkymdZaZl_ebleft, right gZ^\mo
ihlhfdh\bparent gZij_^dZP\_lmaeZojZgblky\ihe_colorbfh`_l[ulveb[hREDeb[h
BLACK Kh[kl\_ggh ^Zggu_ ojZgylky \ ihe_ data <k_ ebklvy ^_j_\Z y\eyxlky klhjh`_\ufb
(sentinel  qlh kbevgh mijhsZ_l dh^u Ma_e root y\ey_lky dhjg_f ^_j_\Z b \ kZfhf gZqZe_
y\ey_lkyklhjh`_\uf
NmgdpbyinsertNode aZijZrb\Z_liZfylvih^gh\ucma_emklZgZ\eb\Z_lgm`gu_agZq_gby
_]hihe_cb\klZ\ey_l\^_j_\hKhhl\_lkl\_gghhgZ\uau\Z_linsertFixupdhlhjZyke_^blaZ
khojZg_gb_f djZkghq_jguo k\hckl\ Nmgdpby deleteNode m^Zey_l ma_e ba ^_j_\Z HgZ
\uau\Z_ldeleteFixupdhlhjZy\hkklZgZ\eb\Z_ldjZkghq_jgu_k\hckl\ZNmgdpbyfindNode
bs_l\^_j_\_gm`gucma_e

3.4JZa^_e_ggu_kibkdb
JZa^_e_ggu_ kibkdb  wlh k\yagu_ kibkdb dhlhju_ iha\heyxl \Zf iju]gmlv skip  d gm`ghfm
we_f_glm Wlh iha\hey_l ij_h^he_lv h]jZgbq_gby ihke_^h\Zl_evgh]h ihbkdZ y\eyxs_]hky
hkgh\guf bklhqgbdhf g_wnn_dlb\gh]h ihbkdZ \ kibkdZo < lh `_ \j_fy \klZ\dZ b m^Ze_gb_
hklZxlky kjZ\gbl_evgh wnn_dlb\gufb Hp_gdZ kj_^g_]h \j_f_gb ihbkdZ \ lZdbo kibkdZo _klv
O(lg n). >eygZbom^r_]hkemqZyhp_gdhcy\ey_lkyO(n),ghom^rbckemqZc djZcg_ fZeh\_jhyl_g
Hlebqgh_\\_^_gb_\jZa^_e_ggu_kibkdb\ugZc^_l_mIvx [5].

L_hjby
B^_ye_`ZsZy\hkgh\_jZa^_e_gguokibkdh\hq_gvgZihfbgZ_lf_lh^bkihevam_fucijbihbkd_
bf_g \ Z^j_kghc dgb`d_ Qlh[u gZclb bfy \u ihf_qZ_l_ [md\hc kljZgbpm hldm^Z gZqbgZxlky
bf_gZ gZqbgZxsb_ky k wlhc [md\u GZ jbk  gZijbf_j kZfuc \_jogbc kibkhd ij_^klZ\ey_l
h[uqguc h^ghk\yaguc kibkhd >h[Z\b\ h^bg mjh\_gv kkuehd fu mkdhjbf ihbkd KgZqZeZ fu
ihc^_fihkkuedZfmjh\gyaZl_f,dh]^Z^hc^_fihgm`gh]hhlj_adZkibkdZihc^_fihkkuedZf
gme_\h]hmjh\gy
WlZijhklZyb^_yfh`_l[ulvjZkrbj_gZfufh`_f^h[Z\blvgm`gh_qbkehmjh\g_c<gbam
gZ jbk  fu \b^bf \lhjhc mjh\_gv dhlhjuc iha\hey_l ^\b]Zlvky _s_ [uklj__ i_j\h]h Ijb
ihbkd_ we_f_glZ fu ^\b]Z_fky ih wlhfm mjh\gx ihdZ g_ ^hc^_f ^h gm`gh]h hlj_adZ kibkdZ
AZl_f fu _s_ mf_gvrZ_f bgl_j\Ze g_hij_^_e_gghklb ^\b]Zykv ih kkuedZf ]h mjh\gy Ebrv
ihke_wlh]hfuijhoh^bfihkkuedZf]hmjh\gy
<klZ\eyy ma_e gZf ihgZ^h[blky hij_^_eblv dhebq_kl\h bkoh^ysbo hl g_]h kkuehd WlZ
ijh[e_fZe_]q_\k_]hj_rZ_lkykbkihevah\Zgb_fkemqZcgh]hf_oZgbafZijb^h[Z\e_gbbgh\h]h
maeZ fu [jhkZ_f fhg_lm qlh[u hij_^_eblv gm`gh eb ^h[Z\eylv _s_ kehc GZijbf_j fu
fh`_f ^h[Z\eylv hq_j_^gu_ kehb ^h l_o ihj ihdZ \uiZ^Z_l j_rdZ ?keb j_Zebah\Zg lhevdh
h^bgmjh\_gvfubf__f^_ehnZdlbq_kdbkh[uqgufkibkdhfb\j_fyihbkdZ_klvO(n)H^gZdh
_kebbf__lky^hklZlhqgh_qbkehmjh\g_cjZa^_e_gguckibkhdfh`ghkqblZlv^_j_\hfkdhjg_fgZ
\ukr_fmjh\g_Z^ey^_j_\Z\j_fyihbkdZ_klv O(lg n).
Ihkdhevdmj_ZebaZpbyjZa^_e_gguokibkdh\\dexqZ_l\k_[ykemqZcgucijhp_kk^ey\j_f_gb
ihbkdZ \ gbo mklZgZ\eb\Zxlky \_jhylghklgu_ ]jZgbpu Ijb h[uqguo mkeh\byo wlb ]jZgbpu
^h\hevghmadbGZijbf_jdh]^Zfubs_fwe_f_gl\kibkd_bamaeh\\_jhylghklvlh]hqlh
\j_fyihbkdZhdZ`_lky\jZa[hevr_kj_^g_]hfh`ghhp_gblvdZd 1,000,000,000,000,000,000[5].

26

abe

art

ben

bob

cal

cat

dan

don

abe

art

ben

bob

cal

cat

dan

don

abe

art

ben

bob

cal

cat

dan

don

0
1

0
1
2

Jbk0.8Mkljhckl\hjZa^_e_ggh]hkibkdZ

J_ZebaZpby
J_ZebaZpbyjZ[hlukjZa^_e_ggufbkibkdZfbgZKbgZoh^blky\jZa^_e_Hi_jZlhjutypedef T,
ZlZd`_compLT b compEQ ke_^m_lbaf_gblvlZdqlh[uhgbkhhl\_lkl\h\Zeb^ZggufojZgbfuf
\maeZokibkdZDjhf_lh]hihgZ^h[blky\u[jZlvdhgklZglmMAXLEVEL__agZq_gb_\u[bjZ_lky\
aZ\bkbfhklbhlh`b^Z_fh]hh[t_fZ^Zgguo
NmgdpbyinitList \uau\Z_lky\kZfhfgZqZe_HgZhl\h^bliZfylvih^aZ]heh\hdkibkdZb
bgbpbZebabjm_l_]hiheyIjbagZdhflh]hqlhkibkhdimklNmgdpbyinsertNode kha^Z_lgh\uc
ma_e b \klZ\ey_l _]h \ kibkhd Dhg_qgh insertNode kgZqZeZ hlukdb\Z_l f_klh \ kibkd_ dm^Z
ma_e^he`_g[ulv\klZ\e_g <fZkkb\_update nmgdpbymqblu\Z_l\klj_lb\rb_kykkuedbgZmaeu
\_jogbomjh\g_cWlZbgnhjfZpby\^Zevg_cr_fbkihevam_lky^eydhjj_dlghcmklZgh\dbkkuehd
gh\h]h maeZ >ey wlh]h maeZ k ihfhsvx ]_g_jZlhjZ kemqZcguo qbk_e hij_^_ey_lky agZq_gb_
newLevel ihke_ q_]h hl\h^blky iZfylv ^ey maeZ Kkuedb \i_j_^ mklZgZ\eb\Zxlky ih
bgnhjfZpbb kh^_j`Zs_c \ fZkkb\_ update Nmgdpby deleteNode m^Zey_l maeu ba kibkdZ b
hk\h[h`^Z_laZgbfZ_fmxbfbiZfylvHgZj_Zebah\ZgZZgZeh]bqghnmgdpbbfindNode blZd`_
bs_l\kibkd_m^Zey_fucma_e

27

3.5KjZ\g_gb_f_lh^h\
Fu jZkkfhlj_eb g_kdhevdh f_lh^h\ hj]ZgbaZpbb keh\Zj_c o_rlZ[ebpu g_k[ZeZgkbjh\Zggu_
^\hbqgu_ ^_j_\vy djZkghq_jgu_ ^_j_\vy b jZa^_e_ggu_ kibkdb Bf_xlky g_kdhevdh nZdlhjh\
dhlhju_\ebyxlgZ\u[hjZe]hjblfZ\dhgdj_lghckblmZpbb

Khjlbjh\dZ j_amevlZlZ ?keb j_amevlZl ^he`_g [ulv hlkhjlbjh\Zg o_rlZ[ebpu


ij_^klZ\eyxlky g_ \iheg_ ih^oh^ysbfb ihkdhevdm bo we_f_glu aZghkylky \ lZ[ebpm \
ihjy^d_ hij_^_ey_fhf lhevdh bo o_ragZq_gbyfb <k_ h[klhbl kh\k_f ih^jm]hfm ijb
jZ[hl_ k ^\hbqgufb ^_j_\vyfb Ijb ijhoh^_ ^_j_\Z \ h[jZlghf ihjy^d_1 fu ihemqZ_f
hlkhjlbjh\Zgguckibkhd<hldZdwlh^_eZ_lky
void WalkTree(Node *P) {
if (P == NIL) return;
WalkTree(P->Left);
/* A^_kvbkke_^m_f P->Data */
WalkTree(P->Right);
}
WalkTree(Root);

Qlh[u ihemqblv hlkhjlbjh\Zgguc kibkhd maeh\ jZa^_e_ggh]h kibkdZ ^hklZlhqgh


ijhclbkvihkkuedZfgme_\h]hmjh\gy<hllZd:
Node *P = List.Hdr->Forward[0];
while (P != NIL) {
/* A^_kvbkke_^m_f P->Data */
P = P->Forward[0];
}

IZfylvFbgbfbaZpbyiZfylbdhlhjZymoh^blgZgZdeZ^gu_jZkoh^uhkh[_ggh\Z`gZ
dh]^Zgm`ghojZgblvfgh]hfZe_gvdbomaeh\
>ey o_rlZ[ebp lj_[m_lky lhevdh h^bg mdZaZl_ev gZ ma_e Djhf_ lh]h lj_[m_lky
iZfylvih^kZfmlZ[ebpm
>ey djZkghq_jguo ^_j_\v_\ \ dZ`^hf mae_ gm`gh ojZgblv kkuedm gZ e_\h]h b
ijZ\h]h ihlhfdZ Z lZd`_ kkuedm gZ ij_^dZ Djhf_ lh]h ]^_lh gm`gh ojZgblv b
p\_lmaeZOhlygZp\_l^hklZlhq_glhevdhh^bg[blbaaZ\ujZ\gb\Zgbykljmdlmj
lj_[m_fh]h ^ey wnn_dlb\ghklb ^hklmiZ dZd ijZ\beh [m^_l ihljZq_gh [hevr_
f_klZ LZdbf h[jZahf dZ`^uc ma_e djZkghq_jgh]h ^_j_\Z lj_[m_l iZfylb
dhlhjhco\Zlbeh[ugZmdZaZl_ey
>ey jZa^_e_gguo kibkdh\ \ dZ`^hf mae_ bf__lky kkuedZ gme_\h]h mjh\gy
<_jhylghklv bf_lv kkuedm mjh\gy  jZ\gZ S <_jhylghklv bf_lv kkuedm mjh\gy 
jZ\gZj.<h[s_fdhebq_kl\hkkuehdgZma_ejZ\gh

L_jfbgheh]by>DgmlZ li postorder.<hjb]bgZe_^ZgghcjZ[hluwlhl`_ihjy^hdgZa\Zgin-order.

28

<j_fy :e]hjblf ^he`_g [ulv wnn_dlb\guf Wlh hkh[_ggh \Z`gh dh]^Z h`b^Zxlky
[hevrb_ h[t_fu ^Zgguo < lZ[ebp_  kjZ\gb\Zxlky \j_f_gZ ihbkdZ ^ey dZ`^h]h
Ze]hjblfZ H[jZlbl_ \gbfZgb_ gZ lh qlh gZbom^rb_ kemqZb ^ey o_rlZ[ebp b
jZa^_e_gguo kibkdh\ qj_a\uqZcgh fZeh\_jhylgu Wdki_jbf_glZevgu_ ^Zggu_ hibkZgu

n = 1+

1 1
+ +  =2.
2 4

gb`_

IjhklhlZ ?keb Ze]hjblf dhjhlhd b ijhkl ijb _]h j_ZebaZpbb bbeb bkihevah\Zgbb
hrb[db [m^ml ^hims_gu k f_gvr_c \_jhylghklvx Djhf_ lh]h wlh h[e_]qZ_l ijh[e_fu
khijh\h`^_gby ijh]jZff Dhebq_kl\Z hi_jZlhjh\ bkihegy_fuo \ dZ`^hf Ze]hjblf_
lZd`_kh^_j`Zlky\lZ[ebp_
f_lh^

hi_jZlhju

kj_^g__\j_fy

o_rlZ[ebpu
g_k[ZeZgkbjh\Zggu_
^_j_\vy
djZkghq_jgu_
^_j_\vy
jZa^_e_ggu_kibkdb

26
41

O(1)
O(lg n)

\j_fy \ om^r_f
kemqZ_
O(n)
O(n)

120

O(lg n)

O(lg n)

55

O(lg n)

O(n)

LZ[ebpZ0.2KjZ\g_gb_Ze]hjblfh\\_^_gbykeh\Zj_c
< lZ[ebp_  ijb\_^_gu \j_f_gZ lj_[m_fu_ ^ey \klZ\db ihbkdZ b m^Ze_gby 65536 (216)
kemqZcguo we_f_glh\ < wlbo wdki_jbf_glZo jZaf_j o_rlZ[ebpu jZ\gyeky10009 ^ey
jZa^_e_gguokibkdh\^himkdZehkv^hmjh\g_ckkuehdDhg_qgh\j_ZevguokblmZpbyo\j_f_gZ
fh]mlhlebqZlvkyhlijb\_^_gguoa^_kvh^gZdhlZ[ebpZ^Z_l^hklZlhqghbgnhjfZpbb^ey\u[hjZ
ih^oh^ys_]hZe]hjblfZ
f_lh^
o_rlZ[ebpu
g_k[ZeZgkbjh\Zggu_
^_j_\vy
djZkghq_jgu_
^_j_\vy
jZa^_e_ggu_kibkdb

\klZ\dZ
18
37

ihbkd
8
17

m^Ze_gb_
10
26

40

16

37

48

31

35

LZ[ebpZ0.3Kj_^g__\j_fy fk_d ^ey kemqZcguowe_f_glh\


< lZ[ebp_  ijb\_^_gu kj_^gb_ \j_f_gZ ihbkdZ ^ey ^\mo kemqZ_\ kemqZcguo ^Zgguo b
mihjy^hq_gguo agZq_gby dhlhjuo ihklmiZeb \ \hajZklZxs_f ihjy^d_ Mihjy^hq_ggu_ ^Zggu_
y\eyxlkygZbom^rbfkemqZ_f^eyg_k[ZeZgkbjh\Zgguo^_j_\v_\ihkdhevdm^_j_\h\ujh`^Z_lky\
h[uqguc h^ghk\yaguc kibkhd Ijb\_^_gu \j_f_gZ ^ey h^bghqgh]h ihbkdZ ?keb [u gZf
ihgZ^h[behkvgZclb\k_we_f_glh\lhdjZkghq_jgufm^_j_\mihgZ^h[behkv[uk_dmg^Z
g_k[ZeZgkbjh\ZgghfmhdhehqZkZ

29

We_f_g
lh\
kemqZcgu_
^Zggu_
mihjy^hq_ggu_
^Zggu_

16
256
4,096
65,536
16
256
4,096
65,536

o_r
lZ[ebpu

g_k[ZeZgkbjh\Zg
gu_^_j_\vy
4
3
3
8
3
3
3
7

3
4
7
17
4
47
1,033
55,019

LZ[ebpZ0.4Kj_^g__\j_fyihbkdZ fk_d

30

jZa^_e_ggu_
kibkdb

djZkgh
q_jgu_
^_j_\vy
2
4
6
16
2
4
6
9

5
9
12
31
4
7
11
15

4. L_dkluijh]jZff
4.1Dh^u^eykhjlbjh\db\klZ\dZfb
typedef int T;
typedef int tblIndex;

/* type of item to be sorted */


/* type of subscript */

#define compGT(a,b) (a > b)


void insertSort(T *a, tblIndex lb, tblIndex ub) {
T t;
tblIndex i, j;
/**************************
* sort array a[lb..ub] *
**************************/
for (i = lb + 1; i <= ub; i++) {
t = a[i];
/* Shift elements down until */
/* insertion point found.
*/
for (j = i-1; j >= lb && compGT(a[j], t); j--)
a[j+1] = a[j];
/* insert */
a[j+1] = t;
}
}

31

4.2Dh^u^eykhjlbjh\dbR_eeZ
typedef int T;
typedef int tblIndex;

/* type of item to be sorted */


/* type of subscript */

#define compGT(a,b) (a > b)


void shellSort(T *a, tblIndex lb, tblIndex ub) {
tblIndex n, h, i, j;
T t;
/**************************
* sort array a[lb..ub] *
**************************/
/* compute largest increment */
n = ub - lb + 1;
h = 1;
if (n < 14)
h = 1;
else if (sizeof(tblIndex) == 2 && n > 29524)
h = 3280;
else {
while (h < n) h = 3*h + 1;
h /= 3;
h /= 3;
}
while (h > 0) {
/* sort-by-insertion in increments of h */
for (i = lb + h; i <= ub; i++) {
t = a[i];
for (j = i-h; j >= lb && compGT(a[j], t); j -= h)
a[j+h] = a[j];
a[j+h] = t;
}
/* compute next increment */
h /= 3;
}
}

32

4.3Dh^u^ey[ukljh]hihbkdZ nmgdpbb4XLFNVRUW
typedef int T;
typedef int tblIndex;

/* type of item to be sorted */


/* type of subscript */

#define CompGT(a,b) (a > b)


tblIndex partition(T *a, tblIndex lb, tblIndex ub) {
T t, pivot;
tblIndex i, j, p;
/*******************************
* partition array a[lb..ub] *
*******************************/
/* select pivot and exchange with 1st element */
p = lb + ((ub - lb)>>1);
pivot = a[p];
a[p] = a[lb];
/* sort lb+1..ub based on pivot */
i = lb+1;
j = ub;
while (1) {
while (i < j && compGT(pivot, a[i])) i++;
while (j >= i && compGT(a[j], pivot)) j--;
if (i >= j) break;
t = a[i];
a[i] = a[j];
a[j] = t;
j--; i++;
}
/* pivot belongs in a[j] */
a[lb] = a[j];
a[j] = pivot;
return j;
}
void quickSort(T *a, tblIndex lb, tblIndex ub) {
tblIndex m;
/**************************
* sort array a[lb..ub] *
**************************/
while (lb < ub) {
/* quickly sort short lists */
if (ub - lb <= 12) {
insertSort(a, lb, ub);
return;

33

}
/* partition into two segments */
m = partition (a, lb, ub);
/* sort the smallest partition
*/
/* to minimize stack requirements */
if (m - lb <= ub - m) {
quickSort(a, lb, m - 1);
lb = m + 1;
} else {
quickSort(a, m + 1, ub);
ub = m - 1;
}
}
}

34

4.4Dh^u^eyklZg^Zjlghcj_ZebaZpbb[ukljh]hihbkdZ
#include <limits.h>
#define MAXSTACK (sizeof(size_t) * CHAR_BIT)
static void exchange(void *a, void *b, size_t size) {
size_t i;
/******************
* exchange a,b *
******************/
for (i = sizeof(int); i <= size; i += sizeof(int)) {
int t = *((int *)a);
*(((int *)a)++) = *((int *)b);
*(((int *)b)++) = t;
}
for (i = i - sizeof(int) + 1; i <= size; i++) {
char t = *((char *)a);
*(((char *)a)++) = *((char *)b);
*(((char *)b)++) = t;
}
}
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *)) {
void *lbStack[MAXSTACK], *ubStack[MAXSTACK];
int sp;
unsigned int offset;
lbStack[0] = (char *)base;
ubStack[0] = (char *)base + (nmemb-1)*size;
for (sp = 0; sp >= 0; sp--) {
char *lb, *ub, *m;
char *P, *i, *j;
lb = lbStack[sp];
ub = ubStack[sp];
while (lb < ub) {
/* select pivot and exchange with 1st element */
offset = (ub - lb) >> 1;
P = lb + offset - offset % size;
exchange (lb, P, size);
/* partition into two segments */
i = lb + size;
j = ub;
while (1) {
while (i < j && compar(lb, i) > 0) i += size;
while (j >= i && compar(j, lb) > 0) j -= size;
if (i >= j) break;

35

exchange (i, j, size);


j -= size;
i += size;
}
/* pivot belongs in A[j] */
exchange (lb, j, size);
m = j;
/* keep processing smallest segment, and stack largest */
if (m - lb <= ub - m) {
if (m + size < ub) {
lbStack[sp] = m + size;
ubStack[sp++] = ub;
}
ub = m - size;
} else {
if (m - size > lb) {
lbStack[sp] = lb;
ubStack[sp++] = m - size;
}
lb = m + size;
}
}
}
}

4.5Dh^u^eyo_rlZ[ebp
typedef int T;
#define compEQ(a,b) (a == b)
typedef struct Node_ {
struct Node_ *next;
T data;
} Node;

/* type of item to be sorted */

/* next node */
/* data stored in node */

typedef int hashTableIndex;


Node **hashTable;
int hashTableSize;
hashTableIndex hash(T data) {
/***********************************
* hash function applied to data *
***********************************/
return (data % hashTableSize);
}
Node *insertNode(T data) {
Node *p, *p0;
hashTableIndex bucket;

36

/************************************************
* allocate node for data and insert in table *
************************************************/
/* insert node at beginning of list */
bucket = hash(data);
if ((p = malloc(sizeof(Node))) == 0) {
fprintf (stderr, "out of memory (insertNode)\n");
exit(1);
}
p0 = hashTable[bucket];
hashTable[bucket] = p;
p->next = p0;
p->data = data;
return p;
}
void deleteNode(T data) {
Node *p0, *p;
hashTableIndex bucket;
/********************************************
* delete node containing data from table *
********************************************/
/* find node */
p0 = 0;
bucket = hash(data);
p = hashTable[bucket];
while (p && !compEQ(p->data, data)) {
p0 = p;
p = p->next;
}
if (!p) return;
/* p designates node to delete, remove it from list */
if (p0)
/* not first node, p0 points to previous node */
p0->next = p->next;
else
/* first node on chain */
hashTable[bucket] = p->next;
free (p);
}
Node *findNode (T data) {
Node *p;
/*******************************
* find node containing data *
*******************************/
p = hashTable[hash(data)];
while (p && !compEQ(p->data, data))

37

p = p->next;
return p;
}

4.6Dh^u^ey[bgZjguo^_j_\v_\
typedef int T;
#define compLT(a,b) (a < b)
#define compEQ(a,b) (a == b)
typedef struct Node_ {
struct Node_ *left;
struct Node_ *right;
struct Node_ *parent;
T data;
} Node;
Node *root = NULL;

/* type of item to be sorted */

/*
/*
/*
/*

left child */
right child */
parent */
data stored in node */

/* root of binary tree */

Node *insertNode(T data) {


Node *x, *current, *parent;
/***********************************************
* allocate node for data and insert in tree *
***********************************************/
/* find x's parent */
current = root;
parent = 0;
while (current) {
if (compEQ(data, current->data)) return (current);
parent = current;
current = compLT(data, current->data) ?
current->left : current->right;
}
/* setup new node */
if ((x = malloc (sizeof(*x))) == 0) {
fprintf (stderr, "insufficient memory (insertNode)\n");
exit(1);
}
x->data = data;
x->parent = parent;
x->left = NULL;
x->right = NULL;
/* insert x in tree */
if(parent)
if(compLT(x->data, parent->data))
parent->left = x;
else
parent->right = x;
else
root = x;

38

return(x);
}
void deleteNode(Node *z) {
Node *x, *y;
/*****************************
* delete node z from tree *
*****************************/
/* y will be removed from the parent chain */
if (!z || z == NULL) return;
/* find tree successor */
if (z->left == NULL || z->right == NULL)
y = z;
else {
y = z->right;
while (y->left != NULL) y = y->left;
}
/* x is y's only child */
if (y->left != NULL)
x = y->left;
else
x = y->right;
/* remove y from the parent chain */
if (x) x->parent = y->parent;
if (y->parent)
if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
else
root = x;
/*
/*
/*
if

y is the node we're removing */


z is the data we're removing */
if z and y are not the same, replace z with y. */
(y != z) {
y->left = z->left;
if (y->left) y->left->parent = y;
y->right = z->right;
if (y->right) y->right->parent = y;
y->parent = z->parent;
if (z->parent)
if (z == z->parent->left)
z->parent->left = y;
else
z->parent->right = y;
else
root = y;
free (z);
} else {
free (y);

39

}
}
Node *findNode(T data) {
/*******************************
* find node containing data *
*******************************/
Node *current = root;
while(current != NULL)
if(compEQ(data, current->data))
return (current);
else
current = compLT(data, current->data) ?
current->left : current->right;
return(0);
}

4.7Dh^u^eydjZkghq_jguo^_j_\v_\
typedef int T;
#define compLT(a,b) (a < b)
#define compEQ(a,b) (a == b)

/* type of item to be sorted */

/* Red-Black tree description */


typedef enum { BLACK, RED } nodeColor;
typedef struct Node_ {
struct Node_ *left;
struct Node_ *right;
struct Node_ *parent;
nodeColor color;
T data;
} Node;

/*
/*
/*
/*
/*

left child */
right child */
parent */
node color (BLACK, RED) */
data stoRED in node */

#define NIL &sentinel


/* all leafs are sentinels */
Node sentinel = { NIL, NIL, 0, BLACK, 0};
Node *root = NIL;

/* root of Red-Black tree */

void rotateLeft(Node *x) {


/**************************
* rotate node x to left *
**************************/
Node *y = x->right;
/* establish x->right link */
x->right = y->left;
if (y->left != NIL) y->left->parent = x;
/* establish y->parent link */
if (y != NIL) y->parent = x->parent;

40

if (x->parent) {
if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
} else {
root = y;
}
/* link x and y */
y->left = x;
if (x != NIL) x->parent = y;
}
void rotateRight(Node *x) {
/****************************
* rotate node x to right *
****************************/
Node *y = x->left;
/* establish x->left link */
x->left = y->right;
if (y->right != NIL) y->right->parent = x;
/* establish y->parent link */
if (y != NIL) y->parent = x->parent;
if (x->parent) {
if (x == x->parent->right)
x->parent->right = y;
else
x->parent->left = y;
} else {
root = y;
}
/* link x and y */
y->right = x;
if (x != NIL) x->parent = y;
}
void insertFixup(Node *x) {
/*************************************
* maintain Red-Black tree balance *
* after inserting node x
*
*************************************/
/* check Red-Black properties */
while (x != root && x->parent->color == RED) {
/* we have a violation */
if (x->parent == x->parent->parent->left) {
Node *y = x->parent->parent->right;
if (y->color == RED) {

41

/* uncle is RED */
x->parent->color = BLACK;
y->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
} else {
/* uncle is BLACK */
if (x == x->parent->right) {
/* make x a left child */
x = x->parent;
rotateLeft(x);
}
/* recolor and rotate */
x->parent->color = BLACK;
x->parent->parent->color = RED;
rotateRight(x->parent->parent);
}
} else {
/* mirror image of above code */
Node *y = x->parent->parent->left;
if (y->color == RED) {
/* uncle is RED */
x->parent->color = BLACK;
y->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
} else {
/* uncle is BLACK */
if (x == x->parent->left) {
x = x->parent;
rotateRight(x);
}
x->parent->color = BLACK;
x->parent->parent->color = RED;
rotateLeft(x->parent->parent);
}
}
}
root->color = BLACK;
}
Node *insertNode(T data) {
Node *current, *parent, *x;
/***********************************************
* allocate node for data and insert in tree *
***********************************************/
/* find where node belongs */
current = root;
parent = 0;

42

while (current != NIL) {


if (compEQ(data, current->data)) return (current);
parent = current;
current = compLT(data, current->data) ?
current->left : current->right;
}
/* setup new node */
if ((x = malloc (sizeof(*x))) == 0) {
printf ("insufficient memory (insertNode)\n");
exit(1);
}
x->data = data;
x->parent = parent;
x->left = NIL;
x->right = NIL;
x->color = RED;
/* insert node in tree */
if(parent) {
if(compLT(data, parent->data))
parent->left = x;
else
parent->right = x;
} else {
root = x;
}
insertFixup(x);
return(x);
}
void deleteFixup(Node *x) {
/*************************************
* maintain Red-Black tree balance *
* after deleting node x
*
*************************************/
while (x != root && x->color == BLACK) {
if (x == x->parent->left) {
Node *w = x->parent->right;
if (w->color == RED) {
w->color = BLACK;
x->parent->color = RED;
rotateLeft (x->parent);
w = x->parent->right;
}
if (w->left->color == BLACK && w->right->color == BLACK) {
w->color = RED;
x = x->parent;
} else {
if (w->right->color == BLACK) {
w->left->color = BLACK;
w->color = RED;
rotateRight (w);

43

w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->right->color = BLACK;
rotateLeft (x->parent);
x = root;
}
} else {
Node *w = x->parent->left;
if (w->color == RED) {
w->color = BLACK;
x->parent->color = RED;
rotateRight (x->parent);
w = x->parent->left;
}
if (w->right->color == BLACK && w->left->color == BLACK) {
w->color = RED;
x = x->parent;
} else {
if (w->left->color == BLACK) {
w->right->color = BLACK;
w->color = RED;
rotateLeft (w);
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->left->color = BLACK;
rotateRight (x->parent);
x = root;
}
}
}
x->color = BLACK;
}
void deleteNode(Node *z) {
Node *x, *y;
/*****************************
* delete node z from tree *
*****************************/
if (!z || z == NIL) return;

if (z->left == NIL || z->right == NIL) {


/* y has a NIL node as a child */
y = z;
} else {
/* find tree successor with a NIL node as a child */
y = z->right;
while (y->left != NIL) y = y->left;
}

44

/* x is y's only child */


if (y->left != NIL)
x = y->left;
else
x = y->right;
/* remove y from the parent chain */
x->parent = y->parent;
if (y->parent)
if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
else
root = x;
if (y != z) z->data = y->data;

if (y->color == BLACK)
deleteFixup (x);
free (y);
}
Node *findNode(T data) {
/*******************************
* find node containing data *
*******************************/
Node *current = root;
while(current != NIL)
if(compEQ(data, current->data))
return (current);
else
current = compLT (data, current->data) ?
current->left : current->right;
return(0);
}

4.8Dh^u^eyjZa^_e_gguokibkdh\
typedef int T;
#define compLT(a,b) (a < b)
#define compEQ(a,b) (a == b)

/* type of item to be sorted */

/*
* levels range from (0 .. MAXLEVEL)
*/
#define MAXLEVEL 15
typedef struct Node_ {
T data;
struct Node_ *forward[1];

/* user's data */
/* skip list forward pointer */

45

} Node;
typedef struct {
Node *hdr;
int listLevel;
} SkipList;

/* list Header */
/* current level of list */

SkipList list;

/* skip list information */

#define NIL list.hdr


Node *insertNode(T data) {
int i, newLevel;
Node *update[MAXLEVEL+1];
Node *x;
/***********************************************
* allocate node for data and insert in list *
***********************************************/
/* find where data belongs */
x = list.hdr;
for (i = list.listLevel; i >= 0; i--) {
while (x->forward[i] != NIL
&& compLT(x->forward[i]->data, data))
x = x->forward[i];
update[i] = x;
}
x = x->forward[0];
if (x != NIL && compEQ(x->data, data)) return(x);
/* determine level */
newLevel = 0;
while (rand() < RAND_MAX/2) newLevel++;
if (newLevel > MAXLEVEL) newLevel = MAXLEVEL;
if (newLevel > list.listLevel) {
for (i = list.listLevel + 1; i <= newLevel; i++)
update[i] = NIL;
list.listLevel = newLevel;
}
/* make new node */
if ((x = malloc(sizeof(Node) +
newLevel*sizeof(Node *))) == 0) {
printf ("insufficient memory (insertNode)\n");
exit(1);
}
x->data = data;
/* update forward links */
for (i = 0; i <= newLevel; i++) {
x->forward[i] = update[i]->forward[i];
update[i]->forward[i] = x;
}
return(x);

46

}
void deleteNode(T data) {
int i;
Node *update[MAXLEVEL+1], *x;
/*******************************************
* delete node containing data from list *
*******************************************/
/* find where data belongs */
x = list.hdr;
for (i = list.listLevel; i >= 0; i--) {
while (x->forward[i] != NIL
&& compLT(x->forward[i]->data, data))
x = x->forward[i];
update[i] = x;
}
x = x->forward[0];
if (x == NIL || !compEQ(x->data, data)) return;
/* adjust forward pointers */
for (i = 0; i <= list.listLevel; i++) {
if (update[i]->forward[i] != x) break;
update[i]->forward[i] = x->forward[i];
}
free (x);
/* adjust header level */
while ((list.listLevel > 0)
&& (list.hdr->forward[list.listLevel] == NIL))
list.listLevel--;
}
Node *findNode(T data) {
int i;
Node *x = list.hdr;
/*******************************
* find node containing data *
*******************************/
for (i = list.listLevel; i >= 0; i--) {
while (x->forward[i] != NIL
&& compLT(x->forward[i]->data, data))
x = x->forward[i];
}
x = x->forward[0];
if (x != NIL && compEQ(x->data, data)) return (x);
return(0);
}
void initList() {
int i;

47

/**************************
* initialize skip list *
**************************/
if ((list.hdr = malloc(sizeof(Node) + MAXLEVEL*sizeof(Node *))) == 0) {
printf ("insufficient memory (initList)\n");
exit(1);
}
for (i = 0; i <= MAXLEVEL; i++)
list.hdr->forward[i] = NIL;
list.listLevel = 0;
}

48

5. Ebl_jZlmjZ
[1]

Donald E. Knuth. The Art of Computer Programming, volume 3. Massachusetts:


Addison-Wesley, 1973.?klvjmkkdbci_j_\h^>DgmlBkdmkkl\hijh]jZffbjh\Zgby^eyW<FL
Ba^\hFbjF

[2]

Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest. Introduction to Algorithms. New York: McGraw-Hill, 1992.

[3]

Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. Data Structures and Algorithms.
Massachusetts: Addison-Wesley, 1983.
[4]

Peter K. Pearson. Fast hashing of variable-length text strings. Communications of the


ACM, 33(6):677-680, June 1990.

[5]

William Pugh. Skip lists: A probabilistic alternative to balanced trees. Communications


of the ACM, 33(6):668-676, June 1990.

49

6. Keh\Zjv
Term
sort by insertion
array
linked list
comparison
in-place
stable sort
underflow
overflow
red-black tree
skip list
rotation

L_jfbg

khjlbjh\dZ\klZ\dZfb
fZkkb\
ebg_cguckibkhd
kjZ\g_gb_
gZ
f_kl_
[_a
^hihegbl_evguofZkkb\h\
mklhcqb\Zykhjlbjh\dZ
\ujh`^_gb_
i_j_iheg_gb_
djZkghq_jgh_^_j_\h
jZa^_e_gguckibkhd
\jZs_gb_

50