100% found this document useful (2 votes)
56 views

Data Structures and Algorithms in C 2nd Edition Michael T. Goodrich all chapter instant download

Structures

Uploaded by

tejiorigon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
56 views

Data Structures and Algorithms in C 2nd Edition Michael T. Goodrich all chapter instant download

Structures

Uploaded by

tejiorigon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Download Full ebookname - Read Now at ebookname.

com

Data Structures and Algorithms in C 2nd Edition


Michael T. Goodrich

https://ebookname.com/product/data-structures-and-
algorithms-in-c-2nd-edition-michael-t-goodrich/

OR CLICK BUTTON

DOWLOAD EBOOK

Discover More Ebook - Explore Now at ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Data Structures and Algorithms in Java 5th International


student edition M.T. Goodrich And R. Tamassia

https://ebookname.com/product/data-structures-and-algorithms-in-
java-5th-international-student-edition-m-t-goodrich-and-r-tamassia/

ebookname.com

Data Structures and Algorithms with JavaScript 1st Edition


Michael Mcmillan

https://ebookname.com/product/data-structures-and-algorithms-with-
javascript-1st-edition-michael-mcmillan/

ebookname.com

Data Structures Using C 2nd Edition A. K. Sharma

https://ebookname.com/product/data-structures-using-c-2nd-edition-a-k-
sharma/

ebookname.com

Mobile Device Security A Comprehensive Guide to Securing


Your Information in a Moving World 1st Edition Stephen
Fried
https://ebookname.com/product/mobile-device-security-a-comprehensive-
guide-to-securing-your-information-in-a-moving-world-1st-edition-
stephen-fried/
ebookname.com
Testamentary trusts strategies and precedents 2nd edition.
Edition Charles Rowland

https://ebookname.com/product/testamentary-trusts-strategies-and-
precedents-2nd-edition-edition-charles-rowland/

ebookname.com

Culinary calculations simplified math for culinary


professionals 1st Ed Edition Jones

https://ebookname.com/product/culinary-calculations-simplified-math-
for-culinary-professionals-1st-ed-edition-jones/

ebookname.com

Revolutionary Hope vs Free Market Fantasies Keeping the


Southern Africa Liberation Struggle Alive Theory Practice
Contexts John S. Saul
https://ebookname.com/product/revolutionary-hope-vs-free-market-
fantasies-keeping-the-southern-africa-liberation-struggle-alive-
theory-practice-contexts-john-s-saul/
ebookname.com

Organization Development The Process of Leading


Organizational Change 4th Edition Donald L. Anderson

https://ebookname.com/product/organization-development-the-process-of-
leading-organizational-change-4th-edition-donald-l-anderson/

ebookname.com

Encyclopedia of Suicide 3 Volume Set Oliver B. Torres

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

“main” — 2011/1/13 — 9:10 — page i — #1


i i

Data Structures and


Algorithms in C++
Second Edition

i i

i i
This page intentionally left blank
i i

“main” — 2011/1/13 — 9:10 — page iii — #3


i i

Data Structures and


Algorithms in C++
Second Edition

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

John Wiley & Sons, Inc.

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page iv — #4


i i

ACQUISITIONS EDITOR Beth Lang Golub


MARKETING MANAGER Chris Ruel
EDITORIAL ASSISTANT Elizabeth Mills
MEDIA EDITOR Thomas Kulesa
SENIOR DESIGNER Jim O’Shea
CONTENT MANAGER Micheline Frederick
PRODUCTION EDITOR Amy Weintraub
PHOTO EDITOR Sheena Goldstein

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

Printed in the United States of America


10 9 8 7 6 5 4 3 2 1

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page v — #5


i i

To Karen, Paul, Anna, and Jack


– Michael T. Goodrich

To Isabel
– Roberto Tamassia

To Jeanine
– David M. Mount

i i

i i
This page intentionally left blank
i i

“main” — 2011/1/13 — 9:10 — page vii — #7


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.

This book is related to the following books:


• M.T. Goodrich and R. Tamassia, Data Structures and Algorithms in Java,
John Wiley & Sons, Inc. This book has a similar overall structure to the
present book, but uses Java as the underlying language (with some modest,
but necessary pedagogical differences required by this approach).
• M.T. Goodrich and R. Tamassia, Algorithm Design: Foundations, Analysis,
and Internet Examples, John Wiley & Sons, Inc. This is a textbook for a more
advanced algorithms and data structures course, such as CS210 (T/W/C/S
versions) in the IEEE/ACM 2001 curriculum.
While this book retains the same pedagogical approach and general structure
as Data Structures and Algorithms in Java, the code fragments have been com-
pletely redesigned. We have been careful to make full use of C++’s capabilities and
design code in a manner that is consistent with modern C++ usage. In particular,
whenever appropriate, we make extensive use of C++ elements that are not part of
Java, including the C++ Standard Template Library (STL), C++ memory allocation
vii

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page viii — #8


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

