Table Of ContentData Structures
and
Program Design
++
in C
NAVIGATING THE DISK
ForinformationonusingtheAcrobattoolbarandotherAcrobatcommands,consult
theHelpdocumentwithinAcrobat. Seeespeciallythesection“NavigatingPages.”
Material displayed in green enables jumps to other locations in the book, to
transparencymasters,andtorunsampledemonstrationprograms. Thesecomein
threevarieties:
(cid:229)
The green menu boxes in the left margin of each page perform jumps to fre-
quentlyusedpartsofthebook:
(cid:229)
Greenmaterialinthetextitselfwilljumptotheplaceindicated. Aftertaking
suchajump,youmayreturnbyselectingthe//icon(goback)intheAcrobat
toolbar.
(cid:229)
The transparency-projector icon ( ) brings up a transparency master on the
currenttopic. Returnbyselectingthe//icon(goback)intheAcrobattoolbar.
(cid:229)
TheWindows( )iconintheleftmarginselectandrunademonstrationpro-
gram,whichwilloperateonlyontheWindowsplatform.
ThisCDcontainsafoldertextprogthatcontainsthesourcecodeforallprograms
and program segments appearing in the book. These files cannot be compiled
directly,buttheycanbecopiedandusedforwritingotherprograms.
HINTS FOR PAGE NAVIGATION
(cid:229) Each chapter (or other major section) of the book is in a separate pdf file, so
youmaystartAcrobatdirectlyonadesiredchapter.
(cid:229)
Tofindaparticularsectioninthecurrentchapter,hittheHomekey,orselect
j/ intheAcrobattoolbaror inthegreenmenubar,whichwilljumptothe
firstpageofthechapterwherethereisatableofcontentsforthechapter.
(cid:229)
After jumping to a new location in the book, you can easily return to your
previouslocationbyselecting // (goback)intheAcrobattoolbar.
(cid:229)
Tofindaparticulartopic,selecttheindexicon( )intheleftmargin.
(cid:229)
Tofindaparticularwordinthecurrentchapter,usethebinocularsiconinthe
Acrobattoolbar.
(cid:229) ThePgDownandEnter(orReturn)keysadvanceonescreenful,whereas.,#,
!,and advanceonepage. Ofthese,only willmovefromthelastpageof
onechaptertothefirstpageofthenextchapter.
(cid:229)
To move backwards, PgUp and Shift+Enter move up one screenful, whereas
/, ", ,and movebackonepage. Ofthese,only willmovefromthefirst
pageofonechaptertothelastpageofthepreviouschapter.
Data Structures
and
Program Design
++
in C
Robert L. Kruse
Alexander J. Ryba
CD-ROM prepared by
Paul A. Mailhot
Prentice Hall
UpperSaddleRiver,NewJersey07458
Library of Congress Cataloging–in–Publication Data
KRUSE, ROBERT L.
Data structures and program design in C++ / Robert L. Kruse,
Alexander J. Ryba.
p. cm.
Includes bibliographical references and index.
ISBN 0–13–087697–6
1. C++ (Computer program language) 2. Data Structures
(Computer Science) I. Ryba, Alexander J. II. Title.
QA76.73.C153K79 1998 98–35979
005.13’3—dc21 CIP
Publisher: Alan Apt Cover Designer: Heather Scott
Editor in Chief: Marcia Horton Manufacturing Buyer: Pat Brown
Acquisitions Editor: Laura Steele Assistant Vice President of Production and
Production Editor: Rose Kernan Manufacturing: David W. Riccardi
Managing Editor: Eileen Clark Editorial Assistant: Kate Kaibni
Art Director: Heather Scott Interior Design: Robert L. Kruse
Assistant to Art Director: John Christiana Page Layout: Ginnie Masterson (PreTEX, Inc.)
Copy Editor: Patricia Daly Art Production: Blake MacLean (PreTEX, Inc.)
Cover art: Orange, 1923, by Wassily Kandinsky (1866-1944), Lithograph in Colors. Source: Christie’s Images
© 2000 by Prentice-Hall, Inc.
Simon & Schuster/A Viacom Company
Upper Saddle River, New Jersey 07458
The typesetting for this book was done with PreTEX, a preprocessor and macro package for the TEX typesetting system
and the POSTSCRIPT page-description language. PreTEX is a trademark of PreTEX, Inc.; TEX is a trademark of the American
Mathematical Society; POSTSCRIPT is a registered trademarks of Adobe Systems, Inc.
The authors and publisher of this book have used their best efforts in preparing this book. These efforts include the re-
search, development, and testing of the theory and programs in the book to determine their effectiveness. The authors
and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documenta-
tion contained in this book. The authors and publisher shall not be liable in any event for incidental or consequential
damages in connection with, or arising out of, the furnishing, performance, or use of these programs.
All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writ-
ing from the publisher.
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
ISBN 0-13-087697-6
Prentice-Hall International (U.K.) Limited, London
Prentice-Hall of Australia Pty. Limited, Sydney
Prentice-Hall Canada Inc., Toronto
Prentice-Hall Hispanoamericana, S.A., Mexico
Prentice-Hall of India Private Limited, New Delhi
Prentice-Hall of Japan, Inc., Tokyo
Simon & Schuster Asia Pte. Ltd., Singapore
Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro
Contents
Preface ix
1.4.7 ProgramTracing 28
Synopsis xii 1.4.8 PrinciplesofProgramTesting 29
CourseStructure xiv
1.5 ProgramMaintenance 34
SupplementaryMaterials xv
1.5.1 ProgramEvaluation 34
BookProduction xvi
1.5.2 ReviewoftheLifeProgram 35
Acknowledgments xvi
1.5.3 ProgramRevision
andRedevelopment 38
1 Programming
1.6 ConclusionsandPreview 39
Principles 1
1.6.1 SoftwareEngineering 39
1.6.2 ProblemAnalysis 40
1.1 Introduction 2
1.6.3 RequirementsSpecification 41
1.2 TheGameofLife 4 1.6.4 Coding 41
1.2.1 RulesfortheGameofLife 4 PointersandPitfalls 45
1.2.2 Examples 5
ReviewQuestions 46
1.2.3 TheSolution: Classes,Objects,
andMethods 7 ReferencesforFurtherStudy 47
1.2.4 Life: TheMainProgram 8 C++ 47
ProgrammingPrinciples 47
1.3 ProgrammingStyle 10
TheGameofLife 47
1.3.1 Names 10 SoftwareEngineering 48
1.3.2 DocumentationandFormat 13
1.3.3 RefinementandModularity 15 2 Introduction
1.4 Coding,Testing, to Stacks 49
andFurtherRefinement 20
1.4.1 Stubs 20 2.1 StackSpecifications 50
1.4.2 DefinitionoftheClassLife 22 2.1.1 ListsandArrays 50
1.4.3 CountingNeighbors 23 2.1.2 Stacks 50
1.4.4 UpdatingtheGrid 24 2.1.3 FirstExample: ReversingaList 51
1.4.5 InputandOutput 25 2.1.4 InformationHiding 54
1.4.6 Drivers 27 2.1.5 TheStandardTemplateLibrary 55
v
vi Contents
4 Linked Stacks
2.2 ImplementationofStacks 57
2.2.1 SpecificationofMethods and Queues 112
forStacks 57
2.2.2 TheClassSpecification 60 4.1 PointersandLinkedStructures 113
2.2.3 Pushing,Popping, 4.1.1 IntroductionandSurvey 113
andOtherMethods 61 4.1.2 PointersandDynamicMemory
inC++ 116
2.2.4 Encapsulation 63
4.1.3 TheBasicsofLinkedStructures 122
2.3 Application: ADeskCalculator 66
4.2 LinkedStacks 127
2.4 Application: BracketMatching 69 4.3 LinkedStackswithSafeguards 131
4.3.1 TheDestructor 131
2.5 AbstractDataTypes
4.3.2 Overloadingthe
andTheirImplementations 71
AssignmentOperator 132
2.5.1 Introduction 71 4.3.3 TheCopyConstructor 135
2.5.2 GeneralDefinitions 73 4.3.4 TheModified
2.5.3 RefinementofDataSpecification 74 Linked-StackSpecification 136
PointersandPitfalls 76 4.4 LinkedQueues 137
4.4.1 BasicDeclarations 137
ReviewQuestions 76 4.4.2 ExtendedLinkedQueues 139
ReferencesforFurtherStudy 77 4.5 Application: PolynomialArithmetic 141
4.5.1 PurposeoftheProject 141
4.5.2 TheMainProgram 141
3 4.5.3 ThePolynomialDataStructure 144
Queues 78 4.5.4 ReadingandWriting
Polynomials 147
4.5.5 AdditionofPolynomials 148
3.1 Definitions 79
4.5.6 CompletingtheProject 150
3.1.1 QueueOperations 79
3.1.2 ExtendedQueueOperations 81 4.6 AbstractDataTypes
andTheirImplementations 152
3.2 ImplementationsofQueues 84
PointersandPitfalls 154
3.3 CircularImplementation
ReviewQuestions 155
ofQueuesinC++ 89
5
3.4 DemonstrationandTesting 93
Recursion 157
3.5 ApplicationofQueues: Simulation 96
3.5.1 Introduction 96 5.1 IntroductiontoRecursion 158
3.5.2 SimulationofanAirport 96 5.1.1 StackFramesforSubprograms 158
3.5.3 RandomNumbers 99 5.1.2 TreeofSubprogramCalls 159
3.5.4 TheRunwayClassSpecification 99 5.1.3 Factorials:
ARecursiveDefinition 160
3.5.5 ThePlaneClassSpecification 100
5.1.4 DivideandConquer:
3.5.6 FunctionsandMethods
TheTowersofHanoi 163
oftheSimulation 101
3.5.7 SampleResults 107 5.2 PrinciplesofRecursion 170
5.2.1 DesigningRecursiveAlgorithms 170
PointersandPitfalls 110
5.2.2 HowRecursionWorks 171
5.2.3 TailRecursion 174
ReviewQuestions 110
5.2.4 WhenNottoUseRecursion 176
ReferencesforFurtherStudy 111 5.2.5 GuidelinesandConclusions 180
Contents vii
7
5.3 Backtracking: PostponingtheWork 183
5.3.1 SolvingtheEight-QueensPuzzle 183 Searching 268
5.3.2 Example: FourQueens 184
5.3.3 Backtracking 185 7.1 Searching:
5.3.4 OverallOutline 186 IntroductionandNotation 269
5.3.5 Refinement: TheFirstDataStructure
andItsMethods 188 7.2 SequentialSearch 271
5.3.6 ReviewandRefinement 191
7.3 BinarySearch 278
5.3.7 AnalysisofBacktracking 194
7.3.1 OrderedLists 278
5.4 Tree-StructuredPrograms: 7.3.2 AlgorithmDevelopment 280
Look-AheadinGames 198 7.3.3 TheForgetfulVersion 281
5.4.1 GameTrees 198 7.3.4 RecognizingEquality 284
5.4.2 TheMinimaxMethod 199
7.4 ComparisonTrees 286
5.4.3 AlgorithmDevelopment 201
7.4.1 Analysisfor n(cid:131)10 287
5.4.4 Refinement 203
7.4.2 Generalization 290
5.4.5 Tic-Tac-Toe 204
7.4.3 ComparisonofMethods 294
PointersandPitfalls 209 7.4.4 AGeneralRelationship 296
ReviewQuestions 210
7.5 LowerBounds 297
ReferencesforFurtherStudy 211
7.6 Asymptotics 302
7.6.1 Introduction 302
6 Lists and
7.6.2 OrdersofMagnitude 304
Strings 212 7.6.3 TheBig-O
andRelatedNotations 310
6.1 ListDefinition 213 7.6.4 KeepingtheDominantTerm 311
6.1.1 MethodSpecifications 214
PointersandPitfalls 314
6.2 ImplementationofLists 217
ReviewQuestions 315
6.2.1 ClassTemplates 218
6.2.2 ContiguousImplementation 219 ReferencesforFurtherStudy 316
6.2.3 SimplyLinkedImplementation 221
6.2.4 Variation: KeepingtheCurrent
8
Position 225
6.2.5 DoublyLinkedLists 227 Sorting 317
6.2.6 ComparisonofImplementations 230
8.1 IntroductionandNotation 318
6.3 Strings 233
8.1.1 SortableLists 319
6.3.1 StringsinC++ 233
6.3.2 ImplementationofStrings 234 8.2 InsertionSort 320
6.3.3 FurtherStringOperations 238 8.2.1 OrderedInsertion 320
8.2.2 SortingbyInsertion 321
6.4 Application: ATextEditor 242
8.2.3 LinkedVersion 323
6.4.1 Specifications 242
6.4.2 Implementation 243 8.2.4 Analysis 325
6.5 LinkedListsinArrays 251 8.3 SelectionSort 329
8.3.1 TheAlgorithm 329
6.6 Application:
8.3.2 ContiguousImplementation 330
GeneratingPermutations 260
8.3.3 Analysis 331
PointersandPitfalls 265 8.3.4 Comparisons 332
ReviewQuestions 266 8.4 ShellSort 333
ReferencesforFurtherStudy 267 8.5 LowerBounds 336
viii Contents
8.6 Divide-and-ConquerSorting 339 9.8 Conclusions:
8.6.1 TheMainIdeas 339 ComparisonofMethods 417
8.6.2 AnExample 340
9.9 Application:
8.7 MergesortforLinkedLists 344 TheLifeGameRevisited 418
8.7.1 TheFunctions 345 9.9.1 ChoiceofAlgorithm 418
8.7.2 AnalysisofMergesort 348 9.9.2 SpecificationofDataStructures 419
9.9.3 TheLifeClass 421
8.8 QuicksortforContiguousLists 352
9.9.4 TheLifeFunctions 421
8.8.1 TheMainFunction 352
8.8.2 PartitioningtheList 353 PointersandPitfalls 426
8.8.3 AnalysisofQuicksort 356
ReviewQuestions 427
8.8.4 Average-CaseAnalysisof
Quicksort 358 ReferencesforFurtherStudy 428
8.8.5 ComparisonwithMergesort 360
10
8.9 HeapsandHeapsort 363
8.9.1 Two-WayTreesasLists 363 Binary Trees 429
8.9.2 DevelopmentofHeapsort 365
8.9.3 AnalysisofHeapsort 368 10.1 BinaryTrees 430
8.9.4 PriorityQueues 369 10.1.1 Definitions 430
10.1.2 TraversalofBinaryTrees 432
8.10 Review: ComparisonofMethods 372
10.1.3 LinkedImplementation
PointersandPitfalls 375 ofBinaryTrees 437
ReviewQuestions 376 10.2 BinarySearchTrees 444
10.2.1 OrderedLists
ReferencesforFurtherStudy 377
andImplementations 446
10.2.2 TreeSearch 447
9 Tables and Information
10.2.3 InsertionintoaBinarySearch
Retrieval 379 Tree 451
10.2.4 Treesort 453
9.1 Introduction: 10.2.5 RemovalfromaBinarySearch
Tree 455
BreakingthelgnBarrier 380
10.3 BuildingaBinarySearchTree 463
9.2 RectangularTables 381
10.3.1 GettingStarted 464
9.3 TablesofVariousShapes 383 10.3.2 Declarations
9.3.1 TriangularTables 383 andtheMainFunction 465
9.3.2 JaggedTables 385 10.3.3 InsertingaNode 466
9.3.3 InvertedTables 386 10.3.4 FinishingtheTask 467
10.3.5 Evaluation 469
9.4 Tables: ANewAbstractDataType 388
10.3.6 RandomSearchTrees
9.5 Application: RadixSort 391 andOptimality 470
9.5.1 TheIdea 392
10.4 HeightBalance: AVLTrees 473
9.5.2 Implementation 393
10.4.1 Definition 473
9.5.3 Analysis 396
10.4.2 InsertionofaNode 477
9.6 Hashing 397 10.4.3 RemovalofaNode 484
9.6.1 SparseTables 397 10.4.4 TheHeightofanAVLTree 485
9.6.2 ChoosingaHashFunction 399
10.5 SplayTrees:
9.6.3 CollisionResolutionwithOpen
ASelf-AdjustingDataStructure 490
Addressing 401
10.5.1 Introduction 490
9.6.4 CollisionResolutionbyChaining 406
10.5.2 SplayingSteps 491
9.7 AnalysisofHashing 411 10.5.3 AlgorithmDevelopment 495
Contents ix
12
10.5.4 AmortizedAlgorithmAnalysis:
Introduction 505 Graphs 569
10.5.5 AmortizedAnalysis
ofSplaying 509 12.1 MathematicalBackground 570
12.1.1 DefinitionsandExamples 570
PointersandPitfalls 515
12.1.2 UndirectedGraphs 571
ReviewQuestions 516 12.1.3 DirectedGraphs 571
ReferencesforFurtherStudy 518 12.2 ComputerRepresentation 572
12.2.1 TheSetRepresentation 572
11 Multiway 12.2.2 AdjacencyLists 574
12.2.3 InformationFields 575
Trees 520
12.3 GraphTraversal 575
11.1 Orchards,Trees,andBinaryTrees 521 12.3.1 Methods 575
11.1.1 OntheClassificationof 12.3.2 Depth-FirstAlgorithm 577
Species 521 12.3.3 Breadth-FirstAlgorithm 578
11.1.2 OrderedTrees 522
12.4 TopologicalSorting 579
11.1.3 ForestsandOrchards 524
12.4.1 TheProblem 579
11.1.4 TheFormalCorrespondence 526
12.4.2 Depth-FirstAlgorithm 580
11.1.5 Rotations 527
12.4.3 Breadth-FirstAlgorithm 581
11.1.6 Summary 527
12.5 AGreedyAlgorithm:
11.2 LexicographicSearchTrees: Tries 530
ShortestPaths 583
11.2.1 Tries 530
12.5.1 TheProblem 583
11.2.2 SearchingforaKey 530
12.5.2 Method 584
11.2.3 C++Algorithm 531
12.5.3 Example 585
11.2.4 SearchingaTrie 532
12.5.4 Implementation 586
11.2.5 InsertionintoaTrie 533
11.2.6 DeletionfromaTrie 533 12.6 MinimalSpanningTrees 587
11.2.7 AssessmentofTries 534 12.6.1 TheProblem 587
12.6.2 Method 589
11.3 ExternalSearching: B-Trees 535
12.6.3 Implementation 590
11.3.1 AccessTime 535
12.6.4 Verification
11.3.2 MultiwaySearchTrees 535
ofPrim’sAlgorithm 593
11.3.3 BalancedMultiwayTrees 536
11.3.4 InsertionintoaB-Tree 537 12.7 GraphsasDataStructures 594
11.3.5 C++Algorithms:
PointersandPitfalls 596
SearchingandInsertion 539
11.3.6 DeletionfromaB-Tree 547 ReviewQuestions 597
11.4 Red-BlackTrees 556 ReferencesforFurtherStudy 597
11.4.1 Introduction 556
11.4.2 DefinitionandAnalysis 557 Case Study:
11.4.3 Red-BlackTreeSpecification 559
13 The Polish
11.4.4 Insertion 560
11.4.5 InsertionMethod Notation 598
Implementation 561
11.4.6 RemovalofaNode 565 13.1 TheProblem 599
13.1.1 TheQuadraticFormula 599
PointersandPitfalls 566
13.2 TheIdea 601
ReviewQuestions 567
13.2.1 ExpressionTrees 601
ReferencesforFurtherStudy 568 13.2.2 PolishNotation 603
x Contents
13.3 EvaluationofPolishExpressions 604 A.5 CatalanNumbers 661
13.3.1 EvaluationofanExpression A.5.1 TheMainResult 661
inPrefixForm 605 A.5.2 TheProofbyOne-to-One
13.3.2 C++Conventions 606 Correspondences 662
13.3.3 C++Function A.5.3 History 664
forPrefixEvaluation 607 A.5.4 NumericalResults 665
13.3.4 Evaluation ReferencesforFurtherStudy 665
ofPostfixExpressions 608
13.3.5 ProofoftheProgram: B Random
CountingStackEntries 609
Numbers 667
13.3.6 RecursiveEvaluation
ofPostfixExpressions 612
B.1 Introduction 667
13.4 TranslationfromInfixForm
B.2 Strategy 668
toPolishForm 617
B.3 ProgramDevelopment 669
13.5 AnInteractive
ReferencesforFurtherStudy 673
ExpressionEvaluator 623
13.5.1 OverallStructure 623
C Packages and
13.5.2 RepresentationoftheData:
ClassSpecifications 625 Utility Functions 674
13.5.3 Tokens 629
13.5.4 TheLexicon 631 C.1 PackagesandC++TranslationUnits 674
13.5.5 Expressions: TokenLists 634 C.2 PackagesintheText 676
13.5.6 Auxiliary
EvaluationFunctions 639 C.3 TheUtilityPackage 678
13.5.7 GraphingtheExpression: C.4 TimingMethods 679
TheClassPlot 640
13.5.8 AGraphics-Enhanced D Programming Precepts,
PlotClass 643
Pointers, and Pitfalls 681
ReferencesforFurtherStudy 645
D.1 ChoiceofDataStructures
A Mathematical andAlgorithms 681
D.1.1 Stacks 681
Methods 647
D.1.2 Lists 681
D.1.3 SearchingMethods 682
A.1 SumsofPowersofIntegers 647
D.1.4 SortingMethods 682
A.2 Logarithms 650 D.1.5 Tables 682
A.2.1 DefinitionofLogarithms 651 D.1.6 BinaryTrees 683
A.2.2 SimpleProperties 651 D.1.7 GeneralTrees 684
A.2.3 ChoiceofBase 652 D.1.8 Graphs 684
A.2.4 NaturalLogarithms 652
D.2 Recursion 685
A.2.5 Notation 653
A.2.6 ChangeofBase 654 D.3 DesignofDataStructures 686
A.2.7 LogarithmicGraphs 654
D.4 AlgorithmDesignandAnalysis 687
A.2.8 HarmonicNumbers 656
D.5 Programming 688
A.3 Permutations,Combinations,
D.6 ProgrammingwithPointerObjects 689
Factorials 657
A.3.1 Permutations 657 D.7 DebuggingandTesting 690
A.3.2 Combinations 657
D.8 Maintenance 690
A.3.3 Factorials 658
Index 693
A.4 FibonacciNumbers 659
Description:Data structures and program design in C++ / Robert L. Kruse,. Alexander J. Ryba. p. cm. Includes bibliographical references and index. search, development, and testing of the theory and programs in the book to determine their