Data Structures and Algorithms in Java: Third Edition
Data Structures and Algorithms in Java: Third Edition
Algorithms in Java
Third Edition
Data Structures and
Algorithms in Java
Third Edition
Michael T. Goodrich
Department of Computer Science
University of California, Irvine
Roberto Tamassia
Department of Computer Science
Brown University
About the cover: Anatjari Tjampitjinpa, “Snake Dreaming,” 1988. Reproduced courtesy of
Patrick Corbally Stourton of Corbally Stourton Contemporary Art Ltd.
This book was set in LATEX by the authors and printed and bound by Von Hoffmann, Inc.
The cover was printed by Von Hoffmann, Inc.
Copyright
c 2004, John Wiley & Sons, Inc. All rights reserved.
ISBN 0-471-46983-1
10 9 8 7 6 5 4 3 2 1
To Karen, Paul, Anna, and Jack
– Michael T. Goodrich
To Isabel
– Roberto Tamassia
Preface to the Third Edition
This third edition is designed to provide an introduction to data structures and al-
gorithms, including their design, analysis, and implementation. With respect to
computer science and computer engineering curricula, we have written this book
to be primarily for Freshman-Sophomore level data structures courses. In terms
of curricula based on the IEEE/ACM 2001 Computing 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 versions). In terms of curricula based
on the 1978 ACM Computer Science Curriculum, this book is intended for the
CS2 course. We discuss its use for such courses in more detail later in this preface.
The major changes, with respect to the second edition, are the following:
• M.T. Goodrich, R. Tamassia, and D.M. Mount, Data Structures and Algo-
rithms in C++, John Wiley & Sons, Inc., 2004. This book has a similar over-
all structure to the present book, but uses C++ as the underlying language
(with some modest, but necessary pedagogical differences required by this
approach). Thus, it could make for a handy companion book in a curriculum
that allows for either a Java or C++ track in the introductory courses.
• M.T. Goodrich and R. Tamassia, Algorithm Design: Foundations, Analysis,
and Internet Examples, John Wiley & Sons, Inc., 2002. This is a textbook
for a more advanced algorithms and data structures course, such as CS7 in
the ACM 1978 curriculum or CS210 (T/W/C/S versions) in the IEEE/ACM
2001 curriculum.
vii
viii Preface
Use as a Textbook
The design and analysis of efficient data structures has long been recognized as a
vital subject in computing, for the study of data structures is part of the core of ev-
ery 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 or introduction to computer science course and this is followed by a
more in-depth introduction to data structures in the following course(s). Further-
more, 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 of 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 as instances of an abstract
data type (ADT) that includes a repertory of methods for performing operations on
the data. 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 in-
troduce important design patterns as means to organize those implementations into
classes, methods, and objects.
For each ADT presented in this book, we provide an associated Java inter-
face. Also, concrete data structures realizing the ADTs are provided as Java classes
implementing the above interfaces. We also give Java implementations of funda-
mental algorithms (such as sorting and graph traversals) and of sample applications
of data structures (such as HTML tag matching and a photo album). Due to space
limitations, we sometimes show only code fragments in the book and make the full
source code available on the companion Web site http://java.datastructures.net.
The Java code in this the book implementing fundamental data structures is
organized in a single Java package, net.datastructures. This package forms a co-
herent library of data structures and algorithms in Java specifically designed for
educational purposes in a way that is complementary with the Java Collections
Framework.
Preface ix
http://java.datastructures.net
Students are encouraged to use this site along with the book, to help with exercises
and increase understanding of the subject. Instructors are likewise welcome to use
the site to help plan, organize, and present their course materials.
We feel that the Java animations and interactive applets should be of particular in-
terest, since they allow readers to interactively “play” with different data structures,
which leads to better understanding of the different ADTs. In addition, the hints
should be of considerable use to anyone needing a little help getting started on
certain exercises.
The slides are fully editable, so as to allow an instructor using this book full free-
dom in customizing his or her presentations.
x Preface
Teaching Options
This book is also structured to allow instructors a great deal of freedom in organiz-
ing and presenting the material. In Table 0.2, we illustrate some possible uses of
this book for an intermediate-paced data structures course.
Prerequisites
We have written this book assuming that the reader comes to it with certain knowl-
edge. That is, we assume that the reader is at least vaguely familiar with a high-level
programming language, such as C, C++, or Java, and that he or she understands the
main constructs from such a high-level language, including:
• Variables and expressions
• Methods (also known as functions 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 Java, we provide a primer on the Java language in Chapter 1. Still, this
book is primarily a data structures book, not a Java book; hence, it does not provide
a comprehensive treatment of Java. Nevertheless, we do not assume that the reader
is necessarily familiar with object-oriented design or with linked structures, such
as linked lists, for these topics are covered in the core chapters of this book.
xii Preface
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 ex-
ercises, programming assignments, and algorithm animation systems. In particular,
we would like to thank Jeff Achter, James Baker, Ryan Baker, Benjamin Boer, Mike
Boilen, Devin Borland, Lubomir Bourdev, Stina Bridgeman, Bryan Cantrill, Yi-Jen
Chiang, Robert Cohen, David Emory, Jody Fanto, Ashim Garg, Natasha Gelfand,
Mark Handy, Michael Horn, Benoı̂t Hudson, Jovanna Ignatowicz, Seth Padowitz,
James Piechota, Dan Polivy, Susannah Raub, Andy Schwerin, Michael Shapiro,
Michael Shin, Christian Straub, Galina Shubina, Nikos Triandopoulos, and Luca
Vismara. Lubomir Bourdev, Mike Demmer, Mark Handy, Michael Horn, and Scott
Speigler developed a basic Java tutorial, which ultimately led to Chapter 1, Java
Programming.
Special thanks go to Eric Zamore, who contributed to the development of the
Java code examples in this book and to the design, implementation and testing of
the net.datastructures library of data structures and algorithms in Java.
Many students and instructors have used the two previous editions of this book
and their experiences and responses have helped shape this third edition.
There have been a number of friends and colleagues whose comments have lead
to improvements in the text. We are particularly thankful to Karen Goodrich, Art
Moorshead, David Mount, Scott Smith and Ioannis Tollis for their insightful com-
ments. In addition, contributions by David Mount to Section 2.5.1 and to several
figures are gratefully acknowledged.
We are also truly indebted to the outside reviewers and readers for their copi-
ous comments, emails, and constructive criticism, which were extremely useful in
writing the third 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.
xiv Preface
We are grateful to our editor, Paul Crockett, for his enthusiastic support of this
project. The team at Wiley has been great. Many thanks go to Lilian Brady, Ken
Santor, Simon Durkin, Madelyn Lesure, Dawn Stanley, and Jeri Warner.
The computing systems and excellent technical support staff in the departments
of computer science at Brown University and University of California, Irvine gave
us reliable working environments. This manuscript was prepared primarily with
the LATEX typesetting package for the text and Adobe FrameMaker R and Microsoft
Visio R for the figures.
Finally, we would like to warmly thank Isabel Cruz, Karen Goodrich, 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