“main” — 2011/1/13 — 9:10 — page ix — #9


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.

For the Student


For all readers, and especially for students, we include the following resources:
• All the C++ source code presented in this book.
• PDF handouts of Powerpoint slides (four-per-page) provided to instructors.
• A database of hints to all exercises, indexed by problem number.
• An online study guide, which includes solutions to selected exercises.
The hints should be of considerable use to anyone needing a little help getting
started on certain exercises, and the solutions should help anyone wishing to see
completed exercises. Students who have purchased a new copy of this book will
get password access to the hints and other password-protected online resources at
no extra charge. Other readers can purchase password access for a nominal fee.

For the Instructor


For instructors using this book, we include the following additional teaching aids:
• Solutions to over 200 of the book’s exercises.
• A database of additional exercises, suitable for quizes and exams.
• Additional C++ source code.
• Slides in Powerpoint and PDF (one-per-page) format.
• Self-contained, special-topic supplements, including discussions on convex
hulls, range trees, and orthogonal segment intersection.
The slides are fully editable, so as to allow an instructor using this book full free-
dom in customizing his or her presentations. All the online resources are provided
at no extra charge to any instructor adopting this book for his or her course.

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page x — #10


i i

x Preface

A Resource for Teaching Data Structures and Algorithms


This book contains many C++-code and pseudo-code fragments, and hundreds of
exercises, which are divided into roughly 40% reinforcement exercises, 40% cre-
ativity exercises, and 20% programming projects.
This book can be used for the CS2 course, as described in the 1978 ACM Com-
puter Science Curriculum, or in courses CS102 (I/O/B versions), CS103 (I/O/B ver-
sions), CS111 (A version), and/or CS112 (A/I/O/F/H versions), as described in the
IEEE/ACM 2001 Computing Curriculum, with instructional units as outlined in
Table 0.1.

Instructional Unit Relevant Material


PL1. Overview of Programming Languages Chapters 1 and 2
PL2. Virtual Machines Sections 14.1.1 and 14.1.2
PL3. Introduction to Language Translation Section 1.7
PL4. Declarations and Types Sections 1.1.2, 1.1.3, and 2.2.5
PL5. Abstraction Mechanisms Sections 2.2.5, 5.1–5.3, 6.1.1, 6.2.1, 6.3,
7.1, 7.3.1, 8.1, 9.1, 9.5, 11.4, and 13.1.1
PL6. Object-Oriented Programming Chapters 1 and 2 and Sections 6.2.1,
7.3.7, 8.1.2, and 13.3.1
PF1. Fundamental Programming Constructs Chapters 1 and 2
PF2. Algorithms and Problem-Solving Sections 1.7 and 4.2
PF3. Fundamental Data Structures Sections 3.1, 3.2, 5.1–5.3, 6.1–6.3, 7.1,
7.3, 8.1, 8.3, 9.1–9.4, 10.1, and 13.1.1
PF4. Recursion Section 3.5
SE1. Software Design Chapter 2 and Sections 6.2.1, 7.3.7,
8.1.2, and 13.3.1
SE2. Using APIs Sections 2.2.5, 5.1–5.3, 6.1.1, 6.2.1, 6.3,
7.1, 7.3.1, 8.1, 9.1, 9.5, 11.4, and 13.1.1
AL1. Basic Algorithmic Analysis Chapter 4
AL2. Algorithmic Strategies Sections 11.1.1, 11.5.1, 12.2, 12.3.1, and
12.4.2
AL3. Fundamental Computing Algorithms Sections 8.1.5, 8.2.2, 8.3.5, 9.2, and
9.3.1, and Chapters 11, 12, and 13
DS1. Functions, Relations, and Sets Sections 4.1, 8.1, and 11.4
DS3. Proof Techniques Sections 4.3, 6.1.3, 7.3.3, 8.3, 10.2–10.5,
11.2.1, 11.3.1, 11.4.3, 13.1.1, 13.3.1,
13.4, and 13.5
DS4. Basics of Counting Sections 2.2.3 and 11.1.5
DS5. Graphs and Trees Chapters 7, 8, 10, and 13
DS6. Discrete Probability Appendix A and Sections 9.2, 9.4.2,
11.2.1, and 11.5

