Part I: Objects and C++ |
|
|
Arrays, Pointers, and Structures |
|
|
3 | (38) |
|
What Are Pointers, Arrays, and Structures? |
|
|
3 | (1) |
|
|
4 | (11) |
|
First-Class Versus Second-Class Objects |
|
|
4 | (2) |
|
|
6 | (1) |
|
|
7 | (4) |
|
push_back: size and capacity |
|
|
11 | (1) |
|
Parameter-Passing Mechanisms |
|
|
11 | (2) |
|
Primitive Arrays of Constants |
|
|
13 | (1) |
|
|
14 | (1) |
|
The Standard Library string Type |
|
|
14 | (1) |
|
|
15 | (5) |
|
Dynamic Memory Management |
|
|
20 | (4) |
|
|
21 | (1) |
|
Garbage Collection and delete |
|
|
21 | (1) |
|
Stale Pointers, Double Deletion, and More |
|
|
22 | (2) |
|
|
24 | (2) |
|
|
26 | (15) |
|
|
28 | (1) |
|
Exogenous Versus Indigenous Data and Shallow Versus Deep Copying |
|
|
29 | (1) |
|
Noncontiguous Lists: Linked Lists |
|
|
30 | (2) |
|
|
32 | (1) |
|
|
32 | (2) |
|
|
34 | (1) |
|
|
35 | (1) |
|
|
35 | (3) |
|
|
38 | (3) |
|
|
41 | (56) |
|
What Is Object-Oriented Programming? |
|
|
41 | (2) |
|
|
43 | (14) |
|
|
43 | (2) |
|
Extra Constructor Syntax and Accessors |
|
|
45 | (3) |
|
Separation of Interface and Implementation |
|
|
48 | (3) |
|
The Big Three: Destructor, Copy Constructor, and operator= |
|
|
51 | (6) |
|
|
57 | (1) |
|
Additional C++ Class Features |
|
|
57 | (11) |
|
Initialization Versus Assignment in the Constructor Revisited |
|
|
61 | (2) |
|
|
63 | (1) |
|
|
64 | (3) |
|
Input and Output and Friends |
|
|
67 | (1) |
|
|
68 | (4) |
|
|
70 | (1) |
|
|
71 | (1) |
|
The enum Trick for Integer Class Constants |
|
|
71 | (1) |
|
|
72 | (1) |
|
|
73 | (9) |
|
Recap: What Gets Called and What Are the Defaults? |
|
|
82 | (2) |
|
|
84 | (13) |
|
|
85 | (1) |
|
|
85 | (2) |
|
|
87 | (2) |
|
|
89 | (1) |
|
|
90 | (6) |
|
|
96 | (1) |
|
|
97 | (22) |
|
|
97 | (1) |
|
|
98 | (2) |
|
A Sorting Function Template |
|
|
100 | (3) |
|
|
103 | (5) |
|
|
103 | (5) |
|
Implementing the vector Class Template |
|
|
108 | (1) |
|
Templates of Templates: A matrix Class |
|
|
108 | (4) |
|
The Data Members, Constructor, and Basic Accessors |
|
|
111 | (1) |
|
|
112 | (1) |
|
Destructor, Copy Assignment, and Copy Constructor |
|
|
112 | (1) |
|
|
112 | (2) |
|
Multiple Template Parameters |
|
|
112 | (1) |
|
Default Template Parameters |
|
|
113 | (1) |
|
The Reserved Word typename |
|
|
113 | (1) |
|
Bugs Associated with Templates |
|
|
114 | (5) |
|
Bad Error Messages and Inconsistent Rules |
|
|
114 | (1) |
|
Template-Matching Algorithms |
|
|
114 | (1) |
|
Nested Classes in a Template |
|
|
114 | (1) |
|
Static Members in Class Templates |
|
|
115 | (1) |
|
|
115 | (1) |
|
|
115 | (1) |
|
|
115 | (1) |
|
|
116 | (1) |
|
|
117 | (2) |
|
|
119 | (36) |
|
|
119 | (4) |
|
|
123 | (13) |
|
|
124 | (1) |
|
The Constructor and Base Class Initialization |
|
|
125 | (1) |
|
|
126 | (2) |
|
|
128 | (1) |
|
Static and Dynamic Binding |
|
|
129 | (2) |
|
The Default Constructor, Copy Constructor, Copy Assignment Operator, and Destructor |
|
|
131 | (1) |
|
Constructors and Destructors: Virtual or Not Virtual? |
|
|
132 | (1) |
|
Abstract Methods and Classes |
|
|
133 | (3) |
|
Example: Expanding the Shape Class |
|
|
136 | (6) |
|
|
142 | (5) |
|
Static Binding of Parameters |
|
|
142 | (1) |
|
|
143 | (1) |
|
Derived Class Methods Hide Base Class Methods |
|
|
144 | (1) |
|
Compatible Return Types for Overridden Methods |
|
|
145 | (1) |
|
|
146 | (1) |
|
|
146 | (1) |
|
Call by Value and Polymorphism Do Not Mix |
|
|
146 | (1) |
|
|
147 | (8) |
|
|
149 | (1) |
|
|
149 | (1) |
|
|
150 | (2) |
|
|
152 | (1) |
|
|
152 | (2) |
|
|
154 | (1) |
|
|
155 | (38) |
|
|
155 | (1) |
|
The Functor (Function Objects) |
|
|
156 | (6) |
|
|
162 | (8) |
|
|
162 | (6) |
|
A Constant Reference Wrapper |
|
|
168 | (1) |
|
Adapters: Changing an Interface |
|
|
169 | (1) |
|
|
170 | (9) |
|
|
171 | (3) |
|
|
174 | (1) |
|
Inheritance-Based Iterators and Factories |
|
|
174 | (5) |
|
|
179 | (1) |
|
|
179 | (14) |
|
|
184 | (1) |
|
|
184 | (1) |
|
|
185 | (1) |
|
|
186 | (1) |
|
|
186 | (4) |
|
|
190 | (3) |
Part II: Algorithms and Building Blocks |
|
|
|
193 | (38) |
|
What Is Algorithm Analysis? |
|
|
193 | (5) |
|
Examples of Algorithm Running Times |
|
|
198 | (1) |
|
The Maximum Contiguous Subsequence Sum Problem |
|
|
199 | (7) |
|
The Obvious O(N3) Algorithm |
|
|
200 | (3) |
|
An Improved O(N2) Algorithm |
|
|
203 | (1) |
|
|
204 | (2) |
|
|
206 | (5) |
|
|
211 | (3) |
|
|
214 | (5) |
|
|
214 | (1) |
|
|
215 | (2) |
|
|
217 | (2) |
|
Checking an Algorithm Analysis |
|
|
219 | (1) |
|
Limitations of Big-Oh Analysis |
|
|
220 | (11) |
|
|
221 | (1) |
|
|
221 | (1) |
|
|
222 | (1) |
|
|
223 | (1) |
|
|
223 | (5) |
|
|
228 | (3) |
|
The Standard Template Library |
|
|
231 | (34) |
|
|
232 | (1) |
|
|
233 | (4) |
|
|
233 | (2) |
|
Stacks and Computer Languages |
|
|
235 | (1) |
|
|
236 | (1) |
|
|
237 | (3) |
|
|
238 | (1) |
|
|
238 | (2) |
|
|
240 | (5) |
|
|
240 | (3) |
|
|
243 | (1) |
|
|
244 | (1) |
|
Implementation of vector with an Iterator |
|
|
245 | (2) |
|
Sequences and Linked Lists |
|
|
247 | (2) |
|
|
247 | (2) |
|
|
249 | (1) |
|
|
249 | (2) |
|
|
251 | (2) |
|
|
253 | (12) |
|
|
257 | (1) |
|
|
257 | (2) |
|
|
259 | (1) |
|
|
259 | (1) |
|
|
260 | (4) |
|
|
264 | (1) |
|
|
265 | (56) |
|
|
265 | (2) |
|
Background: Proofs by Mathematical Induction |
|
|
267 | (2) |
|
|
269 | (15) |
|
Printing Numbers in Any Base |
|
|
271 | (3) |
|
|
274 | (1) |
|
|
275 | (1) |
|
Too Much Recursion Can Be Dangerous |
|
|
276 | (2) |
|
|
278 | (1) |
|
|
279 | (5) |
|
|
284 | (8) |
|
|
285 | (1) |
|
|
285 | (2) |
|
Greatest Common Divisor and Multiplicative Inverses |
|
|
287 | (2) |
|
|
289 | (3) |
|
Divide-and-Conquer Algorithms |
|
|
292 | (11) |
|
The Maximum Contiguous Subsequence Sum Problem |
|
|
293 | (4) |
|
Analysis of a Basic Divide-and-Conquer Recurrence |
|
|
297 | (4) |
|
A General Upper Bound for Divide-and-Conquer Running Times |
|
|
301 | (2) |
|
|
303 | (5) |
|
|
308 | (13) |
|
|
310 | (2) |
|
|
312 | (1) |
|
|
313 | (1) |
|
|
314 | (1) |
|
|
314 | (5) |
|
|
319 | (2) |
|
|
321 | (44) |
|
Why Is Sorting Important? |
|
|
322 | (1) |
|
|
323 | (1) |
|
Analysis of the Insertion Sort and Other Simple Sorts |
|
|
324 | (2) |
|
|
326 | (4) |
|
|
328 | (2) |
|
|
330 | (4) |
|
Linear-Time Merging of Sorted Arrays |
|
|
330 | (2) |
|
|
332 | (2) |
|
|
334 | (14) |
|
|
335 | (2) |
|
|
337 | (3) |
|
|
340 | (2) |
|
|
342 | (2) |
|
|
344 | (1) |
|
Median-of-Three Partitioning |
|
|
345 | (1) |
|
|
346 | (1) |
|
|
346 | (2) |
|
|
348 | (1) |
|
A Lower Bound for Sorting |
|
|
349 | (3) |
|
|
352 | (13) |
|
Using Pionters to Reduce Comparable Copies to 2N |
|
|
352 | (1) |
|
|
353 | (2) |
|
|
355 | (1) |
|
|
356 | (1) |
|
|
357 | (1) |
|
|
357 | (1) |
|
|
358 | (5) |
|
|
363 | (2) |
|
|
365 | (24) |
|
Why Do We Need Random Numbers? |
|
|
365 | (1) |
|
|
366 | (5) |
|
Nonuniform Random Numbers |
|
|
371 | (2) |
|
Generating a Random Permutation |
|
|
373 | (2) |
|
|
375 | (3) |
|
Randomized Primality Testing |
|
|
378 | (11) |
|
|
380 | (2) |
|
|
382 | (1) |
|
|
383 | (1) |
|
|
383 | (1) |
|
|
383 | (3) |
|
|
386 | (3) |
Part III: Applications |
|
|
|
389 | (20) |
|
|
389 | (6) |
|
|
390 | (1) |
|
|
391 | (4) |
|
|
395 | (14) |
|
|
397 | (1) |
|
|
398 | (6) |
|
|
404 | (1) |
|
|
405 | (1) |
|
|
405 | (1) |
|
|
406 | (1) |
|
|
406 | (1) |
|
|
406 | (2) |
|
|
408 | (1) |
|
|
409 | (30) |
|
|
409 | (11) |
|
|
409 | (2) |
|
|
411 | (9) |
|
|
420 | (19) |
|
|
421 | (1) |
|
Infix to Postfix Conversion |
|
|
422 | (2) |
|
|
424 | (8) |
|
|
432 | (3) |
|
|
435 | (1) |
|
|
435 | (1) |
|
|
436 | (1) |
|
|
436 | (1) |
|
|
436 | (2) |
|
|
438 | (1) |
|
|
439 | (32) |
|
|
439 | (22) |
|
|
440 | (2) |
|
|
442 | (3) |
|
|
445 | (16) |
|
A Cross-Reference Generator |
|
|
461 | (10) |
|
|
461 | (1) |
|
|
462 | (4) |
|
|
466 | (1) |
|
|
466 | (1) |
|
|
466 | (1) |
|
|
467 | (1) |
|
|
467 | (3) |
|
|
470 | (1) |
|
|
471 | (18) |
|
|
471 | (4) |
|
|
473 | (1) |
|
A More Efficient Algorithm |
|
|
473 | (2) |
|
|
475 | (14) |
|
|
477 | (1) |
|
Example: A Modern Bank Simulation |
|
|
478 | (8) |
|
|
486 | (1) |
|
|
486 | (1) |
|
|
486 | (1) |
|
|
486 | (1) |
|
|
486 | (3) |
|
|
489 | (48) |
|
|
489 | (14) |
|
|
491 | (12) |
|
Unweighted Shortest-Path Problem |
|
|
503 | (6) |
|
|
504 | (5) |
|
|
509 | (1) |
|
Positive-Weighted, Shortest-Path Problem |
|
|
509 | (5) |
|
Theory: Dijkstra's Algorithm |
|
|
509 | (4) |
|
|
513 | (1) |
|
Negative-Weighted, Shortest-Path Problem |
|
|
514 | (3) |
|
|
514 | (3) |
|
|
517 | (1) |
|
Path Problems in Acyclic Graphs |
|
|
517 | (20) |
|
|
517 | (3) |
|
Theory of the Acyclic Shortest-Path Algorithm |
|
|
520 | (2) |
|
|
522 | (1) |
|
An Application: Critical-Path Analysis |
|
|
522 | (4) |
|
|
526 | (1) |
|
|
527 | (1) |
|
|
528 | (1) |
|
|
529 | (1) |
|
|
529 | (4) |
|
|
533 | (4) |
Part IV: Implementations |
|
|
|
537 | (28) |
|
Dynamic Array Implementations |
|
|
537 | (11) |
|
|
538 | (3) |
|
|
541 | (7) |
|
Linked List Implementations |
|
|
548 | (9) |
|
|
548 | (5) |
|
|
553 | (4) |
|
Comparison of the Two Methods |
|
|
557 | (1) |
|
The STL Stack and Queue Adapters |
|
|
558 | (1) |
|
|
558 | (7) |
|
|
559 | (2) |
|
|
561 | (1) |
|
|
561 | (1) |
|
|
561 | (1) |
|
|
562 | (3) |
|
|
565 | (40) |
|
|
565 | (5) |
|
|
567 | (2) |
|
|
569 | (1) |
|
|
570 | (9) |
|
Doubly Linked Lists and Circularly Linked Lists |
|
|
579 | (3) |
|
|
582 | (1) |
|
Implementing the STL list Class |
|
|
582 | (23) |
|
|
597 | (1) |
|
|
597 | (1) |
|
|
598 | (1) |
|
|
598 | (1) |
|
|
599 | (6) |
|
|
605 | (36) |
|
|
605 | (6) |
|
|
605 | (2) |
|
|
607 | (1) |
|
An Application: File Systems |
|
|
608 | (3) |
|
|
611 | (8) |
|
|
619 | (3) |
|
Tree Traversal: Iterator Classes |
|
|
622 | (19) |
|
|
624 | (6) |
|
|
630 | (1) |
|
|
630 | (1) |
|
|
630 | (3) |
|
|
633 | (3) |
|
|
636 | (1) |
|
|
637 | (1) |
|
|
637 | (1) |
|
|
637 | (4) |
|
|
641 | (84) |
|
|
641 | (11) |
|
|
642 | (2) |
|
|
644 | (8) |
|
|
652 | (5) |
|
|
653 | (4) |
|
Analysis of Binary Search Tree Operations |
|
|
657 | (4) |
|
|
661 | (9) |
|
|
662 | (2) |
|
|
664 | (3) |
|
|
667 | (3) |
|
|
670 | (1) |
|
|
670 | (15) |
|
|
672 | (2) |
|
|
674 | (2) |
|
|
676 | (4) |
|
|
680 | (5) |
|
|
685 | (8) |
|
|
686 | (2) |
|
|
688 | (2) |
|
|
690 | (3) |
|
Implementing the STL set and map Classes |
|
|
693 | (14) |
|
|
707 | (18) |
|
|
714 | (1) |
|
|
715 | (2) |
|
|
717 | (1) |
|
|
717 | (1) |
|
|
718 | (3) |
|
|
721 | (4) |
|
|
725 | (30) |
|
|
725 | (2) |
|
|
727 | (2) |
|
|
729 | (6) |
|
Naive Analysis of Linear Probing |
|
|
731 | (1) |
|
What Really Happens: Primary Clustering |
|
|
732 | (1) |
|
Analysis of the find Operation |
|
|
733 | (2) |
|
|
735 | (11) |
|
|
739 | (6) |
|
Analysis of Quadratic Probing |
|
|
745 | (1) |
|
Separate Chaining Hashing |
|
|
746 | (1) |
|
Hash Tables Versus Binary Search Trees |
|
|
746 | (1) |
|
|
747 | (8) |
|
|
747 | (1) |
|
|
748 | (1) |
|
|
749 | (1) |
|
|
749 | (1) |
|
|
749 | (3) |
|
|
752 | (3) |
|
A Priority Queue: The Binary Heap |
|
|
755 | (40) |
|
|
755 | (6) |
|
|
756 | (2) |
|
|
758 | (1) |
|
|
759 | (2) |
|
Implementation of the Basic Operations |
|
|
761 | (5) |
|
|
762 | (1) |
|
|
763 | (3) |
|
The buildHeap Operation: Linear-Time Heap Construction |
|
|
766 | (5) |
|
STL priority_queue Implementation |
|
|
771 | (2) |
|
Advanced Operations; decreaseKey and merge |
|
|
773 | (1) |
|
Internal Sorting: Heapsort |
|
|
773 | (5) |
|
|
778 | (17) |
|
Why We Need New Algorithms |
|
|
778 | (1) |
|
Model for External Sorting |
|
|
778 | (1) |
|
|
779 | (2) |
|
|
781 | (1) |
|
|
782 | (1) |
|
|
783 | (2) |
|
|
785 | (1) |
|
|
785 | (1) |
|
|
786 | (1) |
|
|
787 | (1) |
|
|
787 | (4) |
|
|
791 | (4) |
Part V: Advanced Data Structures |
|
|
|
795 | (28) |
|
Self-Adjustement and Amortized Analysis |
|
|
795 | (4) |
|
|
797 | (1) |
|
A Simple Self-Adjusting Strategy (That Does Not Work) |
|
|
797 | (2) |
|
The Basic Bottom-Up Splay Tree |
|
|
799 | (3) |
|
Basic Splay Tree Operations |
|
|
802 | (1) |
|
Analysis of Bottom-Up Splaying |
|
|
803 | (6) |
|
Proof of the Splayin Bound |
|
|
806 | (3) |
|
|
809 | (3) |
|
Implementation of Top-Down Splay Trees |
|
|
812 | (6) |
|
Compaison of the Splay Tree with Other Search Trees |
|
|
818 | (5) |
|
|
819 | (1) |
|
|
819 | (1) |
|
|
820 | (1) |
|
|
820 | (1) |
|
|
820 | (2) |
|
|
822 | (1) |
|
|
823 | (22) |
|
|
823 | (5) |
|
|
823 | (1) |
|
Simplistic Merginal of Heap-Ordered Trees |
|
|
824 | (1) |
|
The Skew Heap: A Simple Modification |
|
|
825 | (1) |
|
Analysis of the Skew Heap |
|
|
826 | (2) |
|
|
828 | (17) |
|
|
829 | (1) |
|
Implementation of the Pairing Heap |
|
|
830 | (6) |
|
Application: Dijkstra's Shortest Weighted Path Algorithm |
|
|
836 | (4) |
|
|
840 | (1) |
|
|
840 | (1) |
|
|
841 | (1) |
|
|
841 | (1) |
|
|
841 | (1) |
|
|
842 | (3) |
|
|
845 | (32) |
|
|
845 | (1) |
|
Dynamic Equivalence and Two Applications |
|
|
846 | (11) |
|
Application: Generating Mazes |
|
|
847 | (3) |
|
Application: Minimum Spanning Trees |
|
|
850 | (3) |
|
Application: The Nearest Common Ancestor Problem |
|
|
853 | (4) |
|
|
857 | (1) |
|
The Quick-Union Algorithm |
|
|
858 | (5) |
|
|
860 | (2) |
|
|
862 | (1) |
|
|
863 | (2) |
|
Worst Case for Union-by-Rank and Path Compression |
|
|
865 | (12) |
|
Analysis of the Union/Find Algorithm |
|
|
866 | (7) |
|
|
873 | (1) |
|
|
873 | (2) |
|
|
875 | (1) |
|
|
875 | (1) |
|
|
875 | (2) |
|
|
877 | |
Appendices |
|
|
Appendix A Miscellaneous C++ Details |
|
|
A-3 | |
|
A.1 None of the Compilers Implement and Standard |
|
|
A-3 | |
|
|
A-4 | |
|
A.2.1 Autoincrement and Autodecrement Operators |
|
|
A-4 | |
|
|
A-5 | |
|
|
A-6 | |
|
A.2.4 The Conditional Operator |
|
|
A-8 | |
|
A.3 Command-Line Arguments |
|
|
A-8 | |
|
|
A-9 | |
|
A.4.1 Basic Stream Operations |
|
|
A-9 | |
|
|
A-13 | |
|
|
A-13 | |
|
|
A-15 | |
|
|
A-17 | |
|
|
A-17 | |
|
|
A-21 | |
|
Appendix C Some Library Routines |
|
|
A-23 | |
|
C.1 Routines Declared in <ctype.h> and <cctype> |
|
|
A-23 | |
|
C.2 Costants Declared in <limits.h> and <climits> |
|
|
A-24 | |
|
C.3 Routines Declared in <math.h> and <cmath> |
|
|
A-25 | |
|
C.4 Routines Declared in <stdlib.h> and <cstdlib> |
|
|
A-26 | |
|
Appendix D Primitive Arrays in C++ |
|
|
A-27 | |
|
|
A-27 | |
|
D.1.1 The C++ Implementation: An Array Name Is a Pointer |
|
|
28 | |
|
D.1.2 Multidimensional Arrays |
|
|
A-31 | |
|
D.1.3 The char * Type, const Pointers, and Constant Strings |
|
|
A-31 | |
|
D.2 Dynamic Allocation of Arrays: new [ ] and delete [ ] |
|
|
A-35 | |
|
D.3 Pointer Arithmetic, Pointer Hopping, and Primitive Iteration |
|
|
A-41 | |
|
D.3.1 Implications of the Precedence of *, &, and [ ] |
|
|
A-41 | |
|
D.3.2 What Pointer Arithmetic Means |
|
|
A-42 | |
|
D.3.3 A Pointer-Hopping Example |
|
|
A-44 | |
|
D.3.4 Is Pointer Hopping Worthwhile? |
|
|
A-45 | |
|
|
A-47 | |
|
|
A-47 | |
Index |
|
I-1 | |