{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

JavaStructures - Java Structures Data Structures in Java...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Java Structures Data Structures in Java for the Principled Programmer The √ 7 Edition (Software release 33) Duane A. Bailey Williams College September 2007 This √ 7 text copyrighted 2005-2007 by All rights are reserved by The Author. No part of this draft publiciation may be reproduced or distributed in any form without prior, written consent of the author. Contents Preface to First Edition xi Preface to the Second Edition xiii Preface to the “Root 7” Edition xv 0 Introduction 0.1 Read Me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0.2 He Can’t Say That, Can He? . . . . . . . . . . . . . . . . . . . . . 1 1 2 1 The Object-Oriented Method 1.1 Data Abstraction and Encapsulation . . . . . 1.2 The Object Model . . . . . . . . . . . . . . . 1.3 Object-Oriented Terminology . . . . . . . . 1.4 A Special-Purpose Class: A Bank Account . . 1.5 A General-Purpose Class: An Association . . 1.6 Sketching an Example: A Word List . . . . . 1.7 Sketching an Example: A Rectangle Class . 1.8 Interfaces . . . . . . . . . . . . . . . . . . . 1.9 Who Is the User? . . . . . . . . . . . . . . . 1.10 Conclusions . . . . . . . . . . . . . . . . . . 1.11 Laboratory: The Day of the Week Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 6 7 8 11 14 18 20 22 24 25 29 2 Comments, Conditions, and Assertions 2.1 Pre- and Postconditions . . . . . . . . . 2.2 Assertions . . . . . . . . . . . . . . . . . 2.3 Craftsmanship . . . . . . . . . . . . . . . 2.4 Conclusions . . . . . . . . . . . . . . . . 2.5 Laboratory: Using Javadoc Commenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 34 34 36 37 39 3 Vectors 3.1 The Interface . . . . . . . . . . . 3.2 Example: The Word List Revisited 3.3 Example: Word Frequency . . . . 3.4 The Implementation . . . . . . . 3.5 Extensibility: A Feature . . . . . . 3.6 Example: L-Systems . . . . . . . 3.7 Example: Vector-Based Sets . . . 3.8 Example: The Matrix Class . . . . 3.9 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 45 47 48 50 53 56 57 60 64 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv Contents 3.10 Laboratory: The Silver Dollar Game . . . . . . . . . . . . . . . . . 67 4 Generics 4.1 Motivation (in case we need some) . . . 4.1.1 Possible Solution: Specialization 4.2 Implementing Generic Container Classes 4.2.1 Generic Associations . . . . . . 4.2.2 Parameterizing the Vector Class 4.2.3 Restricting Parameters . . . . . . 4.3 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 70 71 72 72 74 79 80 5 Design Fundamentals 5.1 Asymptotic Analysis Tools . . . . . . . . 5.1.1 Time and Space Complexity . . . 5.1.2 Examples . . . . . . . . . . . . . 5.1.3 The Trading of Time and Space . 5.1.4 Back-of-the-Envelope Estimations 5.2 Self-Reference . . . . . . . . . . . . . . . 5.2.1 Recursion . . . . . . . . . . . . . 5.2.2 Mathematical Induction . . . . . 5.3 Properties of Design . . . . . . . . . . . 5.3.1 Symmetry . . . . . . . . . . . . . 5.3.2 Friction . . . . . . . . . . . . . . 5.4 Conclusions . . . . . . . . . . . . . . . . 5.5 Laboratory: How Fast Is Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 81 82 85 91 92 94 94 101 108 108 110 110 115 6 Sorting 6.1 Approaching the Problem . . . . . . . 6.2 Selection Sort . . . . . . . . . . . . . . 6.3 Insertion Sort . . . . . . . . . . . . . . 6.4 Mergesort . . . . . . . . . . . . . . . . 6.5 Quicksort . . . . . . . . . . . . . . . . 6.6 Radix Sort . . . . . . . . . . . . . . . . 6.7 Sorting Objects . . . . . . . . . . . . . 6.8 Ordering Objects Using Comparators . 6.9 Vector-Based Sorting . . . . . . . . . . 6.10 Conclusions . . . . . . . . . . . . . . . 6.11 Laboratory: Sorting with Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 119 122 125 127 131 134 138 140 143 144 147 7 A Design Method 7.1 The Interface-Based Approach . . . . . . . . . . . . 7.1.1 Design of the Interface . . . . . . . . . . . . 7.1.2 Development of an Abstract Implementation 7.1.3 Implementation . . . . . . . . . . . . . . . . 7.2 Example: Development of Generators . . . . . . . . 7.3 Example: Playing Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 149 150 151 152 152 155 . . . . . . . . . . . Contents v 7.4 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 8 Iterators 8.1 Java’s Enumeration Interface . . . . . 8.2 The Iterator Interface . . . . . . . . . . 8.3 Example: Vector Iterators . . . . . . . 8.4 Example: Rethinking Generators . . . 8.5 Example: Filtering Iterators . . . . . . 8.6 Conclusions . . . . . . . . . . . . . . . 8.7 Laboratory: The Two-Towers Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 161 163 165 167 170 172 175 9 Lists 9.1 Example: A Unique Program . . . . . . . 9.2 Example: Free Lists . . . . . . . . . . . . 9.3 Partial Implementation: Abstract Lists . 9.4 Implementation: Singly Linked Lists . . 9.5 Implementation: Doubly Linked Lists . . 9.6 Implementation: Circularly Linked Lists 9.7 Implementation: Vectors . . . . . . . . . 9.8 List Iterators . . . . . . . . . . . . . . . . 9.9 Conclusions . . . . . . . . . . . . . . . . 9.10 Laboratory: Lists with Dummy Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 182 183 186 188 201 206 209 209 211 215 10 Linear Structures 10.1 Stacks . . . . . . . . . . . . . . . . . . 10.1.1 Example: Simulating Recursion 10.1.2 Vector-Based Stacks . . . . . . 10.1.3 List-Based Stacks . . . . . . . . 10.1.4 Comparisons . . . . . . . . . . 10.2 Queues . . . . . . . . . . . . . . . . . 10.2.1 Example: Solving a Coin Puzzle 10.2.2 List-Based Queues . . . . . . . 10.2.3 Vector-Based Queues . . . . . . 10.2.4 Array-Based Queues . . . . . . 10.3 Example: Solving Mazes . . . . . . . . 10.4 Conclusions . . . . . . . . . . . . . . . 10.5 Laboratory: A Stack-Based Language . 10.6 Laboratory: The Web Crawler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 221 222 225 227 228 229 231 234 235 238 242 244 247 251 11 Ordered Structures 11.1 Comparable Objects Revisited . . . . . . . . . 11.1.1 Example: Comparable Ratios . . . . . 11.1.2 Example: Comparable Associations . . 11.2 Keeping Structures Ordered . . . . . . . . . . 11.2.1 The OrderedStructure Interface . . . . 11.2.2 The Ordered Vector and Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 253 254 256 258 258 259 . . . . . . . . . . . . . . vi Contents 11.2.3 Example: Sorting Revisited . . . . 11.2.4 A Comparator-based Approach . . 11.2.5 The Ordered List . . . . . . . . . . 11.2.6 Example: The Modified Parking Lot 11.3 Conclusions . . . . . . . . . . . . . . . . . 11.4 Laboratory: Computing the “Best Of” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 265 267 270 272 275 12 Binary Trees 12.1 Terminology . . . . . . . . . . . . . . . . . 12.2 Example: Pedigree Charts . . . . . . . . . 12.3 Example: Expression Trees . . . . . . . . . 12.4 Implementation . . . . . . . . . . . . . . . 12.4.1 The BinaryTree Implementation . . 12.5 Example: An Expert System . . . . . . . . 12.6 Traversals of Binary Trees . . . . . . . . . 12.6.1 Preorder Traversal . . . . . . . . . 12.6.2 In-order Traversal . . . . . . . . . 12.6.3 Postorder Traversal . . . . . . . . . 12.6.4 Level-order Traversal . . . . . . . . 12.6.5 Recursion in Iterators . . . . . . . 12.7 Property-Based Methods . . . . . . . . . . 12.8 Example: Huffman Compression . . . . . 12.9 Example Implementation: Ahnentafel . . . 12.10 Conclusions . . . . . . . . . . . . . . . . . 12.11 Laboratory: Playing Gardner’s Hex-a-Pawn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 277 280 281 282 284 287 290 291 293 295 296 297 299 303 307 309 313 13 Priority Queues 13.1 The Interface . . . . . . . . . . . . . . . 13.2 Example: Improving the Huffman Code 13.3 A Vector-Based Implementation . . . . . 13.4 A Heap Implementation . . . . . . . . . 13.4.1 Vector-Based Heaps . . . . . . . 13.4.2 Example: Heapsort . . . . . . . . 13.4.3 Skew Heaps . . . . . . . . . . . . 13.5 Example: Circuit Simulation . . . . . . . 13.6 Conclusions . . . . . . . . . . . . . . . . 13.7 Laboratory: Simulating Business . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 315 317 318 319 320 326 329 333 337 341 14 Search Trees 14.1 Binary Search Trees . . . . . . . 14.2 Example: Tree Sort . . . . . . . 14.3 Example: Associative Structures 14.4 Implementation . . . . . . . . . 14.5 Splay Trees . . . . . . . . . . . 14.6 Splay Tree Implementation . . 14.7 An Alternative: Red-Black Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 343 345 345 348 354 357 361 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Contents vii 14.8 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 14.9 Laboratory: Improving the BinarySearchTree . . . . . . . . . . . . 367 15 Maps 15.1 Example Revisited: The Symbol Table . . . . . . 15.2 The Interface . . . . . . . . . . . . . . . . . . . 15.3 Simple Implementation: MapList . . . . . . . . 15.4 Constant Time Maps: Hash Tables . . . . . . . . 15.4.1 Open Addressing . . . . . . . . . . . . . 15.4.2 External Chaining . . . . . . . . . . . . 15.4.3 Generation of Hash Codes . . . . . . . . 15.4.4 Hash Codes for Collection Classes . . . . 15.4.5 Performance Analysis . . . . . . . . . . . 15.5 Ordered Maps and Tables . . . . . . . . . . . . 15.6 Example: Document Indexing . . . . . . . . . . 15.7 Conclusions . . . . . . . . . . . . . . . . . . . . 15.8 Laboratory: The Soundex Name Lookup System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 369 370 372 374 375 383 385 391 392 392 395 398 401 16 Graphs 16.1 Terminology . . . . . . . . . . . . . . . 16.2 The Graph Interface . . . . . . . . . . 16.3 Implementations . . . . . . . . . . . . 16.3.1 Abstract Classes Reemphasized 16.3.2 Adjacency Matrices . . . . . . . 16.3.3 Adjacency Lists . . . . . . . . . 16.4 Examples: Common Graph Algorithms 16.4.1 Reachability . . . . . . . . . . . 16.4.2 Topological Sorting . . . . . . . 16.4.3 Transitive Closure . . . . . . . 16.4.4 All Pairs Minimum Distance . . 16.4.5 Greedy Algorithms . . . . . . . 16.5 Conclusions . . . . . . . . . . . . . . . 16.6 Laboratory: Converting Between Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 403 404 408 408 410 416 422 422 424 427 428 429 434 439 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Answers 441 A.1 Solutions to Self Check Problems . . . . . . . . . . . . . . . . . . 441 A.2 Solutions to Odd-Numbered Problems . . . . . . . . . . . . . . . 451 B Beginning with Java B.1 A First Program . . . . . . . . . . . . . B.2 Declarations . . . . . . . . . . . . . . . B.2.1 Primitive Types . . . . . . . . . B.2.2 Reference Types . . . . . . . . B.3 Important Classes . . . . . . . . . . . . B.3.1 The structure.ReadStream Class B.3.2 The java.util.Scanner Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 489 491 491 493 494 494 495 viii Contents B.4 B.5 B.6 B.7 B.8 B.3.3 The PrintStream Class . . . . . B.3.4 Strings . . . . . . . . . . . . . . Control Constructs . . . . . . . . . . . B.4.1 Conditional Statements . . . . B.4.2 Loops . . . . . . . . . . . . . . Methods . . . . . . . . . . . . . . . . . Inheritance and Subtyping . . . . . . . B.6.1 Inheritance . . . . . . . . . . . B.6.2 Subtyping . . . . . . . . . . . . B.6.3 Interfaces and Abstract Classes Use of the Assert Command . . . . . . Use of the Keyword Protected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 497 498 498 499 502 502 502 503 504 506 507 C Collections 511 C.1 Collection Class Features . . . . . . . . . . . . . . . . . . . . . . . 511 C.2 Parallel Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 C.3 Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 D Documentation 513 D.1 Structure Package Hierarchy . . . . . . . . . . . . . . . . . . . . . 513 D.2 Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 Index 517 for Mary, my wife and best friend without the model of my mentors, the comments of my colleagues, the support of my students, the friendship of my family this book would never be thank you! Preface to the First Edition “I T ’ S A WONDERFUL TIME TO BE ALIVE .” At least that’s what I’ve found myself saying over the past couple of decades. When I first started working with computers, they were resources used by a privileged (or in my case, persistent) few. They were physically large, and logically small. They were cast from iron. The challenge was to make these behemoths solve complex problems quickly. Today, computers are everywhere. They are in the office and at home. They speak to us on telephones; they zap our food in the microwave. They make starting cars in New England a possibility. Everyone’s using them. What has aided their introduction into society is their diminished size and cost, and increased capability. The challenge is to make these behemoths solve complex problems quickly. Thus, while the computer and its applications have changed over time, the challenge remains the same: How can we get the best performance out of the current technology? The design and analysis of data structures lay the fundamental groundwork for a scientific understanding of what computers can do efficiently. The motivations for data structure design work accomplished three decades ago in assembly language at the keypunch are just as familiar to us today as we practice our craft in modern languages on computers on our laps. The focus of this material is the identification and development of relatively abstract principles for structuring data in ways that make programs efficient in terms of their consumption of resources, as well as efficient in terms of “programmability.” In the past, my students have encountered this material in Pascal, Modula-2, and, most recently, C++. None of these languages has been ideal, but each has been met with increasing expectation. This text uses The Java Programming Language1 —“Java”—to structure data. Java is a new and exciting language that has received considerable public attention. At the time of this writing, for example, Java is one of the few tools that can effectively use the Internet as a computing resource. That particular aspect of Java is not touched on greatly in this text. Still, Internet-driven applications in Java will need supporting data structures. This book attempts to provide a fresh and focused approach to the design and implementation of classic structures in a manner that meshes well with existing Java packages. It is hoped that learning this material in Java will improve the way working programmers craft programs, and the way future designers craft languages. Pedagogical Implications. This text was developed specifically for use with CS2 in a standard Computer Science curriculum. It is succinct in its approach, and requires, perhaps, a little more effort to read. I hope, though, that this text 1 Java is a trademark of Sun Microsystems, Incorporated. xii Preface to the First Edition SE SW E W NE NW N S List nim becomes not a brief encounter with object-oriented data structure design, but a touchstone for one’s programming future. The material presented in this text follows the syllabus I have used for several years at Williams. As students come to this course with experience using Java, the outline of the text may be followed directly. Where students are new to Java, a couple of weeks early in the semester will be necessary with a good companion text to introduce the student to new concepts, and an introductory Java language text or reference manual is recommended. For students that need a quick introduction to Java we provide a tutorial in Appendix B. While the text was designed as a whole, some may wish to eliminate less important topics and expand upon others. Students may wish to drop (or consider!) the section on induction (Section 5.2.2). The more no...
View Full Document

{[ snackBarMessage ]}