Table 0.1: Material for units in the IEEE/ACM 2001 Computing Curriculum.

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page xi — #11


i i

Preface xi

Contents and Organization


The chapters for this course are organized to provide a pedagogical path that starts
with the basics of C++ programming and object-oriented design. We provide an
early discussion of concrete structures, like arrays and linked lists, in order to pro-
vide a concrete footing to build upon when constructing other data structures. We
then add foundational techniques like recursion and algorithm analysis, and, in the
main portion of the book, we present fundamental data structures and algorithms,
concluding with a discussion of memory management (that is, the architectural
underpinnings of data structures). Specifically, the chapters for this book are orga-
nized as follows:

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

“main” — 2011/1/13 — 9:10 — page xii — #12


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.

About the Authors


Professors Goodrich, Tamassia, and Mount are well-recognized researchers in al-
gorithms and data structures, having published many papers in this field, with ap-
plications to Internet computing, information visualization, computer security, and
geometric computing. They have served as principal investigators in several joint
projects sponsored by the National Science Foundation, the Army Research Of-
fice, the Office of Naval Research, and the Defense Advanced Research Projects
Agency. They are also active in educational technology research.
Michael Goodrich received his Ph.D. in Computer Science from Purdue Uni-
versity in 1987. He is currently a Chancellor’s Professor in the Department of Com-
puter Science at University of California, Irvine. Previously, he was a professor at
Johns Hopkins University. He is an editor for a number of journals in computer
science theory, computational geometry, and graph algorithms. He is an ACM Dis-
tinguished Scientist, a Fellow of the American Association for the Advancement of
Science (AAAS), a Fulbright Scholar, and a Fellow of the IEEE. He is a recipient of
the IEEE Computer Society Technical Achievement Award, the ACM Recognition
of Service Award, and the Pond Award for Excellence in Undergraduate Teaching.

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page xiii — #13


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

“main” — 2011/1/13 — 9:10 — page xiv — #14


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

“main” — 2011/1/13 — 9:10 — page xv — #15


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

“main” — 2011/1/13 — 9:10 — page xvi — #16


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

3 Arrays, Linked Lists, and Recursion 103


3.1 Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
3.1.1 Storing Game Entries in an Array . . . . . . . . . . . . . 104
3.1.2 Sorting an Array . . . . . . . . . . . . . . . . . . . . . . 109
3.1.3 Two-Dimensional Arrays and Positional Games . . . . . 111
3.2 Singly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . 117
3.2.1 Implementing a Singly Linked List . . . . . . . . . . . . 117
3.2.2 Insertion to the Front of a Singly Linked List . . . . . . 119
3.2.3 Removal from the Front of a Singly Linked List . . . . . 119
3.2.4 Implementing a Generic Singly Linked List . . . . . . . . 121
3.3 Doubly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . 123
3.3.1 Insertion into a Doubly Linked List . . . . . . . . . . . . 123
3.3.2 Removal from a Doubly Linked List . . . . . . . . . . . 124
3.3.3 A C++ Implementation . . . . . . . . . . . . . . . . . . 125
3.4 Circularly Linked Lists and List Reversal . . . . . . . . . . . 129
3.4.1 Circularly Linked Lists . . . . . . . . . . . . . . . . . . . 129
3.4.2 Reversing a Linked List . . . . . . . . . . . . . . . . . . 133
3.5 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
3.5.1 Linear Recursion . . . . . . . . . . . . . . . . . . . . . . 140
3.5.2 Binary Recursion . . . . . . . . . . . . . . . . . . . . . 144
3.5.3 Multiple Recursion . . . . . . . . . . . . . . . . . . . . 147
3.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

4 Analysis Tools 153


4.1 The Seven Functions Used in This Book . . . . . . . . . . . 154
4.1.1 The Constant Function . . . . . . . . . . . . . . . . . . 154
4.1.2 The Logarithm Function . . . . . . . . . . . . . . . . . 154

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page xvii — #17


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

5 Stacks, Queues, and Deques 193


