Table Of ContentDataS tructuarnedAs l gorithms
UsinPgy thoann dC ++
DavidM . Reed
CapiUtnailv ersity
JohnZ elle
WartbuCrogl lege
FarnkBleilened,A ssaotc,eI icsonrporated
&
SWS .tH elDeirnvsSe t.,De
8536
Wiolnslv,eOi rlegon
97070
wwwfb.eeldeo.mc
PersiaddneP nutsb hleiJmri L eyi( [email protected])d
PorejcMtan gaer TomS umenr
Edoirt StpehaeW nelich
Pirntietdnh UeS. A..
Nameosfa lplruo cdhteser ianru es feodire d ntifpiucrsapeotosin aolnnyad ert radrsek ma
adn/roerg itsartdeermseoa dftr hkrer iespeowcntesirF.avr en kBleilened,A scsioeast,
&
Icn.m,ak enso colfoa winmei ropsrc h orpaosorscatieaw tiitohpn r tuohcdet s poar com
neist htoa wtnh .e m
FarnlnkiB,e leedA ssocIcinoartpeos.r apNtatoer dofb otomhkayib ser p ero
©2009
&
dcuesdt,o iraner der tile vtsaeytmsr,na simt,to ertad rnsbcer,idi anyn f oromrb ya yn
measn-ecltoernimcec,hi aaclnt,e lteh,ppi ahctoopcyoinrgec,iog nro,dr othweirtwhise-
ouptrr wi roitpteiersnms oifto hne l ipsuhRbeeurqse.tf sop re irsomsnsi hlobdue a drdessed
asfl oowls:
Rgihsat dnP eirssmions
Farkniln,l eBAe secsdioeasIt,con rporated
&
SWS .tH eelnDsir vSeu,iD t e
8536
WilosnlevlO,ir egon
97070
ISBN
978--159028-233-5
Liabroryf CroeCnsasglt goani-gini-cPaudtbailmtoaaynb e tioaenbdf rotmhp eu ibslher.
Contents
Praecfe. . . . . . .. . . . . . .
Xl
Chapte1rA bstractainodnA nalysis 1
1.I1n trod.uc.t.io.n. . . . . 1
1.1P.r1o gramimnti ehnL ga r.g e 2
11.2. ThReo aAdh ea.d . 3
12. FunctAibosntarla .c t.i on. . 5
1..21D esibgyCn o ntr.a c.t 5
12..2Tes tiPnrge conditions 9
12.3. Top-DoDwens ign 13
"
1..24D ocumenStiidneg Effects 16
13. AlgorAintahlmy sis . . 17
13.. 1L inear Search . . . . . . 1.7
13..2Bi naSreryac h. . .. . . 21
13..3In forAmlaglo rCiotmhpma rison 22
13.. 4Fo rmAanla ly.si.s. . .. . . . 24
13..5Bi 0g N otatvisTo.hn e Ntoat ation 30
14. ChapStuemrm ary 32
15. E xerc.is.es.. . . 33
Chapte2rD ataA bstraction 39
2.O1v erv.ie.w . . . ... . . . . . 39
2.2Ab strDaatcaTt y pe.s. . .. . . 40
2.12 F.roDma ta tToyA pDeT 40
2.2D.e2fi nainnA gD T. . . . 41
2.2I.m3p lemeannt iAnDgT . 43
2.A3D Tasn Odb je.ct.s . .. . . 46
iii
iv Contents
2.13 S.pecific.a.ti.on. .. . . . 46
2.3I.m2p lemen.t a.t i.o n. . . . . 48
2.33 C.hangtihnRege presentation 50
2.3O.bj4ce t-iOernDteesdi agnnPd r ogram.m ing 51
2.A4n E xampAlDeTD :a taset 55
2.14 T.heP rocoefOs OsD . . 55
2.4I.d2e ynitnaignfA DT . 56
,
2.4I.m3p lemetnhAteDi Tng 58
2.A5n E xampAlDeTR :a tio.n al 60
2.5O.pe1ar toOrv erloading 60
2.5T.h2Re a tioCnlaa.ls s . . 61
2.6In cremDeenvteallo apnmUden nitTt e sting 63
2.7Ch apStuemrm ary 67
2.E8x ercises 68
Chapte3rC ontainer Classes 75
3.O1v ervi.e.w . . . . . ... .. . . . . . . 75
3.2Py thLoins .ts. .... . . . .. . . . . 76
3.3A SequeCnotlilaelAc tDieoconkfC: a rds 77
3.4A SortCeodl leHcatni.do n.:. 80
3.14 C.reatai nBgr Hiadnged 81
3.4C.o2m parCianr.gd s. . 83
3.4S.o3r ting. .C ar.d s. 85
.
3.5Py thLoinIs mtp lementation 87
35..A1r ray-Lbiass.te.sd . 87
3.5E.ffi2c ieAnncayl y.s i.s 89
3.6Py thDoinic otna(rOitpeiso nal) 89
3.16 .DAi ctiAoDnTa. r y. . 90
3.6P.y2t hDoinc tio.n ar.i es 90
3.6D.i3c tiIomnpalreym entation 93
3.6A.n4E xtenEdxeadm pAl Mea:r kCohva in 95
3.7Ch apStuemrm ary 99
3.8Ex ercises 100
Chapte4rL inked StrucatnudrI etse rators 107
4.1Ov erv.ie.w.. . . .. . . 107
4.2Th eP ythMoenm oMroyd e.l . . . 180
4.2P.a1s sPianrga me.t e.r s. . . 141
4.3A LinkIemdp lemenotfLa its.it osn 171
Contents
v
4.4Lni keIdm plemenotfaaL tiiAsotDn T 122
4.5It era.t.or.s . . . ... . . . . . . 153
4.5I.t1e raitnPo yrtsh. o n. . . . . . 163
4.5A.d2d iannIg t ertaoLt Loir.s t . 137
4.35 I.terawtiitnhPg y ta hGoenn erator 183
4.6A CursorL-ibAsaPtse I(d Op toinal) 104
4.16 A. Curso.r. A.PI. . 141
4.6A. P2y thCourns orList 142
4.36 .LAi nkCeudrr sLosit 144
4.7Li nvk.ss Arrays . 174
4.8Ch apStuemrm ary 148
4.9Ex ercises 184
5 155
ChapterS tackasn dQ ueues
5.O1v erv.i ew. . . . . 155
5.2St caks. . . .. . . . 155
5.2T.h1eS tcakA DT 165
5.2S.i2m pSlteaA cpkp lications 175
5.32 I.mplemeSntctaki.sn g. . . 195
5.2A.n4A pplicEaxtpiroensM:sa inoinp ulation 106
5.2A.n5A pplicGartaimomnPa:rr o ce(sOstipinogn al) 136
5.Q3u eu.e.s . . . . . .. . . . . . . . 196
5.3A. 1Q ueue .A.DT. .. . . 169
5.3S.i2m pQluee uAep plications 107
5.4Qu eIume plement.a t.i o.n s . . . 127
5.A5n E xampAlpep licQauteiuoenSi:in mgu la(tOpitoinosn al) 147
5.6Ch apStuemrm ary 108
5.E7x erc.is.es 181
6 189
ChapterR ecursion
61. Introd.u ction 198
6.2Re curDseifivnei t.i.on.s. 191
6.3Si mpRleec urEsxiavmep les 139
6.3E.x1a ml:peS tring Reversal 193
6.3E.x2a ml:peA nagra.m.s . 195
6.3E.x3a mpFlaesE:tx ponentiation 197
6.3E.x4a mlpeB:i rnyaS ear.c h 198
6.4An alyRzeicnugr sion 199
6.5So rt.i.n.g.. . . . . . . . . . 202
.
vi Contents
65..R1e curDseisvieg n: M.er.g esort 202
6.5A.n2a lyMzeirngge .s or.t . . . . . 205
6.6A "HardP"r oblTehmeT: o weorfH anoi 207
6.7Ch apStuemrm ary 212
6.8Ex ercises 212
Chapte7rTr ees 223
71. Overvi.e.w . . . .......... .... 223
7.2Tre eT ermino.l og.y . . . . . . . . 2.2 4 . . . .
7.3A nE xampAlpep licEaxtpiroensT:rse ieosn 226
7.4Tr eRee presen.t a.t i.o. n .s. . . . 228
.
7.5An A pplicAa BtiirnoyaSn e:a rTcrehe. . . 230
75..T1h eB irnyaS eracPhr ope.r t.y . . . 230
7.5I.m2p lemeAn BtiinnaSgre ya rTcrhe e 230
7.5Tr.a3v erasB iSnTg. . . . . .. . . . . 238
7.5A. R4u n-time oAfBn SaTAl lygsoirsi. t hms 241
7.6Im plemeanM taipnpgiw nigtB hS T t(iOopnal) 242
7.7Ch apStuemrm ary 245
7.8Ex ercises 245
Chapte8rC ++ IntroductfioorPn y thoPnr ogrammers 255
81. Inturcotdi. o n. . . . . . . . . . 2.5 5 . . . . . . . .
8.2C+ +H istaonrBdya c kgrou.n.d . . .. . . . 265
8.3Co mmtesnB,l kosco fC od,Ie dentainfiKdee rysw,o rds 262
8.4Da tTay pesV aairnadbD leec lara.t.io.n.s . . . 263
.
8.5In clSutdaet emNeanmtess,p aancIden stp,Ou/ utupt 267
8.6Co mpi.li.ng. .. .. .. . .. .. . . 271
8.7Ex presasniOdop nesr aPtroerc edence 247
8.8De ciSstitaoenm ents 277
8.9Ty pCeo nver.s i.o ns 281
81.0L oopSitntage ments 282
81.1Ar ra.y s. . . . . . . 285
8.1S1i.n1g le-DAirmyresan. sion 285
81.12. M Duilmtein-sAirornaayls 287
81..13Ar raoyfCs h raact.e r.s . 287
8.F1u2n ctDieotna .il.s. . . . . . . 288
81.21.D eclarDaetfiinointsai,no Pdnr so,t otypes 289
81.2P.a2sb syV alu.e . . 292
81.2.Pa3s bsyR efere.n.ce. . . ... . . . . .2 93
Contents vii
81.2P.a4s singa sPA arrraamyes.t ers 294
81.2.cno5s Pta rame.te.rs. . . 296
81.2D.e6af ult Par.a m.e t.e rs. . 297
8.H1e3a dFeirla ensId n lFiunnec t.i ons 298
8.41 AsSsteartte maennTdte sst ing . . 303
8.T1h5Se coapneLd i feotfiV amirea bles 305
8.C1o6m moCn+ +M iasktebsy P ythPorno gram.m ers 306
81.7A dditCi+o+nTa olp i(Ocpst i)o .n al 307
8..1T17h eC ++S witSctthae me.n t 307
81.7.Cr2e atCi+n+Ng a mespaces 310
8..13G7l obVaailra bles 310
8.1Ch8a pStuemrm ary 312
8.E1x9e rc.is.es. 313
Chapte9rC ++ Classes 319
91. BasSiycn taanxSd e mantics 319
9.2St ri.ng.s. . .. . . . 330
9.3Fi Ilnep auntOd u tp.ut. . 333
9.4Op eraOtvoerr loa.d i.n g. 335
9.5Cl aVsarsi abalneMdse thods 343
9.C6h apStuemrm ary 347
9.E7x erc.is.es. . .. . . . . 348
Chapte1r 0C ++ DynamicM emory 353
101I. ntrod.u ct.i o.n 353
10C.+2+ P oin.te.rs. .. . . 360
10.Dy3n amic .Ar.ra..y .s 366
10.Dy4n amic CMelmaosrsye s 371
1.041D .estr.u ct.o r. . . 371
10.C4o.p2Cy o nstructor 373
1.04A.s3s ignOmpeenrta tor 378
1.04A. C4o mplDeytnea mAircyr C alass 381
10.R4e.f5e rReentcuTery np es 386
10D.y5n amMiecm oErryr o.r s. . . . 388
10.1 5.Memory. L.ea..k. s . 388
10.A5c.c2e sIsnivnaMgle imdo ry 390
10.M5e.m3o Erryr Sourm ma.r y 393
10.Ch6a pStuemrm ary 395
10E.x7e rc.is.es. . . 395
viii Contents
Chapte1r 1C+ + LinkeSdt ructures 403
11I.n1t rod.uc.t.io.n. . . . . 403
1.12A C ++L inkSetdr ucCtluarses 404
1.13A C++L inkLeids. t . ... . 407
114C .++L inkDeydn amic Memory Errors 421
1.15Ch apStuemrm ary 422
116E .xercises 422
Chapte1r 2C ++ Templates 427
121I. n trod.u ct.i o.n . . 427
12T.e2m plFautnec tions 429
12T.em3p laCtlea s.se.s 431
12.1T 3h.eS tandTaermdp lLaitberv aercyt Colra. s s 431
12.U3s.e2r -dTeefimnpeldCa ltaes ses 435
12C.h4a pStuemrm ary 440
12E.x5e rcises 440
Chapte1r 3He apsB,a lanceTrde es,a ndH ashT ables 443
131.I ntrod.uc.t.io.n. . . . . 443
13P.r2i oQruietuyae nsHd e ap.s . . . .. . . . . 444
1.321H .eaps.or.t . . . . . .. . . . . . . .45 1
13.N2o.t2oe nsH eaapn Pdr ioQruietuIyem plementatio4n5s2
13B.a3l anTcreeeds. . . . 453
13.Ot4eh rT reeS tructures 465
13H.a5s Tha bl.e.s . . 465
13.Ch6a pStuemrm ary 478
13E.x7e rcises 478
Chapte1r 4G raphs 485
141I. ntrod.uc.t.io.n. . . . 485
14G.r2a pDhat aS truc.tu.r es 487
14.Sh3o rtesAtl gPoartiht hms 491
14..1T3 hUen weigShhtoerdPt aetsht 492
14.T3h.We2e i ghtSehdo rPtaetsht 496
14.De4p Ftihr Asltg ori.th.m s 501
14M.i5n imSupma nnTirenegs . . . . . 507
.
145.1.K rus'kAsal lgori.t.hm. . . 507
14.T5h.De2i sjSoeiDtnat taS tructure 509
14.P5ri.m3'A slg or.it.hm. . .. . . . 511
Contents ix
14C.h6a pStuemrm ary 512
14E.x7e rc.is.es. . . 513
Chapte1r 5Al goritThemc hniques 517
151I. ntrod.uc.t.io.n. . . . 517
15D.i2v iadneCd o nqu.e r. . . . . . . . 5.1 8
151.A 2n.alyRzeicnugr Fsuinvcet ions 518
15.Q2u.i2c k.so.r.t. 521
15.Gr3e eAdlyg ori.t.hm.s . . .. . . . . 527
15D.y4n amPirco gram.mi.ng. .. . . . 536
15.L4o.n1g Ceosmtm oSnu bsequence 537
154..M2e moiza.t i.o n . . . . . . . 541
1.54M.a3t rCihxa in Multiplication 541
15N.P5- CompPlreotbel ems 543
15C.h6a pStuemrm ary 545
15E.x7e rc.i ses 546
549
AppendiGxl ossary
563
Index
Description:THIS BOOK is intended for use in a traditional college-level data structures course (commonly known as CS2). This book assumes that students have learned the basic syntax of Python and been exposed to the use of existing classes. Most traditional CS1 courses that use Python will have covered all the