CarrTOCv1 - Contents Preface Introduction Chapter 1 Chapter...

Info iconThis preview shows page 1. Sign up to view the full content.

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: Contents Preface Introduction Chapter 1 Chapter 2 Chapter 3 Java Classes 1 Objects and Classes 2 Using the Methods in a Java Class 3 References and Aliases 5 Arguments and Parameters 6 Defining a Java Class 6 Method Definitions 7 Passing Arguments 10 A Definition of the Class Name 12 Constructors 14 The Method toString 16 Static Fields and Methods 16 Packages 18 The Java Class Library 18 Creating Classes from Other Classes 25 Composition 26 Adapters 28 Inheritance 29 Invoking Constructors from Within Constructors 32 Private Fields and Methods of the Base Class 33 Overriding and Overloading Methods 34 Protected Access 37 Multiple Inheritance 38 Type Compatibility and Base Classes 38 The Class Object 40 Abstract Classes and Methods 41 Polymorphism 42 Designing Classes 53 Encapsulation 54 Specifying Methods 56 Java Interfaces 59 Writing an Interface 59 Implementing an Interface 61 An Interface as a Data Type 62 Type Casts Within an Interface Implementation 62 Extending an Interface 63 Named Constants Within an Interface 64 Interfaces Versus Abstract Classes 65 1 2 Contents Choosing Classes 66 Identifying Classes 67 CRC Cards 68 Reusing Classes 70 Chapter 4 Lists 75 Specifications for the ADT List 76 Refining the Specifications 80 Using the ADT List 84 Java Class Library: The Interface List 87 Using a List Is Like Using a Vending Machine 88 Chapter 5 List Implementations That Use Arrays 93 Using a Fixed-Size Array to Implement the ADT List 94 An Analogy 94 The Java Implementation 96 Using Dynamic Array Expansion to Implement the ADT List 102 Expanding an Array 102 A New Implementation of a List 104 Using a Vector to Implement the ADT List 105 A Summary of Methods in the Class Vector 108 The Pros and Cons of Using an Array to Implement the ADT List 109 Java Class Library 109 The Class ArrayList 109 The Interface Serializable 110 Chapter 6 List Implementations That Link Data 113 Linked Data 114 Forming a Chain 115 Forming Another Chain 117 Forming Yet Another Chain 119 The Class Node 125 A Linked Implementation of the ADT List 126 Adding to the End of the List 127 Adding at a Given Position Within the List 130 The Private Method getNodeAt 132 The Method remove 133 The Method replace 136 The Method getEntry 136 The Method contains 137 The Remaining Methods 137 Using a Class Node That Has Set and Get Methods 138 Tail References 138 A Revised Implementation of the List 139 The Pros and Cons of Using a Chain to Implement the ADT List 143 Java Class Library: The Class LinkedList 143 Chapter 7 Iterators 147 What Is an Iterator? 148 A Basic Iterator 149 Iterator Methods That Modify the ADT 151 Contents Implementing an Internal Iterator 153 Implementing an Iterator as Its Own Class 158 An External Iterator 160 An Inner Class Iterator 162 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Java’s Iterator Interfaces 169 The Interface Iterator 170 Implementing the Interface Iterator 173 A Linked Implementation 173 An Array-Based Implementation 175 The Interface ListIterator 178 Using the Interface ListIterator 181 An Array-Based Implementation of the Interface ListIterator 184 The Inner Class 184 Java Class Library: ArrayList and LinkedList Revisited 190 The Efficiency of Algorithms 195 Motivation 196 Measuring an Algorithm’s Efficiency 198 Big Oh Notation 201 Formalities 204 Picturing Efficiency 206 The Efficiency of Implementations of the ADT List 210 The Array-Based Implementation 210 The Linked Implementation 211 Comparing the Implementations 212 Recursion 219 What Is Recursion? 220 Tracing a Recursive Method 224 Recursive Methods That Return a Value 227 Recursively Processing an Array 230 Recursively Processing a Linked Chain 232 The Time Efficiency of Recursive Methods 233 The Time Efficiency of countDown 234 The Time Efficiency of Computing xn 235 A Simple Solution to a Difficult Problem 236 A Poor Solution to a Simple Problem 241 Tail Recursion 243 Mutual Recursion 245 An Introduction to Sorting 251 Selection Sort 252 Iterative Selection Sort 254 Recursive Selection Sort 256 The Efficiency of Selection Sort 256 Insertion Sort 257 Iterative Insertion Sort 258 Recursive Insertion Sort 260 The Efficiency of Insertion Sort 262 Insertion Sort of a Chain of Linked Nodes 262 3 4 Contents Shell Sort 265 The Java Code 267 The Efficiency of Shell Sort 268 Comparing the Algorithms 268 Chapter 12 Faster Sorting Methods 273 Merge Sort 274 Merging Arrays 274 Recursive Merge Sort 275 The Efficiency of Merge Sort 277 Iterative Merge Sort 278 Merge Sort in the Java Class Library 279 Quick Sort 279 The Efficiency of Quick Sort 280 Creating the Partition 280 Java Code for Quick Sort 283 Quick Sort in the Java Class Library 286 Radix Sort 286 Pseudocode for Radix Sort 288 The Efficiency of Radix Sort 288 Comparing the Algorithms 289 Chapter 13 Sorted Lists 295 Specifications for the ADT Sorted List 296 Using the ADT Sorted List 299 A Linked Implementation 300 The Method add 300 The Efficiency of the Linked Implementation 307 An Implementation That Uses the ADT List 308 Efficiency Issues 311 Chapter 14 Inheritance and Lists 315 Using Inheritance to Implement a Sorted List 316 Designing a Base Class 318 An Efficient Implementation of a Sorted List 322 The Method add 322 Chapter 15 Mutable, Immutable, and Cloneable Objects 327 Mutable and Immutable Objects 328 Companion Classes 331 Using Inheritance to Form Companion Classes 332 Cloneable Objects 335 A Sorted List of Clones 340 Cloning an Array 343 Cloning a Chain 345 Chapter 16 Searching 351 The Problem 352 Searching an Unsorted Array 353 An Iterative Sequential Search of an Unsorted Array 353 A Recursive Sequential Search of an Unsorted Array 353 Contents The Efficiency of a Sequential Search of an Array 356 Searching a Sorted Array 356 A Sequential Search of a Sorted Array 357 A Binary Search of a Sorted Array 357 Java Class Library: The Method binarySearch 362 The Efficiency of a Binary Search of an Array 362 Searching an Unsorted Chain 363 An Iterative Sequential Search of an Unsorted Chain 363 A Recursive Sequential Search of an Unsorted Chain 364 The Efficiency of a Sequential Search of a Chain 365 Searching a Sorted Chain 365 A Sequential Search of a Sorted Chain 365 A Binary Search of a Sorted Chain 366 Choosing a Search Method 367 Chapter 17 Chapter 18 Chapter 19 Dictionaries 371 Specifications for the ADT Dictionary 372 A Java Interface 375 Iterators 376 Using the ADT Dictionary 377 A Directory of Telephone Numbers 378 The Frequency of Words 381 A Concordance of Words 383 Java Class Library: The Interface Map 385 Dictionary Implementations 389 Array-Based Implementations 390 The Entries 390 An Unsorted Array-Based Dictionary 391 A Sorted Array-Based Dictionary 393 Vector-Based Implementations 396 Linked Implementations 398 The Entries 398 An Unsorted Linked Dictionary 399 A Sorted Linked Dictionary 400 Hashing as a Dictionary Implementation 407 What Is Hashing? 408 Hash Functions 411 Computing Hash Codes 411 Compressing a Hash Code into an Index for the Hash Table 414 Resolving Collisions 415 Open Addressing with Linear Probing 415 Open Addressing with Quadratic Probing 420 Open Addressing with Double Hashing 421 A Potential Problem with Open Addressing 422 Separate Chaining 423 Efficiency 426 The Load Factor 426 The Cost of Open Addressing 427 5 6 Contents The Cost of Separate Chaining 428 Rehashing 430 Comparing Schemes for Collision Resolution 430 A Dictionary Implementation That Uses Hashing 431 Entries in the Hash Table 431 Data Fields and Constructors 432 The Methods getValue, remove, and add 433 Iterators 439 Java Class Library: The Class HashMap 440 Chapter 20 Stacks 445 Specifications of the ADT Stack 446 Using a Stack to Process Algebraic Expressions 449 Checking for Balanced Parentheses, Brackets, and Braces in an Infix Algebraic Expression 450 Transforming an Infix Expression to a Postfix Expression 455 Evaluating Postfix Expressions 462 Evaluating Infix Expressions 464 The Program Stack 466 Recursive Methods 467 Using a Stack Instead of Recursion 468 An Iterative Binary Search 469 Java Class Library: The Class Stack 470 Chapter 21 Stack Implementations 475 A Linked Implementation 475 An Array-Based Implementation 478 A Vector-Based Implementation 482 Chapter 22 Queues, Deques, and Priority Queues 487 Specifications of the ADT Queue 488 Using a Queue to Simulate a Waiting Line 492 The Classes WaitLine and Customer 492 Using a Queue to Compute the Capital Gain in a Sale of Stock 498 The Classes StockLedger and StockPurchase 498 Specifications of the ADT Deque 500 Using a Deque to Compute the Capital Gain in a Sale of Stock 502 Specifications of the ADT Priority Queue 503 Using a Priority Queue to Compute the Capital Gain in a Sale of Stock 504 Chapter 23 Queue, Deque, and Priority Queue Implementations 509 A Linked Implementation of a Queue 510 An Array-Based Implementation of a Queue 513 A Circular Array 514 A Circular Array with One Unused Location 516 A Vector-Based Implementation of a Queue 521 Circular Linked Implementations of a Queue 523 A Two-Part Circular Linked Chain 523 A Doubly Linked Implementation of a Deque 530 Possible Implementations of a Priority Queue 533 Contents Chapter 24 Chapter 25 Chapter 26 Trees 537 Tree Concepts 538 Hierarchical Organizations 538 Tree Terminology 539 Traversals of a Tree 544 Traversals of a Binary Tree 544 Traversals of a General Tree 547 Java Interfaces for Trees 548 Interfaces for All Trees 548 An Interface for Binary Trees 548 Examples of Binary Trees 550 Expression Trees 550 Decision Trees 552 Binary Search Trees 555 Heaps 558 Examples of General Trees 560 Parse Trees 560 Game Trees 561 Tree Implementations 567 The Nodes in a Binary Tree 568 An Interface for a Node 568 An Implementation of BinaryNode 569 An Implementation of the ADT Binary Tree 570 Creating a Basic Binary Tree 571 The Method privateSetTree 572 Accessor and Mutator Methods 575 Computing the Height and Counting Nodes 575 Traversals 576 An Implementation of an Expression Tree 581 General Trees 583 A Node for a General Tree 583 Using a Binary Tree to Represent a General Tree 583 A Binary Search Tree Implementation 589 Getting Started 590 An Interface for the Binary Search Tree 591 Duplicate Entries 593 Beginning the Class Definition 594 Searching and Retrieving 595 Traversing 596 Adding an Entry 596 An Iterative Implementation 597 A Recursive Implementation 599 Removing an Entry 604 Removing an Entry Whose Node Is a Leaf 604 Removing an Entry Whose Node Has One Child 604 Removing an Entry Whose Node Has Two Children 606 Removing an Entry in the Root 609 7 8 Contents An Iterative Implementation 609 A Recursive Implementation 613 The Efficiency of Operations 616 The Importance of Balance 617 The Order in Which Nodes Are Added 618 An Implementation of the ADT Dictionary 619 Chapter 27 A Heap Implementation 625 Reprise: The ADT Heap 626 Using an Array to Represent a Heap 626 Adding an Entry 628 Removing the Root 632 Creating a Heap 635 Heapsort 637 Chapter 28 Balanced Search Trees 1 AVL Trees 2 Single Rotations 3 Double Rotations 3 Implementation Details 6 2-3 Trees 9 Searching a 2-3 Tree 9 Adding Entries to a 2-3 Tree 10 Splitting Nodes During Addition 11 2-4 Trees 12 Adding Entries to a 2-4 Tree 13 Comparing AVL, 2-3, and 2-4 Trees 14 Red-Black Trees 15 Properties of a Red-Black Tree 16 Adding Entries to a Red-Black Tree 17 Java Class Library: The Class TreeMap 19 B-Trees 19 Chapter 29 Graphs 1 Some Examples and Terminology 2 Road Maps 2 Airline Routes 5 Mazes 6 Course Prerequisites 7 Trees 8 Traversals 9 Breadth-First Traversal 9 Depth-First Traversal 11 Topological Order 13 Paths 17 Finding a Path 17 The Shortest Path in an Unweighted Graph 17 The Shortest Path in a Weighted Graph 20 Java Interfaces for the ADT Graph 23 Contents Chapter 30 Graph Implementations 1 An Overview of Two Implementations 2 The Adjacency Matrix 2 The Adjacency List 3 Vertices and Edges 3 Specifying the Class Vertex 4 The Class Edge 6 Implementing the Class Vertex 7 An Implementation of the ADT Graph 9 Basic Operations 9 Graph Algorithms 12 Appendix A Java Essentials Appendix B Exception Handling Appendix C File Input and Output Appendix D Documentation and Programming Style Appendix E Answers to Self-Test Questions Index 9 ...
View Full Document

This note was uploaded on 04/29/2010 for the course CS 5503 taught by Professor Kaylor during the Spring '10 term at W. Alabama.

Ask a homework question - tutors are online