5.1 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
5.1.1 The Stack Abstract Data Type . . . . . . . . . . . . . . 195
5.1.2 The STL Stack . . . . . . . . . . . . . . . . . . . . . . 196
5.1.3 A C++ Stack Interface . . . . . . . . . . . . . . . . . . 196
5.1.4 A Simple Array-Based Stack Implementation . . . . . . 198
5.1.5 Implementing a Stack with a Generic Linked List . . . . 202
5.1.6 Reversing a Vector Using a Stack . . . . . . . . . . . . . 203
5.1.7 Matching Parentheses and HTML Tags . . . . . . . . . 204
5.2 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
5.2.1 The Queue Abstract Data Type . . . . . . . . . . . . . 208
5.2.2 The STL Queue . . . . . . . . . . . . . . . . . . . . . . 209
5.2.3 A C++ Queue Interface . . . . . . . . . . . . . . . . . . 210
5.2.4 A Simple Array-Based Implementation . . . . . . . . . . 211
5.2.5 Implementing a Queue with a Circularly Linked List . . . 213
5.3 Double-Ended Queues . . . . . . . . . . . . . . . . . . . . . . 217
5.3.1 The Deque Abstract Data Type . . . . . . . . . . . . . 217
5.3.2 The STL Deque . . . . . . . . . . . . . . . . . . . . . . 218
5.3.3 Implementing a Deque with a Doubly Linked List . . . . 218
5.3.4 Adapters and the Adapter Design Pattern . . . . . . . . 220
5.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page xviii — #18


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

“main” — 2011/1/13 — 9:10 — page xix — #19


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

9 Hash Tables, Maps, and Skip Lists 367


9.1 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
9.1.1 The Map ADT . . . . . . . . . . . . . . . . . . . . . . 369
9.1.2 A C++ Map Interface . . . . . . . . . . . . . . . . . . . 371
9.1.3 The STL map Class . . . . . . . . . . . . . . . . . . . . 372
9.1.4 A Simple List-Based Map Implementation . . . . . . . . 374
9.2 Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
9.2.1 Bucket Arrays . . . . . . . . . . . . . . . . . . . . . . . 375
9.2.2 Hash Functions . . . . . . . . . . . . . . . . . . . . . . 376
9.2.3 Hash Codes . . . . . . . . . . . . . . . . . . . . . . . . 376
9.2.4 Compression Functions . . . . . . . . . . . . . . . . . . 380
9.2.5 Collision-Handling Schemes . . . . . . . . . . . . . . . . 382
9.2.6 Load Factors and Rehashing . . . . . . . . . . . . . . . 386
9.2.7 A C++ Hash Table Implementation . . . . . . . . . . . . 387
9.3 Ordered Maps . . . . . . . . . . . . . . . . . . . . . . . . . . 394
9.3.1 Ordered Search Tables and Binary Search . . . . . . . . 395
9.3.2 Two Applications of Ordered Maps . . . . . . . . . . . . 399
9.4 Skip Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page xx — #20


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

10 Search Trees 423


10.1 Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . 424
10.1.1 Searching . . . . . . . . . . . . . . . . . . . . . . . . . 426
10.1.2 Update Operations . . . . . . . . . . . . . . . . . . . . 428
10.1.3 C++ Implementation of a Binary Search Tree . . . . . . 432
10.2 AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
10.2.1 Update Operations . . . . . . . . . . . . . . . . . . . . 440
10.2.2 C++ Implementation of an AVL Tree . . . . . . . . . . . 446
10.3 Splay Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
10.3.1 Splaying . . . . . . . . . . . . . . . . . . . . . . . . . . 450
10.3.2 When to Splay . . . . . . . . . . . . . . . . . . . . . . . 454
10.3.3 Amortized Analysis of Splaying ⋆ . . . . . . . . . . . . 456
10.4 (2,4) Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
10.4.1 Multi-Way Search Trees . . . . . . . . . . . . . . . . . . 461
10.4.2 Update Operations for (2, 4) Trees . . . . . . . . . . . . 467
10.5 Red-Black Trees . . . . . . . . . . . . . . . . . . . . . . . . . 473
10.5.1 Update Operations . . . . . . . . . . . . . . . . . . . . 475
10.5.2 C++ Implementation of a Red-Black Tree . . . . . . . . 488
10.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492

11 Sorting, Sets, and Selection 499


