Data structures and algorithms in Java 2nd Edition Adam Drozdek download
Data structures and algorithms in Java 2nd Edition Adam Drozdek download
https://ebookfinal.com/download/data-structures-and-algorithms-in-
java-2nd-edition-adam-drozdek/
https://ebookfinal.com/download/data-structures-in-java-a-laboratory-
course-1st-edition-sandra-andersen/
https://ebookfinal.com/download/data-structures-and-algorithm-
analysis-in-java-3rd-edition-edition-mark-a-weiss/
https://ebookfinal.com/download/data-structures-and-algorithm-
analysis-in-java-3rd-edition-dr-clifford-a-shaffer/
https://ebookfinal.com/download/problem-solving-in-data-structures-
algorithms-using-python-programming-interview-guide-1st-edition-
hemant-jain/
Fundamentals of Data Structures in C 2nd Edition Ellis
Horowitz
https://ebookfinal.com/download/fundamentals-of-data-structures-
in-c-2nd-edition-ellis-horowitz/
https://ebookfinal.com/download/data-structures-and-algorithm-
analysis-in-c-2nd-edition-china-reprint-edition-weiss/
SECOND EDITION
Adam Drozdek
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Data Structures
and Algorithms
in Java
SECOND EDITION
Adam Drozdek
SECOND EDITION
Adam Drozdek
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Data Structures and Algorithms in Java, Second Edition
by Adam Drozdek
1 2 3 4 5 6 7 8 9 BM 06 05 04 03 02
ALL RIGHTS RESERVED. No part of this work covered by the copyright hereon
may be reproduced or used in any form or by any means—graphic, electronic,
or mechanical, including photocopying, recording, taping, Web distribution, or
information storage and retrieval systems—without the written permission of
the publisher.
Disclaimer
Course Technology reserves the right to revise this publication and make changes
from time to time in its content without notice.
ISBN 0-534-49252-5
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
TO MY WIFE , BOGNA
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Contents
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
vi ■ Contents
2 C OMPLEXITY A NALYSIS 56
2.1 Computational and Asymptotic Complexity 56
2.2 Big-O Notation 57
2.3 Properties of Big-O Notation 59
2.4 Ω and Q Notations 61
2.5 Possible Problems 62
2.6 Examples of Complexities 62
2.7 Finding Asymptotic Complexity: Examples 64
2.8 The Best, Average, and Worst Cases 66
2.9 Amortized Complexity 69
2.10 NP-Completeness 73
2.11 Exercises 76
Bibliography 79
3 L INKED L ISTS 80
3.1 Singly Linked Lists 80
3.1.1 Insertion 86
3.1.2 Deletion 88
3.1.3 Search 93
3.2 Doubly Linked Lists 95
3.3 Circular Lists 99
3.4 Skip Lists 101
3.5 Self-Organizing Lists 107
3.6 Sparse Tables 111
3.7 Lists in java.util 114
3.7.1 LinkedList 114
3.7.2 ArrayList 120
3.8 Concluding Remarks 123
3.9 Case Study: A Library 124
3.10 Exercises 134
3.11 Programming Assignments 136
Bibliography 139
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Contents ■ vii
5 R ECURSION 169
5.1 Recursive Definitions 169
5.2 Method Calls and Recursion Implementation 172
5.3 Anatomy of a Recursive Call 174
5.4 Tail Recursion 178
5.5 Nontail Recursion 179
5.6 Indirect Recursion 185
5.7 Nested Recursion 187
5.8 Excessive Recursion 188
5.9 Backtracking 191
5.10 Concluding Remarks 198
5.11 Case Study: A Recursive Descent Interpreter 199
5.12 Exercises 207
5.13 Programming Assignments 210
Bibliography 212
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
viii ■ Contents
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Contents ■ ix
8 G RAPHS 376
8.1 Graph Representation 377
8.2 Graph Traversals 379
8.3 Shortest Paths 383
8.3.1 All-to-All Shortest Path Problem 390
8.4 Cycle Detection 392
8.4.1 Union-Find Problem 393
8.5 Spanning Trees 395
8.6 Connectivity 399
8.6.1 Connectivity in Undirected Graphs 399
8.6.2 Connectivity in Directed Graphs 402
8.7 Topological Sort 405
8.8 Networks 407
8.8.1 Maximum Flows 407
8.8.2 Maximum Flows of Minimum Cost 417
8.9 Matching 421
8.9.1 Stable Matching Problem 426
8.9.2 Assignment Problem 428
8.9.3 Matching in Nonbipartite Graphs 430
8.10 Eulerian and Hamiltonian Graphs 432
8.10.1 Eulerian Graphs 432
8.10.2 Hamiltonian Graphs 436
8.11 Graph Coloring 442
8.12 NP-Complete Problems in Graph Theory 445
8.12.1 The Clique Problem 445
8.12.2 The 3-Colorability Problem 446
8.12.3 The Vertex Cover Problem 448
8.12.4 The Hamiltonian Cycle Problem 449
8.13 Case Study: Distinct Representatives 450
8.14 Exercises 460
8.15 Programming Assignments 464
Bibliography 466
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
x ■ Contents
9 S ORTING 469
9.1 Elementary Sorting Algorithms 470
9.1.1 Insertion Sort 470
9.1.2 Selection Sort 474
9.1.3 Bubble Sort 475
9.2 Decision Trees 477
9.3 Efficient Sorting Algorithms 481
9.3.1 Shell Sort 481
9.3.2 Heap Sort 484
9.3.3 Quicksort 488
9.3.4 Mergesort 494
9.3.5 Radix Sort 497
9.4 Sorting in java.util 502
9.5 Concluding Remarks 505
9.6 Case Study: Adding Polynomials 507
9.7 Exercises 515
9.8 Programming Assignments 516
Bibliography 517
10 H ASHING 519
10.1 Hash Functions 520
10.1.1 Division 520
10.1.2 Folding 520
10.1.3 Mid-Square Function 521
10.1.4 Extraction 521
10.1.5 Radix Transformation 522
10.2 Collision Resolution 522
10.2.1 Open Addressing 522
10.2.2 Chaining 528
10.2.3 Bucket Addressing 530
10.3 Deletion 531
10.4 Perfect Hash Functions 532
10.4.1 Cichelli’s Method 533
10.4.2 The FHCD Algorithm 536
10.5 Hash Functions for Extendible Files 538
10.5.1 Extendible Hashing 539
10.5.2 Linear Hashing 541
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Contents ■ xi
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xii ■ Contents
A PPENDIXES
A Computing Big-O 718
A.1 Harmonic Series 718
A.2 Approximation of the Function lg(n!) 718
A.3 Big-O for Average Case of Quicksort 720
A.4 Average Path Length in a Random Binary Tree 722
A.5 The Number of Nodes in an AVL Tree 723
B NP-Completeness 724
B.1 Cook’s Theorem 724
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Preface
xiii
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xiv ■ Preface
Brief examples of Java code are included throughout the book to illustrate the
practical importance of data structures. However, theoretical analysis is equally im-
portant. Thus, presentations of algorithms are integrated with analyses of efficiency.
Great care is taken in the presentation of recursion because even advanced students
have problems with it. Experience has shown that recursion can be explained best if the
run-time stack is taken into consideration. Changes to the stack are shown when tracing
a recursive function not only in the chapter on recursion, but also in other chapters. For
example, a surprisingly short method for tree traversal may remain a mystery if work
done by the system on the run-time stack is not included in the explanation. Standing
aloof from the system and retaining only a purely theoretical perspective when dis-
cussing data structures and algorithms are not necessarily helpful. This book also in-
cludes comprehensive chapters on data compression and memory management.
The thrust of this book is data structures, and other topics are treated here only as
much as necessary to ensure a proper understanding of this subject. Algorithms are
discussed from the perspective of data structures, so the reader will not find a com-
prehensive discussion of different kinds of algorithms and all the facets that a full
presentation of algorithms requires. However, as mentioned, recursion is covered in
depth. In addition, complexity analysis of algorithms is presented in some detail.
Chapters 1 and 3–8 present a number of different data structures and the algo-
rithms that operate on them. The efficiency of each algorithm is analyzed, and improve-
ments to the algorithm are suggested.
■ Chapter 1 presents the basic principles of object-oriented programming, an intro-
duction to dynamic memory allocation and the use of pointers, and a rudimentary
introduction to Java.
■ Chapter 2 describes some methods used to assess the efficiency of algorithms.
■ Chapter 3 contains an introduction to linked lists.
■ Chapter 4 presents stacks and queues and their applications.
■ Chapter 5 contains a detailed discussion of recursion. Different types of recursion are
discussed, and a recursive call is dissected.
■ Chapter 6 discusses binary trees, including implementation, traversal, and search.
This chapter also includes balanced trees.
■ Chapter 7 details more generalized trees such as tries, 2– 4 trees, and B-trees.
■ Chapter 8 presents graphs.
Chapters 9–12 show different applications of data structures introduced in the
previous chapters. They emphasize the data structure aspects of each topic under
consideration.
■ Chapter 9 analyzes sorting in detail, and several elementary and nonelementary
methods are presented.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Preface ■ xv
■ Chapter 10 discusses hashing, one of the most important areas in searching. Various
techniques are presented with an emphasis on the utilization of data structures.
■ Chapter 11 discusses data compression algorithms and data structures.
■ Chapter 12 presents various techniques and data structures for memory
management.
■ Chapter 13 discusses many algorithms for exact and approximate string matching.
■ Appendix A discusses in greater detail big-O notation, introduced in Chapter 2.
■ Appendix B gives a proof of Cook’s theorem and illustrates it with an extended
example.
Each chapter contains a discussion of the material illustrated with appropriate
diagrams and tables. Except for Chapter 2, all chapters include a case study, which is
an extended example using the features discussed in that chapter. All case studies have
been tested using the Visual C++ compiler on a PC and the g++ compiler under
UNIX except the von Koch snowflake, which runs on a PC under Visual C++. At the
end of each chapter is a set of exercises of varying degrees of difficulty. Except for
Chapter 2, all chapters also include programming assignments and an up-to-date bib-
liography of relevant literature.
Chapters 1–6 (excluding Sections 2.9, 3.4, 6.4.3, 6.7, and 6.8) contain the core
material that forms the basis of any data structures course. These chapters should be
studied in sequence. The remaining six chapters can be read in any order. A one-
semester course could include Chapters 1–6, 9, and Sections 10.1 and 10.2. The entire
book could also be part of a two-semester sequence.
TEACHING TOOLS
Electronic Instructor’s Manual. The Instructor’s Manual that accompanies this text-
book includes complete solutions to all text exercises.
Electronic Figure Files. All images from the text are available in bitmap format for use
in classroom presentations.
Source Code. The source code for the text example programs is available via the au-
thor’s Web site at http://www.mathes.dug.edu/drozdek/DSinJava.
It is also available for student download at course.com. All teaching tools, outlined
above, are available in the Instructor’s Resources section of course.com.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xvi ■ Preface
ACKNOWLEDGMENTS
I would like to thank the following reviewers, whose comments and advice helped me
to improve this book:
James Ball, Indiana State University
Robin Dawes, Queen’s University
Julius Dichter, University of Bridgeport
However, the ultimate content is my responsibility, and I would appreciate
hearing from readers about any shortcomings or strengths. My email address is
[email protected].
Adam Drozdek
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Object-Oriented
Programming
Using Java
1
T
his chapter introduces the reader to elementary Java. Java is an immense lan-
guage and programming environment, and it is impossible to touch upon all
Java-related issues within the confines of one chapter. This chapter introduces
only those aspects of Java that are necessary for understanding the Java code offered in
this book. The reader familiar with Java can skip this chapter.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
2 ■ Chapter 1 Object-Oriented Programming Using Java
give a compilation error, “incompatible type for =. Explicit cast is needed to convert
int to byte.” The addition b + c gives an integer value that must be cast to execute
the assignment to the byte variable a. To avoid the problem, the assignment should
be changed to
a = (byte) (b + c);
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Random documents with unrelated
content Scribd suggests to you:
these characters are so valuable in a native plum that Wyant may
well be used to breed from. The trees from which the description
here given was made came from C. L. Watrous, Des Moines, Iowa,
and to the best of our belief are true to name.
This variety, according to a letter from J. E. Wyant, Shellsburg,
Iowa, was found by J. B. Wyant of Janesville, Iowa, while hunting for
wild plums in 1866 on the Cedar River near his home. The following
year he transplanted the tree to his yard. About 1874, J. E. Wyant
told R. Royce of Shellsburg, Iowa, proprietor of the Benton County
Nursery, of this tree. Royce secured cuttings from the original tree
and began propagating the plum. Fruits were sent to Professor J. L.
Budd, Ames, Iowa, who named it Wyant. The variety was introduced
by Mr. Royce and was disseminated by him and by Professor Budd.
In 1897 it was added to the fruit catalog list of the American
Pomological Society.
YELLOW EGG
YELLOW EGG
Prunus domestica
1. Rea Flora 209. 1676. 2. Ray Hist. Plant. 2:1528, 1529. 1688. 3.
Langley Pomona 95, Pl. XXV fig. VI. 1729. 4. Miller Gard. Dict. 3. 1754. 5.
Duhamel Trait. Arb. Fr. 2:107, Pl. XX fig. 10. 1768. 6. Knoop Fructologie
2:59. 1771. 7. Kraft Pom. Aust. 2:29, Tab. 175 fig. 1; 38, Tab. 188 fig. 1.
1796. 8. Forsyth Treat. Fr. Trees 20, 21. 1803. 9. Coxe Cult. Fr. Trees 233,
fig. 8. 1817. 10. Lond. Hort. Soc. Cat. 149. 1831. 11. Prince Pom. Man.
2:57, 58. 1832. 12. Kenrick Am. Orch. 258, 269. 1832. 13. Floy-Lindley
Guide Orch. Gard. 299, 301. 1846. 14. Poiteau Pom. Franc. 1: fig. 1846.
15. Thomas Am. Fruit Cult. 333. 1849. 16. Elliott Fr. Book 424. 1854. 17.
Thompson Gard. Ass’t 520. 1859. 18. Am. Pom. Soc. Cat. 88. 1862. 19.
Downing Fr. Trees Am. 954 fig. 1869. 20. Pom. France 7: No. 18. 1871.
21. Koch Deut. Obst. 560. 1876. 22. Hogg Fruit Man. 730. 1884. 23.
Mathieu Nom. Pom. 431. 1889. 24. Fell Cat. 49. 1893. 25. Guide Prat.
163, 354. 1895. 26. Cornell Sta. Bul. 131:193. 1897.
Aechte Gelbe Eierpflaume 25. Albert’s Damascene 23, 25. Albertus
Damen Pflaume 20, 23, 25. Askew’s Golden 20. Askew’s Golden Egg 16,
19, 22, 23, 25. Aubertiana 21. Bonum Magnum 11, 20, 22, 23, 25. Bonum
Magnum 1, 2. Col. Young’s Seedling 16. Dame Aubert 10, 11, 13, 17, 19,
22, 23, 25. Dame Ambert 16. Dame Aubert 5, 12. Damas Aubert 7, 23, 25.
Dame Aubert Blanche 10, 17, 19, 20, 22, 23, 25. Dame Ambert Blanche
16. Dame Aubert Grosse Luisante 11. Dame Ambert Jaune 16. Dame
Aubert Jaune 10, 11, 17, 19, 20, 23. Darwin Peach 24. Die Albertus
Damenpflaume 7. De Besançon 25. De Monsieur 25 incor. Die Grosse
Weisse Glanzende 7. Die Kaiserliche Weisse Pflaume ?7. D’OEuf 25. D’OEuf
Blanche 25. Dutch Plum 1, 11. Dutch Plumb 3. Echte Gelbe Eier Pflaume
23. Edle Gelbe Eger Pflaume 20. Edle Gelbe Eier Pflaume 23, 25. Egg
Plum 4, 8, 9, 10, 11, 12, 13, 16, 17, 19, 20, 22, 23, 25. Eier Pflaume 23,
25. Gelbe Eger Pflaume 20, 23, 25. Gelbe Egg 20, 23, 25. Gelbe Malonke
23, 25. Gelbe Ungarische Eier Pflaume 23, 25. Gelbe Marunke 23, 25.
Gelbe Eierpflaume 23. Gelbe Eierpflaume 20, 25. Grosse Weisse
Glanzende 20, 23, 25. Great Mogul ?2. Grosse Datte ?5. Grosse-Luisante
5, 7, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25. Gros Luisante 12. Gelbe
Ungarische Eyerpflaume 20. Grosse Prune Blanche 20, 23, 25. Grosse
Maronke 25. Grosse Marouk 20, 23, 25. Grosse Glanzende Alberts Pflaume
23, 25. Grosse Glanzende 20, 25. Grosse Glanzende Pflaume 23. Grosse
Gelbe Eier Pflaume 23, 25. Grosse Marunke 23. Grünliche Dattel Pflaume
von Besançon 23, 25. Hick’s large Egg? 11. Impériale Blanche ?5.
Impériale Blanche ?7, 10, 11, 12, 13, 17, 19, 20, 22, ?23, 25. Imperial
Blanc 11. Large Yellow Egg 11. Magnum Bonum 19. Monsieur’s Plum ?4, ?
8. Monsieur’s Plum ?4. Mogul 9. Monsieur 11. Mogul 4, 8, 11, 23. Mogul
Plum 25. Mogule Plumb 3. Mogule 11. Mogol Plum 20, 23, 25. Mogols
Pflaume 20, 23, 25. Mogul’s Pflaume 23, 25. Prune de Monsieur? 4, 6.
Prune de Monsieur 23 incor. Prune OEuf 20. Prune Dame Aubert 14, 20.
Prune d’Oeuf 20, 23. Prune d’Oeuf blanche 6, 23. Prune d’Inde Blanc 19.
Prune De Besançon 20, 23. Prune Dame d’Aubert 21. Prune d’Inde
Blanche 23. Supreme ?14. Wentworth 13. Wentworth ?8, 10, 11, 12, 16,
17, 19, 23, 25. Wentworth Plumb ?3. White Imperial 9, 11, 15, 16, 19, 23.
White Imperial 11. White Imperial Bonum Magnum 4, 8. White Holland 3,
4, 8, 10, 11, 12, 16, 17, 19, 23, 25. White Magnum Bonum 9, 11, 13, 15,
16, 19, 20, 23, 25. Weisse Kaiser Pflaume 23, 25. Weisse Hollandische
Pflaume 23, 25. White Bonum Magnum 3, 11, 20, 23, 25. White Egg Plum
11. White Magnum Bonum 10, 12, 17, 18, 22, 23. White Mogul 10, 12, 13,
16, 19, 20, 22, 23, 25. White Egg 15. White Egg 16, 19, 23, 25. Weisser
Kaiser 23, 25. Weisse Magnum Bonum 20, 23, 25. Weisse Kaiserin 23
incor. Yellow Magnum Bonum 10, 15, 16, 17, 18, 19, 22, 23, 25. Yellow
Bonum Magnum 20, 23, 25. Young’s Superior Egg ?11. Yellow Egg 18, 23,
25.
YELLOW GAGE
Prunus domestica
1. Prince Treat. Hort. 25. 1828. 2. Prince Pom. Man. 2:108. 1832. 3.
Downing Fr. Trees Am. 287, 288 fig. 115. 1845. 4. Thomas Am. Fruit Cult.
329. 1849. 5. Cole Am. Fr. Book 208 fig. 1849. 6. Horticulturist 7:403.
1852. 7. Am. Pom. Soc. Rpt. 36, 55. 1852. 8. Elliott Fr. Book 414. 1854.
9. Am. Pom. Soc. Rpt. 210. 1856. 10. Bridgeman Gard. Ass’t 3:126. 1857.
11. U. S. Pat. Off. Rpt. 190, Pl. XIII. 1865. 12. Mas Pom. Gen. 2:163, fig.
82. 1873. 13. Barry Fr. Garden 417. 1883. 14. Mathieu Nom. Pom. 443.
1889. 15. Waugh Plum Cult. 126. 1901.
American Wheat 10. American Yellow Gage of some 3, 4, 8, 11, 14.
American Yellow Gage 10. Auserlesene Gelhe Reine-Claude 14. Harvest
Gage 6, 8, 11, 14. Prince’s Gage 1. Prince’s Gelbe Reine-Claude 14.
Prince’s Yellow Gage 2, 3, 4, 6, 7, 8, 9, 11. Prince’s Yellow Gage 5, 12, 13,
14, 15. Reine-Claude Jaune De Prince 12. White Gage of some 3, 8, 11,
14.
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.
ebookfinal.com