Data Structures and Algorithms in C 2nd Edition Michael T. Goodrich all chapter instant download
Data Structures and Algorithms in C 2nd Edition Michael T. Goodrich all chapter instant download
com
https://ebookname.com/product/data-structures-and-
algorithms-in-c-2nd-edition-michael-t-goodrich/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookname.com/product/data-structures-and-algorithms-in-
java-5th-international-student-edition-m-t-goodrich-and-r-tamassia/
ebookname.com
https://ebookname.com/product/data-structures-and-algorithms-with-
javascript-1st-edition-michael-mcmillan/
ebookname.com
https://ebookname.com/product/data-structures-using-c-2nd-edition-a-k-
sharma/
ebookname.com
https://ebookname.com/product/testamentary-trusts-strategies-and-
precedents-2nd-edition-edition-charles-rowland/
ebookname.com
https://ebookname.com/product/culinary-calculations-simplified-math-
for-culinary-professionals-1st-ed-edition-jones/
ebookname.com
https://ebookname.com/product/organization-development-the-process-of-
leading-organizational-change-4th-edition-donald-l-anderson/
ebookname.com
https://ebookname.com/product/encyclopedia-of-suicide-3-volume-set-
oliver-b-torres/
ebookname.com
Culture of vengeance fate american justice 1st Edition
Terry K. Aladjem
https://ebookname.com/product/culture-of-vengeance-fate-american-
justice-1st-edition-terry-k-aladjem/
ebookname.com
This page intentionally left blank
i i
i i
i i
This page intentionally left blank
i i
Michael T. Goodrich
Department of Computer Science
University of California, Irvine
Roberto Tamassia
Department of Computer Science
Brown University
David M. Mount
Department of Computer Science
University of Maryland
i i
i i
i i
This book was set in LATEX by the authors and printed and bound by Malloy Lithographers.
The cover was printed by Malloy Lithographers. The cover image is from Wuta Wuta Tjan-
gala, “Emu dreaming” c estate of the artist 2009 licensed by Aboriginal Artists Agency.
Jennifer Steele/Art Resource, NY.
This book is printed on acid free paper. ∞
Trademark Acknowledgments: Java is a trademark of Sun Microsystems, Inc. UNIX R is
a registered trademark in the United States and other countries, licensed through X/Open
Company, Ltd. PowerPoint R is a trademark of Microsoft Corporation. All other product
names mentioned herein are the trademarks of their respective owners.
Copyright c 2011, John Wiley & Sons, Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system or transmitted
in any form or by any means, electronic, mechanical, photocopying, recording, scanning
or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States
Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc.
222 Rosewood Drive, Danvers, MA 01923, (978)750-8400, fax (978)646-8600.
Requests to the Publisher for permission should be addressed to the Permissions Depart-
ment, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201)748-6011, fax
(201)748-6008, E-Mail: PERMREQ@WILEY.COM.
To order books or for customer service please call 1-800-CALL WILEY (225-5945).
Founded in 1807, John Wiley & Sons, Inc. has been a valued source of knowledge and
understanding for more than 200 years, helping people around the world meet their needs
and fulfill their aspirations. Our company is built on a foundation of principles that include
responsibility to the communities we serve and where we live and work. In 2008, we
launched a Corporate Citizenship Initiative, a global effort to address the environmental,
social, economic, and ethical challenges we face in our business. Among the issues we
are addressing are carbon impact, paper specifications and procurement, ethical conduct
within our business and among our vendors, and community and charitable support. For
more information, please visit our website: www.wiley.com/go/citizenship.
Library of Congress Cataloging in Publication Data
ISBN-13 978-0-470-38327-8
i i
i i
i i
To Isabel
– Roberto Tamassia
To Jeanine
– David M. Mount
i i
i i
This page intentionally left blank
i i
Preface
This second edition of Data Structures and Algorithms in C++ is designed to pro-
vide an introduction to data structures and algorithms, including their design, analy-
sis, and implementation. In terms of curricula based on the IEEE/ACM 2001 Com-
puting Curriculum, this book is appropriate for use in the courses CS102 (I/O/B
versions), CS103 (I/O/B versions), CS111 (A version), and CS112 (A/I/O/F/H ver-
sions). We discuss its use for such courses in more detail later in this preface.
The major changes in the second edition are the following:
• We added more examples of data structure and algorithm analysis.
• We enhanced consistency with the C++ Standard Template Library (STL).
• We incorporated STL data structures into many of our data structures.
• We added a chapter on arrays, linked lists, and iterators (Chapter 3).
• We added a chapter on memory management and B-trees (Chapter 14).
• We enhanced the discussion of algorithmic design techniques, like dynamic
programming and the greedy method.
• We simplified and reorganized the presentation of code fragments.
• We have introduced STL-style iterators into our container classes, and have
presented C++ implementations for these iterators, even for complex struc-
tures such as hash tables and binary search trees.
• We have modified our priority-queue interface to use STL-style comparator
objects.
• We expanded and revised exercises, continuing our approach of dividing
them into reinforcement, creativity, and project exercises.
i i
i i
i i
viii Preface
and deallocation (and the associated issues of destructors), virtual functions, stream
input and output, operator overloading, and C++’s safe run-time casting.
Use as a Textbook
The design and analysis of efficient data structures has long been recognized as a
vital subject in computing, because the study of data structures is part of the core
of every collegiate computer science and computer engineering major program we
are familiar with. Typically, the introductory courses are presented as a two- or
three-course sequence. Elementary data structures are often briefly introduced in
the first programming course or in an introduction to computer science course and
this is followed by a more in-depth introduction to data structures in the courses that
follow after this. Furthermore, this course sequence is typically followed at a later
point in the curriculum by a more in-depth study of data structures and algorithms.
We feel that the central role of data structure design and analysis in the curriculum
is fully justified, given the importance of efficient data structures in most software
systems, including the Web, operating systems, databases, compilers, and scientific
simulation systems.
With the emergence of the object-oriented paradigm as the framework of choice
for building robust and reusable software, we have tried to take a consistent object-
oriented viewpoint throughout this text. One of the main ideas behind the object-
oriented approach is that data should be presented as being encapsulated with the
methods that access and modify them. That is, rather than simply viewing data
as a collection of bytes and addresses, we think of data objects as instances of an
abstract data type (ADT), which includes a repertoire of methods for performing
operations on data objects of this type. Likewise, object-oriented solutions are often
organized utilizing common design patterns, which facilitate software reuse and
robustness. Thus, we present each data structure using ADTs and their respective
implementations and we introduce important design patterns as a way to organize
those implementations into classes, methods, and objects.
For most of the ADTs presented in this book, we provide a description of the
public interface in C++. Also, concrete data structures realizing the ADTs are
discussed and we often give concrete C++ classes implementing these interfaces.
We also give C++ implementations of fundamental algorithms, such as sorting and
graph searching. Moreover, in addition to providing techniques for using data struc-
tures to implement ADTs, we also give sample applications of data structures, such
as HTML tag matching and a simple system to maintain a play list for a digital
audio system. Due to space limitations, however, we only show code fragments of
some of the implementations in this book and make additional source code avail-
able on the companion web site.
i i
i i
i i
Preface ix
Online Resources
This book is accompanied by an extensive set of online resources, which can be
found at the following web site:
www.wiley.com/college/goodrich
Included on this Web site is a collection of educational aids that augment the
topics of this book, for both students and instructors. Students are encouraged to
use this site along with the book, to help with exercises and increase understand-
ing of the subject. Instructors are likewise welcome to use the site to help plan,
organize, and present their course materials. Because of their added value, some of
these online resources are password protected.
i i
i i
i i
x Preface
Table 0.1: Material for units in the IEEE/ACM 2001 Computing Curriculum.
i i
i i
i i
Preface xi
1. A C++ Primer
2. Object-Oriented Design
3. Arrays, Linked Lists, and Recursion
4. Analysis Tools
5. Stacks, Queues, and Deques
6. List and Iterator ADTs
7. Trees
8. Heaps and Priority Queues
9. Hash Tables, Maps, and Skip Lists
10. Search Trees
11. Sorting, Sets, and Selection
12. Strings and Dynamic Programming
13. Graph Algorithms
14. Memory Management and B-Trees
A. Useful Mathematical Facts
A more detailed listing of the contents of this book can be found in the table of
contents.
i i
i i
i i
xii Preface
Prerequisites
We have written this book assuming that the reader comes to it with certain knowl-
edge. We assume that the reader is at least vaguely familiar with a high-level pro-
gramming language, such as C, C++, Python, or Java, and that he or she understands
the main constructs from such a high-level language, including:
• Variables and expressions.
• Functions (also known as methods or procedures).
• Decision structures (such as if-statements and switch-statements).
• Iteration structures (for-loops and while-loops).
For readers who are familiar with these concepts, but not with how they are ex-
pressed in C++, we provide a primer on the C++ language in Chapter 1. Still, this
book is primarily a data structures book, not a C++ book; hence, it does not provide
a comprehensive treatment of C++. Nevertheless, we do not assume that the reader
is necessarily familiar with object-oriented design or with linked structures, such
as linked lists, since these topics are covered in the core chapters of this book.
In terms of mathematical background, we assume the reader is somewhat famil-
iar with topics from high-school mathematics. Even so, in Chapter 4, we discuss
the seven most-important functions for algorithm analysis. In fact, sections that use
something other than one of these seven functions are considered optional, and are
indicated with a star (⋆). We give a summary of other useful mathematical facts,
including elementary probability, in Appendix A.
i i
i i
i i
Preface xiii
Roberto Tamassia received his Ph.D. in Electrical and Computer Engineering
from the University of Illinois at Urbana-Champaign in 1988. He is the Plastech
Professor of Computer Science and the Chair of the Department of Computer Sci-
ence at Brown University. He is also the Director of Brown’s Center for Geometric
Computing. His research interests include information security, cryptography, anal-
ysis, design, and implementation of algorithms, graph drawing, and computational
geometry. He is an IEEE Fellow and a recipient of the Technical Achievement
Award from the IEEE Computer Society for pioneering the field of graph drawing.
He is an editor of several journals in geometric and graph algorithms. He previously
served on the editorial board of IEEE Transactions on Computers.
David Mount received his Ph.D. in Computer Science from Purdue University
in 1983. He is currently a professor in the Department of Computer Science at
the University of Maryland with a joint appointment in the University of Mary-
land’s Institute for Advanced Computer Studies. He is an associate editor for ACM
Transactions on Mathematical Software and the International Journal of Compu-
tational Geometry and Applications. He is the recipient of two ACM Recognition
of Service Awards.
In addition to their research accomplishments, the authors also have extensive
experience in the classroom. For example, Dr. Goodrich has taught data structures
and algorithms courses, including Data Structures as a freshman-sophomore level
course and Introduction to Algorithms as an upper-level course. He has earned sev-
eral teaching awards in this capacity. His teaching style is to involve the students in
lively interactive classroom sessions that bring out the intuition and insights behind
data structuring and algorithmic techniques. Dr. Tamassia has taught Data Struc-
tures and Algorithms as an introductory freshman-level course since 1988. One
thing that has set his teaching style apart is his effective use of interactive hyper-
media presentations integrated with the Web. Dr. Mount has taught both the Data
Structures and the Algorithms courses at the University of Maryland since 1985.
He has won a number of teaching awards from Purdue University, the University of
Maryland, and the Hong Kong University of Science and Technology. His lecture
notes and homework exercises for the courses that he has taught are widely used as
supplementary learning material by students and instructors at other universities.
Acknowledgments
There are a number of individuals who have made contributions to this book.
We are grateful to all our research collaborators and teaching assistants, who
provided feedback on early drafts of chapters and have helped us in developing
exercises, software, and algorithm animation systems. There have been a number of
friends and colleagues whose comments have lead to improvements in the text. We
are particularly thankful to Michael Goldwasser for his many valuable suggestions.
i i
i i
i i
xiv Preface
We are also grateful to Karen Goodrich, Art Moorshead, Scott Smith, and Ioannis
Tollis for their insightful comments.
We are also truly indebted to the outside reviewers and readers for their co-
pious comments, emails, and constructive criticism, which were extremely use-
ful in writing this edition. We specifically thank the following reviewers for their
comments and suggestions: Divy Agarwal, University of California, Santa Bar-
bara; Terry Andres, University of Manitoba; Bobby Blumofe, University of Texas,
Austin; Michael Clancy, University of California, Berkeley; Larry Davis, Univer-
sity of Maryland; Scott Drysdale, Dartmouth College; Arup Guha, University of
Central Florida; Chris Ingram, University of Waterloo; Stan Kwasny, Washington
University; Calvin Lin, University of Texas at Austin; John Mark Mercer, McGill
University; Laurent Michel, University of Connecticut; Leonard Myers, California
Polytechnic State University, San Luis Obispo; David Naumann, Stevens Institute
of Technology; Robert Pastel, Michigan Technological University; Bina Rama-
murthy, SUNY Buffalo; Ken Slonneger, University of Iowa; C.V. Ravishankar,
University of Michigan; Val Tannen, University of Pennsylvania; Paul Van Ar-
ragon, Messiah College; and Christopher Wilson, University of Oregon.
We are grateful to our editor, Beth Golub, for her enthusiastic support of this
project. The team at Wiley has been great. Many thanks go to Mike Berlin, Lil-
ian Brady, Regina Brooks, Paul Crockett, Richard DeLorenzo, Jen Devine, Simon
Durkin, Micheline Frederick, Lisa Gee, Katherine Hepburn, Rachael Leblond, An-
dre Legaspi, Madelyn Lesure, Frank Lyman, Hope Miller, Bridget Morrisey, Chris
Ruel, Ken Santor, Lauren Sapira, Dan Sayre, Diana Smith, Bruce Spatz, Dawn
Stanley, Jeri Warner, and Bill Zobrist.
The computing systems and excellent technical support staff in the departments
of computer science at Brown University, University of California, Irvine, and Uni-
versity of Maryland gave us reliable working environments. This manuscript was
prepared primarily with the LATEX typesetting package.
Finally, we would like to warmly thank Isabel Cruz, Karen Goodrich, Jeanine
Mount, Giuseppe Di Battista, Franco Preparata, Ioannis Tollis, and our parents for
providing advice, encouragement, and support at various stages of the preparation
of this book. We also thank them for reminding us that there are things in life
beyond writing books.
Michael T. Goodrich
Roberto Tamassia
David M. Mount
i i
i i
i i
Contents
1 A C++ Primer 1
1.1 Basic C++ Programming Elements . . . . . . . . . . . . . . . 2
1.1.1 A Simple C++ Program . . . . . . . . . . . . . . . . . . 2
1.1.2 Fundamental Types . . . . . . . . . . . . . . . . . . . . 4
1.1.3 Pointers, Arrays, and Structures . . . . . . . . . . . . . 7
1.1.4 Named Constants, Scope, and Namespaces . . . . . . . 13
1.2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.2.1 Changing Types through Casting . . . . . . . . . . . . . 20
1.3 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.4.1 Argument Passing . . . . . . . . . . . . . . . . . . . . . 28
1.4.2 Overloading and Inlining . . . . . . . . . . . . . . . . . 30
1.5 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.5.1 Class Structure . . . . . . . . . . . . . . . . . . . . . . 33
1.5.2 Constructors and Destructors . . . . . . . . . . . . . . . 37
1.5.3 Classes and Memory Allocation . . . . . . . . . . . . . . 40
1.5.4 Class Friends and Class Members . . . . . . . . . . . . . 43
1.5.5 The Standard Template Library . . . . . . . . . . . . . . 45
1.6 C++ Program and File Organization . . . . . . . . . . . . . . 47
1.6.1 An Example Program . . . . . . . . . . . . . . . . . . . 48
1.7 Writing a C++ Program . . . . . . . . . . . . . . . . . . . . . 53
1.7.1 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
1.7.2 Pseudo-Code . . . . . . . . . . . . . . . . . . . . . . . 54
1.7.3 Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
1.7.4 Testing and Debugging . . . . . . . . . . . . . . . . . . 57
1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2 Object-Oriented Design 65
2.1 Goals, Principles, and Patterns . . . . . . . . . . . . . . . . 66
2.1.1 Object-Oriented Design Goals . . . . . . . . . . . . . . 66
2.1.2 Object-Oriented Design Principles . . . . . . . . . . . . 67
2.1.3 Design Patterns . . . . . . . . . . . . . . . . . . . . . . 70
xv
i i
i i
i i
xvi Contents
2.2 Inheritance and Polymorphism . . . . . . . . . . . . . . . . . 71
2.2.1 Inheritance in C++ . . . . . . . . . . . . . . . . . . . . . 71
2.2.2 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . 78
2.2.3 Examples of Inheritance in C++ . . . . . . . . . . . . . . 79
2.2.4 Multiple Inheritance and Class Casting . . . . . . . . . . 84
2.2.5 Interfaces and Abstract Classes . . . . . . . . . . . . . . 87
2.3 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
2.3.1 Function Templates . . . . . . . . . . . . . . . . . . . . 90
2.3.2 Class Templates . . . . . . . . . . . . . . . . . . . . . . 91
2.4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
2.4.1 Exception Objects . . . . . . . . . . . . . . . . . . . . . 93
2.4.2 Throwing and Catching Exceptions . . . . . . . . . . . . 94
2.4.3 Exception Specification . . . . . . . . . . . . . . . . . . 96
2.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
i i
i i
i i
Contents xvii
4.1.3 The Linear Function . . . . . . . . . . . . . . . . . . . . 156
4.1.4 The N-Log-N Function . . . . . . . . . . . . . . . . . . 156
4.1.5 The Quadratic Function . . . . . . . . . . . . . . . . . . 156
4.1.6 The Cubic Function and Other Polynomials . . . . . . . 158
4.1.7 The Exponential Function . . . . . . . . . . . . . . . . . 159
4.1.8 Comparing Growth Rates . . . . . . . . . . . . . . . . . 161
4.2 Analysis of Algorithms . . . . . . . . . . . . . . . . . . . . . 162
4.2.1 Experimental Studies . . . . . . . . . . . . . . . . . . . 163
4.2.2 Primitive Operations . . . . . . . . . . . . . . . . . . . 164
4.2.3 Asymptotic Notation . . . . . . . . . . . . . . . . . . . 166
4.2.4 Asymptotic Analysis . . . . . . . . . . . . . . . . . . . . 170
4.2.5 Using the Big-Oh Notation . . . . . . . . . . . . . . . . 172
4.2.6 A Recursive Algorithm for Computing Powers . . . . . . 176
4.2.7 Some More Examples of Algorithm Analysis . . . . . . . 177
4.3 Simple Justification Techniques . . . . . . . . . . . . . . . . 181
4.3.1 By Example . . . . . . . . . . . . . . . . . . . . . . . . 181
4.3.2 The “Contra” Attack . . . . . . . . . . . . . . . . . . . 181
4.3.3 Induction and Loop Invariants . . . . . . . . . . . . . . 182
4.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
i i
i i
i i
xviii Contents
6 List and Iterator ADTs 227
6.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
6.1.1 The Vector Abstract Data Type . . . . . . . . . . . . . 228
6.1.2 A Simple Array-Based Implementation . . . . . . . . . . 229
6.1.3 An Extendable Array Implementation . . . . . . . . . . . 231
6.1.4 STL Vectors . . . . . . . . . . . . . . . . . . . . . . . . 236
6.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
6.2.1 Node-Based Operations and Iterators . . . . . . . . . . . 238
6.2.2 The List Abstract Data Type . . . . . . . . . . . . . . . 240
6.2.3 Doubly Linked List Implementation . . . . . . . . . . . . 242
6.2.4 STL Lists . . . . . . . . . . . . . . . . . . . . . . . . . 247
6.2.5 STL Containers and Iterators . . . . . . . . . . . . . . . 248
6.3 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
6.3.1 The Sequence Abstract Data Type . . . . . . . . . . . . 255
6.3.2 Implementing a Sequence with a Doubly Linked List . . 255
6.3.3 Implementing a Sequence with an Array . . . . . . . . . 257
6.4 Case Study: Bubble-Sort on a Sequence . . . . . . . . . . . 259
6.4.1 The Bubble-Sort Algorithm . . . . . . . . . . . . . . . . 259
6.4.2 A Sequence-Based Analysis of Bubble-Sort . . . . . . . . 260
6.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
7 Trees 267
7.1 General Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
7.1.1 Tree Definitions and Properties . . . . . . . . . . . . . . 269
7.1.2 Tree Functions . . . . . . . . . . . . . . . . . . . . . . . 272
7.1.3 A C++ Tree Interface . . . . . . . . . . . . . . . . . . . 273
7.1.4 A Linked Structure for General Trees . . . . . . . . . . . 274
7.2 Tree Traversal Algorithms . . . . . . . . . . . . . . . . . . . 275
7.2.1 Depth and Height . . . . . . . . . . . . . . . . . . . . . 275
7.2.2 Preorder Traversal . . . . . . . . . . . . . . . . . . . . . 278
7.2.3 Postorder Traversal . . . . . . . . . . . . . . . . . . . . 281
7.3 Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
7.3.1 The Binary Tree ADT . . . . . . . . . . . . . . . . . . . 285
7.3.2 A C++ Binary Tree Interface . . . . . . . . . . . . . . . 286
7.3.3 Properties of Binary Trees . . . . . . . . . . . . . . . . 287
7.3.4 A Linked Structure for Binary Trees . . . . . . . . . . . 289
7.3.5 A Vector-Based Structure for Binary Trees . . . . . . . . 295
7.3.6 Traversals of a Binary Tree . . . . . . . . . . . . . . . . 297
7.3.7 The Template Function Pattern . . . . . . . . . . . . . 303
7.3.8 Representing General Trees with Binary Trees . . . . . . 309
7.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
i i
i i
i i
Contents xix
8 Heaps and Priority Queues 321
8.1 The Priority Queue Abstract Data Type . . . . . . . . . . . 322
8.1.1 Keys, Priorities, and Total Order Relations . . . . . . . . 322
8.1.2 Comparators . . . . . . . . . . . . . . . . . . . . . . . . 324
8.1.3 The Priority Queue ADT . . . . . . . . . . . . . . . . . 327
8.1.4 A C++ Priority Queue Interface . . . . . . . . . . . . . . 328
8.1.5 Sorting with a Priority Queue . . . . . . . . . . . . . . . 329
8.1.6 The STL priority queue Class . . . . . . . . . . . . . . . 330
8.2 Implementing a Priority Queue with a List . . . . . . . . . . 331
8.2.1 A C++ Priority Queue Implementation using a List . . . 333
8.2.2 Selection-Sort and Insertion-Sort . . . . . . . . . . . . . 335
8.3 Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
8.3.1 The Heap Data Structure . . . . . . . . . . . . . . . . . 337
8.3.2 Complete Binary Trees and Their Representation . . . . 340
8.3.3 Implementing a Priority Queue with a Heap . . . . . . . 344
8.3.4 C++ Implementation . . . . . . . . . . . . . . . . . . . 349
8.3.5 Heap-Sort . . . . . . . . . . . . . . . . . . . . . . . . . 351
8.3.6 Bottom-Up Heap Construction ⋆ . . . . . . . . . . . . . 353
8.4 Adaptable Priority Queues . . . . . . . . . . . . . . . . . . . 357
8.4.1 A List-Based Implementation . . . . . . . . . . . . . . . 358
8.4.2 Location-Aware Entries . . . . . . . . . . . . . . . . . . 360
8.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
i i
i i
i i
xx Contents
9.4.1 Search and Update Operations in a Skip List . . . . . . 404
9.4.2 A Probabilistic Analysis of Skip Lists ⋆ . . . . . . . . . 408
9.5 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
9.5.1 The Dictionary ADT . . . . . . . . . . . . . . . . . . . 411
9.5.2 A C++ Dictionary Implementation . . . . . . . . . . . . 413
9.5.3 Implementations with Location-Aware Entries . . . . . . 415
9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
i i
i i
i i
Contents xxi
11.4 Sets and Union/Find Structures . . . . . . . . . . . . . . . . 533
11.4.1 The Set ADT . . . . . . . . . . . . . . . . . . . . . . . 533
11.4.2 Mergable Sets and the Template Method Pattern . . . . 534
11.4.3 Partitions with Union-Find Operations . . . . . . . . . . 538
11.5 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
11.5.1 Prune-and-Search . . . . . . . . . . . . . . . . . . . . . 542
11.5.2 Randomized Quick-Select . . . . . . . . . . . . . . . . . 543
11.5.3 Analyzing Randomized Quick-Select . . . . . . . . . . . 544
11.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
i i
i i
i i
xxii Contents
13.3.5 Breadth-First Search . . . . . . . . . . . . . . . . . . . 623
13.4 Directed Graphs . . . . . . . . . . . . . . . . . . . . . . . . . 626
13.4.1 Traversing a Digraph . . . . . . . . . . . . . . . . . . . 628
13.4.2 Transitive Closure . . . . . . . . . . . . . . . . . . . . . 630
13.4.3 Directed Acyclic Graphs . . . . . . . . . . . . . . . . . . 633
13.5 Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . 637
13.5.1 Weighted Graphs . . . . . . . . . . . . . . . . . . . . . 637
13.5.2 Dijkstra’s Algorithm . . . . . . . . . . . . . . . . . . . . 639
13.6 Minimum Spanning Trees . . . . . . . . . . . . . . . . . . . . 645
13.6.1 Kruskal’s Algorithm . . . . . . . . . . . . . . . . . . . . 647
13.6.2 The Prim-Jarnı́k Algorithm . . . . . . . . . . . . . . . . 651
13.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
i i
i i
i i
Chapter
1 A C++ Primer
Contents
1.1 Basic C++ Programming Elements . . . . . . . . . . . 2
1.1.1 A Simple C++ Program . . . . . . . . . . . . . . . . 2
1.1.2 Fundamental Types . . . . . . . . . . . . . . . . . . 4
1.1.3 Pointers, Arrays, and Structures . . . . . . . . . . . 7
1.1.4 Named Constants, Scope, and Namespaces . . . . . 13
1.2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . 16
1.2.1 Changing Types through Casting . . . . . . . . . . . 20
1.3 Control Flow . . . . . . . . . . . . . . . . . . . . . . . 23
1.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.4.1 Argument Passing . . . . . . . . . . . . . . . . . . . 28
1.4.2 Overloading and Inlining . . . . . . . . . . . . . . . 30
1.5 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.5.1 Class Structure . . . . . . . . . . . . . . . . . . . . 33
1.5.2 Constructors and Destructors . . . . . . . . . . . . . 37
1.5.3 Classes and Memory Allocation . . . . . . . . . . . . 40
1.5.4 Class Friends and Class Members . . . . . . . . . . . 43
1.5.5 The Standard Template Library . . . . . . . . . . . . 45
1.6 C++ Program and File Organization . . . . . . . . . . 47
1.6.1 An Example Program . . . . . . . . . . . . . . . . . 48
1.7 Writing a C++ Program . . . . . . . . . . . . . . . . . 53
1.7.1 Design . . . . . . . . . . . . . . . . . . . . . . . . . 54
1.7.2 Pseudo-Code . . . . . . . . . . . . . . . . . . . . . 54
1.7.3 Coding . . . . . . . . . . . . . . . . . . . . . . . . . 55
1.7.4 Testing and Debugging . . . . . . . . . . . . . . . . 57
1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 60
i i
i i
Discovering Diverse Content Through
Random Scribd Documents
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com