11.1 Merge-Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
11.1.1 Divide-and-Conquer . . . . . . . . . . . . . . . . . . . . 500
11.1.2 Merging Arrays and Lists . . . . . . . . . . . . . . . . . 505
11.1.3 The Running Time of Merge-Sort . . . . . . . . . . . . 508
11.1.4 C++ Implementations of Merge-Sort . . . . . . . . . . . 509
11.1.5 Merge-Sort and Recurrence Equations ⋆ . . . . . . . . . 511
11.2 Quick-Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
11.2.1 Randomized Quick-Sort . . . . . . . . . . . . . . . . . . 521
11.2.2 C++ Implementations and Optimizations . . . . . . . . . 523
11.3 Studying Sorting through an Algorithmic Lens . . . . . . . 526
11.3.1 A Lower Bound for Sorting . . . . . . . . . . . . . . . . 526
11.3.2 Linear-Time Sorting: Bucket-Sort and Radix-Sort . . . . 528
11.3.3 Comparing Sorting Algorithms . . . . . . . . . . . . . . 531

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page xxi — #21


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

12 Strings and Dynamic Programming 553


12.1 String Operations . . . . . . . . . . . . . . . . . . . . . . . . 554
12.1.1 The STL String Class . . . . . . . . . . . . . . . . . . . 555
12.2 Dynamic Programming . . . . . . . . . . . . . . . . . . . . . 557
12.2.1 Matrix Chain-Product . . . . . . . . . . . . . . . . . . . 557
12.2.2 DNA and Text Sequence Alignment . . . . . . . . . . . 560
12.3 Pattern Matching Algorithms . . . . . . . . . . . . . . . . . 564
12.3.1 Brute Force . . . . . . . . . . . . . . . . . . . . . . . . 564
12.3.2 The Boyer-Moore Algorithm . . . . . . . . . . . . . . . 566
12.3.3 The Knuth-Morris-Pratt Algorithm . . . . . . . . . . . . 570
12.4 Text Compression and the Greedy Method . . . . . . . . . 575
12.4.1 The Huffman-Coding Algorithm . . . . . . . . . . . . . 576
12.4.2 The Greedy Method . . . . . . . . . . . . . . . . . . . . 577
12.5 Tries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
12.5.1 Standard Tries . . . . . . . . . . . . . . . . . . . . . . . 578
12.5.2 Compressed Tries . . . . . . . . . . . . . . . . . . . . . 582
12.5.3 Suffix Tries . . . . . . . . . . . . . . . . . . . . . . . . 584
12.5.4 Search Engines . . . . . . . . . . . . . . . . . . . . . . 586
12.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587

13 Graph Algorithms 593


13.1 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
13.1.1 The Graph ADT . . . . . . . . . . . . . . . . . . . . . . 599
13.2 Data Structures for Graphs . . . . . . . . . . . . . . . . . . . 600
13.2.1 The Edge List Structure . . . . . . . . . . . . . . . . . . 600
13.2.2 The Adjacency List Structure . . . . . . . . . . . . . . . 603
13.2.3 The Adjacency Matrix Structure . . . . . . . . . . . . . 605
13.3 Graph Traversals . . . . . . . . . . . . . . . . . . . . . . . . . 607
13.3.1 Depth-First Search . . . . . . . . . . . . . . . . . . . . 607
13.3.2 Implementing Depth-First Search . . . . . . . . . . . . . 611
13.3.3 A Generic DFS Implementation in C++ . . . . . . . . . . 613
13.3.4 Polymorphic Objects and Decorator Values ⋆ . . . . . . 621

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page xxii — #22


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

14 Memory Management and B-Trees 665


14.1 Memory Management . . . . . . . . . . . . . . . . . . . . . . 666
14.1.1 Memory Allocation in C++ . . . . . . . . . . . . . . . . 669
14.1.2 Garbage Collection . . . . . . . . . . . . . . . . . . . . 671
14.2 External Memory and Caching . . . . . . . . . . . . . . . . . 673
14.2.1 The Memory Hierarchy . . . . . . . . . . . . . . . . . . 673
14.2.2 Caching Strategies . . . . . . . . . . . . . . . . . . . . 674
14.3 External Searching and B-Trees . . . . . . . . . . . . . . . . 679
14.3.1 (a, b) Trees . . . . . . . . . . . . . . . . . . . . . . . . 680
14.3.2 B-Trees . . . . . . . . . . . . . . . . . . . . . . . . . . 682
14.4 External-Memory Sorting . . . . . . . . . . . . . . . . . . . . 683
14.4.1 Multi-Way Merging . . . . . . . . . . . . . . . . . . . . 684
14.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685

A Useful Mathematical Facts 689


Bibliography 697
Index 702

i i

i i
i i

“main” — 2011/1/13 — 9:10 — page 1 — #23


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

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

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. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

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.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• 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 provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

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.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

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.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like