Java Software Structures Designing and Using Data Structures 3rd Edition John Lewis
Java Software Structures Designing and Using Data Structures 3rd Edition John Lewis
https://ebookgate.com
https://ebookgate.com/product/java-software-
structures-designing-and-using-data-
structures-3rd-edition-john-lewis/
https://ebookgate.com/product/java-foundations-introduction-to-
program-design-and-data-structures-2nd-edition-john-lewis/
ebookgate.com
https://ebookgate.com/product/data-structures-other-objects-using-
java-4th-edition-michael-mann/
ebookgate.com
https://ebookgate.com/product/data-structures-problem-solving-using-
java-4th-edition-mark-allen-weiss/
ebookgate.com
https://ebookgate.com/product/java-structures-data-structures-in-java-
for-the-principled-programmer-2nd-edition-duane-bailey/
ebookgate.com
Java Software Solutions Global Edition John Lewis
https://ebookgate.com/product/java-software-solutions-global-edition-
john-lewis/
ebookgate.com
https://ebookgate.com/product/schaum-s-outline-sof-data-structures-
with-java-second-edition-john-hubbard/
ebookgate.com
https://ebookgate.com/product/data-structures-and-abstractions-with-
java-2nd-ed-edition-carrano/
ebookgate.com
https://ebookgate.com/product/data-structures-and-algorithms-
using-c-1st-edition-michael-mcmillan/
ebookgate.com
https://ebookgate.com/product/data-structures-outside-in-with-
java-1st-edition-sesh-venugopal/
ebookgate.com
java ™
Third Edition
S O F T WA R E S T R U C T U R E S
Designing and Using Data Structures
This page intentionally left blank
java ™
S O F T WA R E S T R U C T U R E S
Third Edition
JOHN LEWIS
V i r g i n i a Te c h
JOSEPH CHASE
Radford University
Addison-Wesley
New York Boston San Francisco
London Toronto Sydney Tokyo Singapore Madrid
Mexico City Munich Paris Cape Town Hong Kong Montreal
Editor-in-Chief Michael Hirsch
Editorial Assistant Stephanie Sellinger
Managing Editor Jeffrey Holcomb
Production Supervisor Heather McNally
Marketing Manager Erin Davis
Marketing Coordinator Kathryn Ferranti
Senior Manufacturing Buyer Carol Melville
Online Product Manager Bethany Tidd
Art Director Linda Knowles
Cover Design Elena Sidorova
Project Management, Composition, and Illustrations Nesbitt Graphics, Inc.
Project Coordinator, Nesbitt Graphics, Inc. Harry Druding
Project Manager, Nesbitt Graphics, Inc. Kathy Smith
Text Design, Nesbitt Graphics, Inc. Jerilyn Bockorick, Alisha Webber
Cover Image Steve Cole/Getty Images
Access the latest information about Addison-Wesley Computer Science titles from our World Wide Web site:
http://www.pearsonhighered.com/cs.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those
designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed in
initial caps or all caps.
The programs and applications presented in this book have been included for their instructional value. They have been tested
with care, but are not guaranteed for any particular purpose. The publisher does not offer any warranties or representations,
nor does it accept any liabilities with respect to the programs or applications.
Copyright © 2010 Pearson Education, 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, or otherwise,
without the prior written permission of the publisher. Printed in the United States of America.
For information on obtaining permission for use of material in this work, please submit a written request to Pearson
Education, Inc., Rights and Contracts Department, 501 Boylston Street, Suite 900, Boston, MA 02116, fax your request to
(617)671-3447, or e-mail at http://www.pearsoned.com/legal/permissions.htm.
Addison Wesley
is an imprint of
ISBN-13: 978-013-607858-6
ISBN-10: 0-13-607858-3
www.pearsonhighered.com 1 2 3 4 5 6 7 8 10–EB–13 12 11 10 09
To my wife Sharon and my kids:
Justin, Kayla, Nathan, and Samantha
–J. L.
our way to introduce collections in an abstract way that separated it from the core
data structures, using examples such as a bag or set collection. This new approach
capitalizes on the fact that a stack is conceptually about as straightforward as it
gets. Using it as a first example enhances the understanding of collections as a
whole.
The previous edition of the book had several chapters that focused on larger
case studies that made use of collections to solve non-trivial problems. While
many instructors found these useful, they also seemed to interrupt the flow of cov-
erage of core topics. Therefore we have taken the case study chapters out of the
book and put them on the web as supplementary resources. We encourage all
instructors to download and use these resources as they see fit.
Finally, for this edition we’ve reviewed and improved the discussions through-
out the book. We’ve expanded the discussion of graphs and reversed the order of
the graphs and hashing chapters to make a cleaner flow. And we’ve added a chap-
ter that specifically covers sets and maps.
We think these modifications build upon the strong pedagogy established by
previous editions and give instructors more opportunity and flexibility to cover
topics as they choose.
Our Approach
Books of this type vary greatly in their overall approach. Our approach is founded
on a few important principles that we fervently embraced. First, we present the
various collections explored in the book in a consistent manner. Second, we
emphasize the importance of sound software design techniques. Third, we organ-
ized the book to support and reinforce the big picture: the study of data structures
and algorithms. Let’s examine these principles further.
Consistent Presentation
When exploring a particular type of collection, we carefully address each of the
following issues in order:
1. Concept: We discuss the collection conceptually, establishing the services it
provides (its interface).
2. Use: We explore examples that illustrate how the particular nature of the
collection, no matter how it’s implemented, can be useful when solving
problems.
3. Implementation: We explore various implementation options for the
collection.
4. Analysis: We compare and contrast the implementations.
P REFA C E ix
Clean Organization
The contents of the book have been carefully organized to minimize distracting
tangents and to reinforce the overall purpose of the book. The organization sup-
ports the book in its role as a pedagogical exploration of data structures and algo-
rithms as well as its role as a valuable reference.
The book can be divided into numerous parts: Part I consists of the first two
chapters and provides an introduction to the concept of a collection and analysis
of algorithms. Part II includes the next four chapters, which cover introductory
and underlying issues that affect all aspects of data structures and algorithms as
well as linear collections (stacks, queues, and lists). Part III covers the concepts of
recursion, sorting, and searching. Part IV covers the nonlinear collections (trees,
heaps, hashing, and graphs). Each type of collection, with the exception of trees,
is covered in its own chapter. Trees are covered in a series of chapters that explore
their various aspects and purposes.
Chapter Breakdown
Chapter 1 (Introduction) discusses various aspects of software quality and pro-
vides an overview of software development issues. It is designed to establish the
x PREFA C E
appropriate mindset before embarking on the details of data structure and algo-
rithm design.
Chapter 2 (Analysis of Algorithms) lays the foundation for determining the
efficiency of an algorithm and explains the important criteria that allow a devel-
oper to compare one algorithm to another in proper ways. Our emphasis in this
chapter is understanding the important concepts more than getting mired in heavy
math or formality.
Chapter 3 (Collections) establishes the concept of a collection, stressing the
need to separate the interface from the implementation. It also conceptually intro-
duces a stack, then explores an array-based implementation of a stack.
Chapter 4 (Linked Structures) discusses the use of references to create linked
data structures. It explores the basic issues regarding the management of linked
lists, and then defines an alternative implementation of a stack (introduced in
Chapter 3) using an underlying linked data structure.
Chapter 5 (Queues) explores the concept and implementation of a first-in, first-
out queue. Radix sort is discussed as an example of using queues effectively. The
implementation options covered include an underlying linked list as well as both
fixed and circular arrays.
Chapter 6 (Lists) covers three types of lists: ordered, unordered, and indexed.
These three types of lists are compared and contrasted, with discussion of the
operations that they share and those that are unique to each type. Inheritance is
used appropriately in the design of the various types of lists, which are imple-
mented using both array-based and linked representations.
Chapter 7 (Recursion) is a general introduction to the concept of recursion and
how recursive solutions can be elegant. It explores the implementation details of
recursion and discusses the basic idea of analyzing recursive algorithms.
Chapter 8 (Sorting and Searching) discusses the linear and binary search algo-
rithms, as well as the algorithms for several sorts: selection sort, insertion sort,
bubble sort, quick sort, and merge sort. Programming issues related to searching
and sorting, such as using the Comparable interface as the basis of comparing
objects, are stressed in this chapter. Searching and sorting that are based in par-
ticular data structures (such as heap sort) are covered in the appropriate chapter
later in the book.
Chapter 9 (Trees) provides an overview of trees, establishing key terminology
and concepts. It discusses various implementation approaches and uses a binary
tree to represent and evaluate an arithmetic expression.
Chapter 10 (Binary Search Trees) builds off of the basic concepts established in
Chapter 9 to define a classic binary search tree. A linked implementation of a
binary search tree is examined, followed by a discussion of how the balance in the
P REFA C E xi
tree nodes is key to its performance. That leads to exploring AVL and red/black
implementations of binary search trees.
Chapter 11 (Priority Queues and Heaps) explores the concept, use, and imple-
mentations of heaps and specifically their relationship to priority queues. A heap
sort is used as an example of its usefulness as well. Both linked and array-based
implementations are explored.
Chapter 12 (Multi-way Search Trees) is a natural extension of the discussion of
the previous chapters. The concepts of 2-3 trees, 2-4 trees, and general B-trees are
examined and implementation options are discussed.
Chapter 13 (Graphs) explores the concept of undirected and directed graphs
and establishes important terminology. It examines several common graph algo-
rithms and discusses implementation options, including adjacency matrices.
Chapter 14 (Hashing) covers the concept of hashing and related issues, such as
hash functions and collisions. Various Java Collections API options for hashing
are discussed.
Chapter 15 (Sets and Maps) explores these two types of collections and their
importance to the Java Collections API.
Appendix A (UML) provides an introduction to the Unified Modeling
Language as a reference. UML is the de facto standard notation for representing
object-oriented systems.
Appendix B (Object-Oriented Design) is a reference for anyone needing a
review of fundamental object-oriented concepts and how they are accomplished
in Java. Included are the concepts of abstraction, classes, encapsulation, inheri-
tance, and polymorphism, as well as many related Java language constructs such
as interfaces.
Supplements
The following supplements are available to all readers of this book at www.aw
.com/cssupport.
■ Source Code for all programs presented in the book
■ Full case studies of programs that illustrate concepts from the text, includ-
ing a Black Jack Game, a Calculator, a Family Tree Program, and a Web
Crawler
The following instructor supplements are only available to qualified in-
structors at Pearson Education’s Instructor Resource Center, http://www
.pearsonhighered.com/irc. Please visit the Web site, contact your local Pearson
Education Sales Representative, or send an e-mail to [email protected],
for information about how to access them.
xii PREFA C E
Acknowledgements
First and most importantly we want to thank our students for whom this book is
written and without whom it never could have been. Your feedback helps us
become better educators and writers. Please continue to keep us on our toes.
We would like to thank all of the reviewers listed below who took the time to
share their insight on the content and presentation of the material in this book and
its previous editions. Your input was invaluable.
Mary P. Boelk, Marquette University
Robert Burton, Brigham Young University
Gerald Cohen, St. Joseph’s College
Robert Cohen, University of Massachusetts–Boston
Jack Davis, Radford University
Bob Holloway, University of Wisconsin–Madison
Nisar Hundewale, Georgia State University
Chung Lee, California State Polytechnic University
Mark C. Lewis, Trinity University
Mark J. Llewellyn, University of Central Florida
Ronald Marsh, University of North Dakota
Eli C. Minkoff, Bates College; University of Maine–Augusta
Ned Okie, Radford University
Manuel A. Perez-Quinones, Virginia Tech
Moshe Rosenfeld University of Washington
Salam Salloum, California State Polytechnic University–Pomona
Don Slater, Carnegie Mellon University
Ashish Soni, University of Southern California
Carola Wenk, University of Texas–San Antonio
that our book makes it into your hands in time to use it in class. Thank you all
very much for all your hard work and dedication to this book.
We’d be remiss if we didn’t acknowledge the wonderful contributions of the
ACM Special Interest Group on Computer Science Education. Its publications and
conferences are crucial to anyone who takes the pedagogy of computing seriously.
If you’re not part of this group, you’re missing out.
Finally, we want to thank our families, who support and encourage us in what-
ever projects we find ourselves diving into. Ultimately, you are the reason we do
what we do.
This page intentionally left blank
Contents
Preface
Chapter 1 Introduction 1
1.1 Software Quality 2
Correctness 3
Reliability 3
Robustness 4
Usability 4
Maintainability 5
Reusability 5
Portability 6
Efficiency 6
Quality Issues 6
Chapter 3 Collections 27
3.1 Introduction to Collections 28
Abstract Data Types 29
The Java Collections API 31
xv
xvi CONTENTS
3.6 Exceptions 51
Exception Messages 52
The try Statement 53
Exception Propagation 54
Chapter 5 Queues 99
5.1 A Queue ADT 100
“Sir, I was duly favoured with your Letter of the 20th. of August,
inclosing answers to my questions respecting your plan for
employing the female poor of this borough, which, with the
sketch of the proposed building, &c. transmitted to Mr. Dixon, I
laid before the Court of Guardians at their meeting, holden on
Thursday last; who after having given the matter their most
mature consideration, instruct me to inform you, that they
entirely decline acceding to your propositions. I therefore
return you the several papers, requesting you will be pleased to
accept the thanks of the Court for the trouble you have had in
the business. I am, &c. J. Smeatham.”
Page 638, last word of the contents of section III, for ineffetual r.
ineffectual.—p. 638, l. 8, for fovourite r. favourite—p. 735, l. 9, after
them a comma.—p. 773, l. 2, after conscience a semicolon.—p. 782,
last line but one, after absentees, a comma. —p. 794, l. 4, for
rejected, r. ejected—p. 800, l. 5, after stand, r. by.—p. 803, l. 21,
after rubbish, a comma—p. 821, l. 9, for Guyborn, r. Guybon.—same
page, l. 19, after even r. the.—p. 835, l. 21, for Assesiation r.
Association—p. 847, Note, l. 19, for proceeding r. preceding.—p. 959,
last line but one, for egregiously r. eggregiously—p. 1027, note, l.
24, for pastime r. patience.—p. 1087, l. 14, for exegencies r.
exigences.—p. 1106 l. 1, comma after here—p. 1127, note, l. 5, for
townsnen r. townsmen.—p. 1140, last l. for 1570 r. 1590—p. 1142,
note, l. 8, after could r. not. —p. 1147, note, l. 2, after much r. to
his.
Whittingham, Printer, Lynn.
FOOTNOTES.
These lines were quoted before imperfectly. They are now given
correctly in the original orthography.
[684b] We allude principally to the poor-rates and paving-tax, which
are certainly most severely felt, and likely to be still more so. The
former by frugal and wise management might, doubtless, be greatly
reduced, without any material detriment to the poor: and the latter
ought never to have existed till the times proved more favourable.
When the project was brought forward it was firmly opposed by a
large body of the householders: but it was carried against them,
very wrongfully. They were told that the work would be completed
at the expence of about 13,000l. and it has already far exceeded, if
not doubled, that sum: yet the work is not finished. Thousands
have been lavished, merely to suit the convenience, or gratify the
caprice of a few opulent families, without being of the least use or
benefit to the town at large; which must have been exceedingly
disingenuous and dishonourable. So great was the liberality which
the paviers experienced at Lynn, that they are reported to be
satisfied to do their work at Norwich and Yarmouth, 25 per cent.
under what they had here.—In short, the managers, or rather the
mis-managers of this concern, went on lavishly and blunderingly, till
they could go no further. They were aground for sometime this last
autumn, and had probably remained so over the winter, and the
summer too, and thrown the whole town into the utmost confusion,
but for the timely assistance of a certain individual, who on this
critical occasion stood in the place and acted the part of that good
man noticed in holy writ, who by his wisdom delivered the city. Eccl.
ix. 15.
[687] See Martin’s History of Thetford chap. xiii. p. 170.
[688] The Surrenders seem to have been all much of the same cast
and tenor, and so were probably the confessions which accompanied
them, a copy of one of these is given by Burnet, and is as follows—