Althoughthe book provides an introduction to these topics in connection withthe implementations of abstract data types ADTs as classes, the empha-sis remains on the ADTs, not on C++.. Th
Trang 2PREFACE
Welcome to the third edition of Data Abstraction and Problem Solving with C++: Walls and Mirrors Since the publication of the second edition, weall have gained experience with teaching data abstraction in an object-oriented way using C++ This edition reflects that experience and theevolution that C++ has taken
This book is based on the original Intermediate Problem Solving and Data Structures: Walls and Mirrors by Paul Helman and Robert Veroff(© 1986 by The Benjamin/Cummings Publishing Company, Inc.) Thiswork builds on their organizational framework and overall perspectiveand includes technical and textual content, examples, figures, and exer-cises derived from the original work Professors Helman and Veroff intro-duced two powerful analogies, walls and mirrors, that have made iteasier for us to teach—and to learn—computer science
With its focus on data abstraction and other problem-solving tools,this book is designed for a second course in computer science In recog-nition of the dynamic nature of the discipline and the great diversity inundergraduate computer science curricula, this book includes compre-hensive coverage of enough topics to make it appropriate for othercourses as well For example, you can use this book in courses such asintroductory data structures or advanced programming and problemsolving The goal remains to give students a superior foundation in dataabstraction, object-oriented programming, and other modern problem-solving techniques
TO THE STUDENT
Thousands of students before you have read and learned from Walls and Mirrors The walls and mirrors in the title represent two fundamentalproblem-solving techniques that appear throughout the presentation Dataabstraction isolates and hides the implementation details of a modulefrom the rest of the program, much as a wall can isolate and hide you fromyour neighbor Recursion is a repetitive technique that solves a problem bysolving smaller problems of exactly the same type, much as mirror imagesgrow smaller with each reflection
This book was written with you in mind As former college students,and as educators who are constantly learning, we appreciate the impor-tance of a clear presentation Our goal is to make this book as under-standable as possible To help you learn and to review for exams, we havePreface C++ 3e.fm Page iii Monday, July 16, 2001 1:44 PM
Trang 3iv APPROACH
included such learning aids as margin notes, chapter summaries, self-testexercises with answers, and a glossary As a help during programming,you will find C++ reference material in the appendixes and inside thecovers You should review the list of this book’s features given later in thispreface in the section “Pedagogical Features.”
The presentation makes some basic assumptions about your edge of C++ Some of you may need to review this language or learn it forthe first time by consulting Appendix A of this book You will need toknow about the selection statements if and switch; the iteration state-ments for, while, and do; functions and argument passing; arrays;strings; structures; and files This book covers C++ classes in Chapters 1, 3,and 8 and does not assume that you already know this topic We assume
knowl-no experience with recursive functions, which are included in Chapters 2and 5
All of the C++ source code that appears in this book is available foryour use Later in this preface, the description of supplementary materi-als tells you how to obtain these files Note, however, that your instruc-tor may already have obtained them for you
APPROACH
This edition of Walls and Mirrors uses C++ to enhance its emphasis ondata abstraction and data structures The book carefully accounts for thestrengths and weaknesses of the C++ language and remains committed
to a pedagogical approach that makes the material accessible to students
at the introductory level
Prerequisites
We assume that readers either know the fundamentals of C++ or knowanother language and have an instructor who will help them make thetransition to C++ by using the provided appendix The book formallyintroduces C++ classes, and so does not assume prior knowledge ofthem Included are the basic concepts of object-oriented programming,inheritance, virtual functions, and class templates, all in C++ Althoughthe book provides an introduction to these topics in connection withthe implementations of abstract data types (ADTs) as classes, the empha-sis remains on the ADTs, not on C++ The material is presented in thecontext of object-based programming, but it assumes that future courseswill cover object-oriented design and software engineering in detail, sothat the focus can remain on data abstraction We do, however, intro-duce the Unified Modeling Language (UML) as a design tool
Flexibility
The extensive coverage of this book should provide you with the rial that you want for your course You can select the topics you desirePreface C++ 3e.fm Page iv Monday, July 16, 2001 1:44 PM
Trang 4mate-PREFACE v
and present them in an order that fits your course The chapter
depen-dency chart shows which chapters should be covered before a given
chapter can be taught
In Part I, you can choose among topics according to your students’
background Three of the chapters in this part provide an extensive
introduction to data abstraction and recursion Both topics are
impor-tant, and there are various opinions about which should be taught first
Chapter 6 Stacks
Chapter 8 Advanced C++ topics
Chapter 7 Queues
Chapter 10 Trees
Chapter 14 Section on
external tables
Section on external sorting
Chapter 9 Algorithm efficiency, sorting
Chapter 2 Recursion
Chapter 5 More recursion
Chapter 1 Principles
Chapter 3 Data abstraction
Chapter 4 Linked lists
Chapter 11 Tables, priority queues
Chapter 12 Advanced tables
Chapter 13 Graphs
CHAPTER DEPENDENCY CHART
This chart indicates the chapters that should be studied before you cover a particular chapter.
Dependency by one section of chapter Dependency that you can ignore Preface C++ 3e.fm Page v Monday, July 16, 2001 1:44 PM
Trang 5vi APPROACH
Although in this book a chapter on recursion both precedes and followsthe chapter on data abstraction, you can simply rearrange this order.Part II treats topics that you can also cover in a flexible order Forexample, you can cover all or parts of Chapter 8 on advanced C++ eitherbefore or after you cover stacks (Chapter 6) You can cover algorithm effi-ciency and sorting (Chapter 9) any time after Chapter 5 You can intro-duce trees before queues or graphs before tables, or cover hashing,balanced search trees, or priority queues any time after tables and in anyorder You also can cover external methods (Chapter 14) earlier in thecourse For example, you can cover external sorting after you covermergesort in Chapter 9
Problem Solving
This book helps students learn to integrate problem-solving and ming abilities by emphasizing both the thought processes and the tech-niques that computer scientists use Learning how a computer scientistdevelops, analyzes, and implements a solution is just as important aslearning the mechanics of the algorithm; a cookbook approach to thematerial is insufficient
program-The presentation includes analytical techniques for the development
of solutions within the context of example problems Abstraction, thesuccessive refinement of both algorithms and data structures, and recur-sion are used to design solutions to problems throughout the book.C++ pointers and linked list processing are introduced early and used
in building data structures The book also introduces at an elementarylevel the order-of-magnitude analysis of algorithms This approach allowsthe consideration—first at an informal level, then more quantitatively—
of the advantages and disadvantages of array-based and pointer-baseddata structures An emphasis on the trade-offs among potential solutionsand implementations is a central problem-solving theme
Finally, programming style, documentation including preconditionsand postconditions, debugging aids, and loop invariants are importantPreface C++ 3e.fm Page vi Monday, July 16, 2001 1:44 PM
Trang 6PREFACE vii
parts of the problem-solving methodology used to implement and verify
solutions These topics are covered throughout the book
Applications
Classic application areas arise in the context of the major topics of this
book For example, the binary search, quicksort, and mergesort
algo-rithms provide important applications of recursion and introduce
order-of-magnitude analysis Such topics as balanced search trees, hashing, and
file indexing continue the discussion of searching Searching and sorting
are considered again in the context of external files
Algorithms for recognizing and evaluating algebraic expressions arefirst introduced in the context of recursion and are considered again
later as an application of stacks Other applications include, for
exam-ple, the Eight Queens problem as an example of backtracking,
event-driven simulation as an application of queues, and graph searching and
traversals as other important applications of stacks and queues
NEW AND REVISED MATERIAL
This edition retains the underlying approach and philosophy of the
second edition We present data abstraction and programming both as
general concepts and in the context of C++ No sentence, example, margin
note, or figure escaped scrutiny during the preparation of this edition
Many changes, additions, and deletions to the text, figures, and margin
notes were made to update and clarify the presentation All C++ programs
were revised to reflect recent changes to the language
Although we revised all chapters and appendixes, here is a list of themajor changes to the book
• The specifications of all ADT operations now use UML notation
to clarify the purpose and data type of their parameters
• Chapter 1 enhances its coverage of object-oriented design andintroduces the Unified Modeling Language (UML) We haverevised our naming conventions for C++ identifiers to be consis-tent with common practice This change will help students whoeither know Java or will learn Java later
• Chapter 3 briefly introduces inheritance after its presentation ofC++ classes Also covered are the C++ namespace and excep-tions Although the ADTs in this chapter return boolean flags toindicate error conditions, subsequent chapters use exceptions
• Chapter 4 includes a new section about the C++ Standard plate Library (STL) Class templates, containers, and iterators areintroduced More extensive coverage of these topics is deferreduntil Chapter 8 Chapter 4 also considers the STL class list.Preface C++ 3e.fm Page vii Monday, July 16, 2001 1:44 PM
Trang 7Tem-viii OVERVIEW
Other classes in the STL are presented throughout the book, andyou can either skip or postpone covering them, as desired
• Chapter 6 includes the STL class stack
• Chapter 7 includes the STL class queue
• Chapter 8 provides a deeper discussion of inheritance and classtemplates than was given earlier Also covered are C++ friendsand iterators
• Appendix A is an updated review of C++ that now includes erage of exceptions Appendix C is an updated list of C++ head-ers Appendix E is new and contains the STL classes list, stack,and queue
cov-OVERVIEW
The pedagogical features and organization of this book were carefullydesigned to facilitate learning and to allow instructors to tailor the mate-rial easily to a particular course
Pedagogical Features
This book contains the following features that help students not onlyduring their first reading of the material, but also during subsequentreview:
• Chapter outlines and previews
• Boxes listing key concepts in the material
• Margin notes
• Chapter summaries
• Cautionary warnings about common errors and misconceptions
• Self-test exercises with answers
• Chapter exercises and programming problems The most lenging exercises are labeled with asterisks Answers to the exer-cises appear in the Instructor’s Resource Manual.
chal-• Specifications for all major ADTs in both English and pseudocode,
as well as in UML notation
• C++ class definitions for all major ADTs
• Examples that illustrate the role of classes and ADTs in theproblem-solving process
• Appendixes, including a review of C++
• Glossary of terms
Preface C++ 3e.fm Page viii Monday, July 16, 2001 1:44 PM
Trang 8PREFACE ix
Organization
The chapters in this book are organized into two parts In most cases,
Chapters 1 through 11 will form the core of a one-semester course
Chapters 1 or 2 might be review material for your students The coverage
given to Chapters 11 through 14 will depend on the role the course
plays in your curriculum More detailed suggestions for using this book
with different courses appear in the Instructor’s Resource Manual.
Part I: Problem-Solving Techniques. The first chapter in Part I
empha-sizes the major issues in programming and software engineering A new
introduction to the Unified Modeling Language (UML) is given here The
next chapter discusses recursion for those students who have had little
exposure to this important topic The ability to think recursively is one
of the most useful skills that a computer scientist can possess and is
often of great value in helping one to better understand the nature of a
problem Recursion is discussed extensively in this chapter and again in
Chapter 5 and is used throughout the book Included examples range
from simple recursive definitions to recursive algorithms for language
recognition, searching, and sorting
Chapter 3 covers data abstraction and abstract data types (ADTs) indetail After a discussion of the specification and use of an ADT, the
chapter presents C++ classes and uses them to implement ADTs This
chapter includes a brief introduction to inheritance, the C++ namespace,
and exceptions Chapter 4 presents additional implementation tools in
its discussion of C++ pointer variables and linked lists This chapter also
introduces class templates, the C++ Standard Template Library (STL),
containers, and iterators
You can choose among the topics in Part I according to the ground of your students and cover these topics in several orders
back-Part II: Problem Solving with Abstract Data Types. Part II continues to
explore data abstraction as a problem-solving technique Basic abstract
data types such as the stack, queue, binary tree, binary search tree, table,
heap, and priority queue are first specified and then implemented as
classes The ADTs are used in examples and their implementations are
compared
Chapter 8 extends the coverage of C++ classes by further developinginheritance, class templates, and iterators The chapter then introduces
virtual functions and friends Chapter 9 formalizes the earlier discussions
of an algorithm’s efficiency by introducing order-of-magnitude analysis
and Big O notation The chapter examines the efficiency of several
searching and sorting algorithms, including the recursive mergesort and
quicksort
Part II also includes advanced topics—such as balanced search trees(2-3, 2-3-4, red-black, and AVL trees) and hashing—that are examined as
table implementations These implementations are analyzed to
deter-mine the table operations that each supports best
Preface C++ 3e.fm Page ix Monday, July 16, 2001 1:44 PM
Trang 9x TALK TO US
Finally, data storage in external direct access files is considered.Mergesort is modified to sort such data, and external hashing and B-treeindexes are used to search it These searching algorithms are generaliza-tions of the internal hashing schemes and 2-3 trees already developed
• The source code and errata list are available at the URL
www.aw.com/cssupport
• Instructor’s Resource Manual Solutions to the exercises at the end ofthe chapters as well as teaching notes and suggestions are avail-able only to instructors through your Addison-Wesley sales repre-sentative
TALK TO US
and corrections will be greatly appreciated You can contact us directly
by e-mail at
carrano@acm.org
or
prichard@bryant.edu
or through the publisher:
Computer Science Editorial Office Addison-Wesley
75 Arlington StreetBoston, MA 02116Preface C++ 3e.fm Page x Monday, July 16, 2001 1:44 PM
Trang 10PREFACE xi
ACKNOWLEDGMENTS
The suggestions from outstanding reviewers contributed greatly to this
book’s present form In alphabetical order, they are
Vicki H Allan—Utah State University
Don Bailey—Carleton University
Sebastian Elbaum—University of Nebraska, Lincoln
Matthew Evett—Eastern Michigan University
Susan Gauch—University of Kansas
Martin Granier—Western Washington University
Judy Hankins—Middle Tennessee State University
Sr Joan Harnett—Manhattan College
Tom Irby—University of North Texas
Edwin J Kay—Lehigh University
David Naffin—Fullerton College
Paul Nagin—Hofstra University
Bina Ramamurthy—SUNY at Buffalo
Dwight Tunistra Karen VanHouten—University of Idaho
Kathie Yerion—Gonzaga University
We especially thank the people who produced this book Our editors
at Addison-Wesley, Susan Hartman Sullivan and Katherine Harutunian,
provided invaluable guidance and assistance This book would not have
been printed on time without our project manager, Daniel Rausch from
Argosy Publishing Thank you, Daniel, for keeping us on schedule
Many thanks to our copy editor, Rebecca Pepper, for smoothing outour rough edges Also, Pat Mahtani, Michael Hirsch, Gina Hagen, Jarrod
Gibbons, Michelle Renda, and Joe Vetere contributed their expertise and
care throughout the production of this book
Many other wonderful people have contributed in various ways
They are Doug McCreadie, Michael Hayden, Sarah Hayden, Andrew
Hayden, Albert Prichard, Ted Emmott, Maybeth Conway, Lorraine
Berube, Marge White, James Kowalski, Gerard Baudet, Joan Peckham, Ed
Lamagna, Victor Fay-Wolfe, Bala Ravikumar, Lisa DiPippo, Jean-Yves
Preface C++ 3e.fm Page xi Monday, July 16, 2001 1:44 PM
Trang 11xii ACKNOWLEDGMENTS
Hervé, Hal Records, Wally Wood, Elaine Lavallee, Ken Sousa, SallyLawrence, Lianne Dunn, Gail Armstrong, Tom Manning, Jim Labonte,Jim Abreu, and Bill Harding
Numerous other people provided input for the previous editions of
Walls and Mirrors at various stages of its development All of their ments were useful and greatly appreciated In alphabetical order, they areKarl Abrahamson, Stephen Alberg, Ronald Alferez, Vicki Allan, JihadAlmahayni, James Ames, Claude W Anderson, Andrew Azzinaro, TonyBaiching, Don Bailey, N Dwight Barnette, Jack Beidler, Wolfgang W.Bein, Sto Bell, David Berard, John Black, Richard Botting, Wolfin Brum-ley, Philip Carrigan, Stephen Clamage, Michael Clancy, David Clayton,Michael Cleron, Chris Constantino, Shaun Cooper, Charles Denault,Vincent J DiPippo, Suzanne Dorney, Colleen Dunn, Carl Eckberg, KarlaSteinbrugge Fant, Jean Foltz, Susan Gauch, Marguerite Hafen , RandyHale, George Hamer, Judy Hankins, Lisa Hellerstein, Mary Lou Hines,Jack Hodges, Stephanie Horoschak, Lily Hou, John Hubbard, Kris Jensen,Thomas Judson, Laura Kenney, Roger King, Ladislav Kohout, JimLaBonte, Jean Lake, Janusz Laski, Cathie LeBlanc, Urban LeJeune, John
com-M Linebarger, Ken Lord, Paul Luker, Manisha Mande, Pierre-Arnoul deMarneffe, John Marsaglia, Jane Wallace Mayo, Mark McCormick, DanMcCracken, Vivian McDougal, Shirley McGuire, Sue Medeiros, JimMiller, Guy Mills, Rameen Mohammadi, Cleve Moler, Narayan Murthy,Paul Nagin, Rayno Niemi, John O’Donnell, Andrew Oldroyd, LarryOlsen, Raymond L Paden, Roy Pargas, Brenda C Parker, Thaddeus F.Pawlicki, Keith Pierce, Lucasz Pruski, George B Purdy, David Radford,Steve Ratering, Stuart Regis, J D Robertson, Robert A Rossi, John Rowe,Michael E Rupp, Sharon Salveter, Charles Saxon, Chandra Sekharan,Linda Shapiro, Yujian Sheng, Mary Shields, Ronnie Smith, Carl Spicola,Richard Snodgrass, Neil Snyder, Chris Spannabel, Paul Spirakis, ClintonStaley, Matt Stallman, Mark Stehlick, Benjamin T Schomp, HarrietTaylor, David Teague, David Tetreault, John Turner, Susan Wallace, James
E Warren, Jerry Weltman, Nancy Wiegand, Howard Williams, BradWilson, James Wirth, Salih Yurttas, and Alan Zaring
Thank you all
F M C
J J P.Preface C++ 3e.fm Page xii Monday, July 16, 2001 1:44 PM