Saumyendra Sengupta Carl Phillip Korobkin C++ Object-Oriented Data Structures With 165 Illustrations Diskette Included Taehnische Hochschule Darmstadt FACHBERE5CH IKFORMATIK BJJL Inventar-Nr.: Sachgebiete: Standort: O T H E K Springer- Verlag New York Berlin Heidelberg London Paris Tokyo Hong Kong Barcelona Budapest
Preface vii 1 Concepts of Function-Oriented and Object-Oriented Data Structures 1 1.1 Data Types, Data Objects, and Related Terminologies... 1 1.2 Definition of Abstract Data Structures 2 1.3 Object-Oriented Design and the ADT 4 1.3.1 Function-Oriented Data Structures 5 1.3.2 Object-Oriented Data Structures 6 1.3.3 A Unified Approach 8 1.3.4 Steps for Deriving an Object-Oriented Design... 11 1.4 Implementing an OOP in C++ 12 1.4.1 A Short Preview of Object-Oriented Programming. 14 1.5 Example Databases 17 1.6 Big Oh Notation 17 1.7 Exercises 18 2 Pointers, Structures, Classes, Functions, Overloaded Functions, and Overlodaded Operators in C-\ h 21 2.1 C++ Pointers 21 2.1.1 C++ Pointer Arithmetic and Operations 24 2.1.2 Call-by-Reference Using Pointers as Function Arguments 24 2.1.3 Pointers as Return Values of Functions 26 2.2 Structures in C++ 26 2.2.1 Denning a Structure 26 2.2.2 Pointers to Structures 27 2.2.3 Accessing Structures 28 2.2.4 Initializing Structures 28 2.2.5 Structure as a Function Argument and Return Value 30 2.2.6 Pointer to a Structure as a Function Argument... 30 2.2.7 Arrays of Structures 31 2.3 Unions 31 2.4 C++ Class 32
2.4.1 Defining a Member Function of a Class Outside Its Scope 34 2.4.2 Defining an Object of a Class 34 2.4.3 Accessing a Member of a Class 35 2.4.4 Friend of a Class and Inheritance 37 2.4.5 Derived Class and Multiple Inheritance 38 2.4.6 Nested Class 40 2.5 Functions in C++ 41 2.5.1 Special Functions: Constructors 41 2.5.2 Special Functions: Destructors 42 2.6 Polymorphism, Virtual Functions, and Inheritance 43 2.6.1 Friend Functions and Inheritance 43 2.6.2 Overloading and Polymorphism 43 2.6.3 Overloaded Functions 43 2.6.4 Overloaded Operators 44 2.7 Dangling Pointers and Memory Leaks 44 2.7.1 Dangling Pointers 45 2.7.2 Memory Leaks 45 2.8 OOP Application: Complex Numbers 47 2.9 Exercises 50 Arrays and Strings 51 3.1 Array Objects 51 3.2 One-Dimensional Arrays 53 3.2.1 Declaration of Arrays in C++ 53 3.2.2 Storing and Retrieving an Element in an Array... 53 3.2.3 Initializing an Array 54 3.2.4 One-Dimensional Array Address Translation... 54 3.2.5 Array as Function Arguments 55 3.2.6 One-Dimensional Array Object 55 3.2.7 OOP for One-Dimensional Array 56 3.3 Two-Dimensional Arrays 61 3.3.1 C++Declaration of Two-Dimensional Arrays... 62 3.3.2 Storing and Retrieving an Element in a Two-dimensional Array 62 3.3.3 Initializing a Two-Dimensional Array 64 3.3.4 Translating Address of Two-Dimensional Array Elements 64 3.3.5 Two-Dimensional Arrays as Function Arguments.. 65 3.3.6 Two-Dimensional Array Object 65 3.3.7 OOP Two-Dimensional Array 66 3.4 Strings 74 3.4.1 Implementing Strings Using Arrays and Pointers.. 74 3.4.2 Array of Array-Based Strings 76 3.4.3 Array of Pointers to Strings 77
xv 3.4.4 String Object and Its OOP Implementation 78 3.5 OOP Application: An Object-Oriented Database 84 3.6 Exercises 94 Recursion 97 4.1 Concept of Recursion 97 4.2 Divide-and-Conquer and Recursion 98 4.3 Recursive and Nonrecursive Functions in C++ 98 4.4 Recursion and Trace of C++ Stack 103 4.5 OOP Application: The Towers of Hanoi 106 4.6 OOP Application: Nonattacking iv-queens 120 4.7 Key Points for Using Recursion 125 4.8 Exercises 126 Lists 131 5.1 List Objects 132 5.2 Implementation Specific Linked List Classes 134 5.3 Array-Based Linked Lists 134 5.3.1 OOP for Array-Based Linked Lists with Implicit Links 136 5.3.2 Adding an Element After a Given Element 138 5.3.3 Deleting an Element from an Array List 138 5.4 Pointer-Based Linked Lists 142 5.4.1 Non-OOP Implementation of the Singly Linked List 143 5.4.2 OOP Implementation of the Singly Linked List... 145 5.4.3 Building a Singly Linked List 147 5.4.4 Inserting an Element in a Singly Linked List... 147 5.4.5 Deleting an Element in a Singly Linked List 149 5.4.6 Methods of the Singly_Linked_List Class.... 151 5.4.7 OOP Implementation of the Doubly Linked List.. 157 5.4.8 Adding an Element in a Doubly Linked List 160 5.4.9 Deleting an Element in a Doubly Linked List... 162 5.4.10 Methods of the Doubly_Linked_List Class... 163 5.5 Circular List Objects 170 5.5.1 OOP Implementation of Singly Linked Circular Lists 171 5.5.2 Methods of the Circ_Linked_List Class 172 5.5.3 Doubly Linked Circular List and Its OOP Implementation 179 5.6 Performance Analyses of List Operations 179 5.7 OOP Application: Polynomial Objects in Single Variable.. 180 5.7.1 Concept of a Polynomial in Single Variable 180 5.7.2 Polynomial Objects 181 5.7.3 Object-Oriented Design and Implementation for Polynomials 181 5.8 OOP Application: Memory Management 193
xvi Contents 5.8.1 The Free List 194 5.8.2 Free List Management by Counted Pointers 203 5.8.3 Free List Management by Garbage Collection... 207 5.9 Summary 208 5.10 Exercises 209 6 Stacks and Queues 217 6.1 Stack Objects 217 6.1.1 OOP Array Implementation of a Stack Object... 221 6.1.2 OOP Implementation of a Stack Using Linked Lists 228 6.1.3 Performance Analyses of Stack Operations 236 6.2 Double Stack Objects 236 6.3 OOP Application: Reverse Polish Notation Using Stacks.. 237 6.3.1 Postfix Evaluation 238 6.3.2 Infix to RPN Translation 243 6.4 Queue Objects 250 6.5 Implementation Specific Queue Classes 253 6.5.1 OOP Implementation of a Queue Using Array... 253 6.5.2 OOP Implementation of a Queue Using Linked List 261 6.6 Circular Queue Objects 268 6.6.1 OOP Implementation of a Circular Queue Using Array 269 6.6.2 OOP Implementation of a Circular Queue Using a Linked List 278 6.6.3 Performance Analyses of Queue Operations 279 6.7 OOP Application: SCAN Disk Scheduling with Priority Queues 280 6.8 Exercises 290 7 Sorting and Searching 295 7.1 Sorting Methods 295 7.1.1 Insertion Sort for an Array List 298 7.1.2 Insertion Sort for a Linked List 301 7.1.3 Selection Sort 306 7.1.4 Bubble Sort 309 7.1.5 Quicksort 314 7.1.6 Merge Sort 320 7.1.7 Binary Tree Sort 330 7.1.8 Heap Sort 331 7.1.9 Straight Radix Sort 342 7.1.10 Radix Exchange Sort 347 7.1.11 Shell Sort 352 7.1.12 Performance Analyses of Sorting Methods 356 7.2 Searching Methods 357 7.2.1 Linear Search of an Unsorted Array 358
xvii 7.2.2 Linear Search of an Unsorted Linked List 361 7.2.3 Linear Search of a Sorted Array 363 7.2.4 Linear Search of a Sorted List 366 7.2.5 Binary Search of a Sorted Array 366 7.2.6 Interpolation Search of a Sorted Array 370 7.2.7 Fibonacci Search of a Sorted Array 373 7.2.8 Searching a Binary Search Tree 376 7.2.9 Hash Strategy for Hash Search Method 378 7.2.10 Performance Analyses of Searching Algorithms... 405 7.3 Exercises 405 8 Trees and Tries 409 8.1 Fundamental Definitions and Terminology 409 8.2 M-ary Trees 412 8.3 Traversing a Tree 413 8.3.1 Traversals of Binary Trees 413 8.4 Tree Objects 417 8.5 OOP Implementation of Binary Trees 418 8.5.1 OOP Implementation of a Binary Tree Using Arrays 419 8.5.2 OOP Implementation of a Binary Tree Using Pointers 423 8.5.3 Methods of the Binary_Tree Class 426 8.6 General Trees 433 8.6.1 Strategies for Representing General Trees 435 8.6.2 General Tree: Binary Tree Implementation 436 8.6.3 General Tree Traversal 437 8.6.4 OOP Implementation of a General Tree 437 8.6.5 Methods of the General-Tree Class 439 8.7 Search Trees 443 8.7.1 Data-Comparative Search Trees Versus Radix Search Trees 445 8.8 Data-Comparative M-ary Search Trees 446 8.8.1 Inserting a Node and Building a Binary Search Tree 447 8.8.2 Deleting a Node from a BST 449 8.8.3 OOP Implementation of a Binary Search Tree Using Pointers 451 8.8.4 Methods of the Binary_Search_Tree Class... 453 8.8.5 BST and Quicksort Relationship 459 8.8.6 Balance Characteristics of Comparative-Based Search Tree 459 8.8.7 AVL Trees 460 8.8.8 AVL Tree Objects 462 8.8.9 OOP Implementation of an AVL Tree Using Pointers 462 8.8.10 Insertion of a Node in an AVL Tree 464 8.8.11 Inserting a New Node into an AVL Tree 464
ii Contents 8.8.12 Inserting a Node and Building an AVL Tree 465 8.8.13 Creating a Node for an AVL Tree 468 8.8.14 Deleting a Node from an AVL Tree 468 8.8.15 Regaining Balance with Rotation Techniques... 469 8.9 Radix Search Trees 478 8.9.1 Discrete Versus Non-Discrete Keys 479 8.9.2 Digital Search Trees 481 8.9.3 OOP Implementation of a Binary Digital Search Tree 482 8.9.4 Radix Search Tries 488 8.9.5 OOP Implementation of an M-ary Radix Search Trie 493 8.9.6 Balance Characteristics of Radix Search Trees... 500 8.9.7 Hybrid Radix Search Tries 502 8.9.8 Radix Search Tries and Radix Exchange Sorting.. 502 8.9.9 OOP Application: Word Dictionaries Using Tries.. 503 8.9.10 Patricia Trees and Tries 504 8.10 Comparative-Based B-Trees for External Searching and Sorting 507 8.10.1 B-Tree Objects 507 8.10.2 Inserting a Key and Building a B-Tree 509 8.10.3 Deleting a Key from a B-Tree 513 8.11 Performance Analysis of Tree Operations 517 8.12 Exercises 518 Multidimensional Search Trees and Search Tries 527 9.1 Extending the Single-Key Model 527 9.2 Geometric Formulation of Associative Search 528 9.2.1 Records as Points in Key-Space 528 9.2.2 Geometric Objects in Euclidean Space 529 9.3 Types of Associative Search 532 9.4 Examples of Associative Search 534 9.5 Approaches to Associative Search 536 9.5.1 ADT Inverted List 537 9.6 Multidimensional Comparative-Based Search Trees 538 9.6.1 K-Tree Objects 538 9.6.2 OOP Implementation of Quadtree 539 9.6.3 K-Tree Balance and Node Deletion 547 9.6.4 Kd-Tree Objects 547 9.6.5 OOP Implementation of 3d-Tree 550 9.6.6 Kd-Tree Balance and Node Deletion 559 9.7 Multidimensional Radix Search Tries 560 9.7.1 K-Trie Objects 561 9.7.2 Kd-Trie Objects 561 9.7.3 Implementation of K-Trie and Kd-Trie 563 9.7.4 Compact Trie Representations 563 9.8 Multidimensional Structures for External Search 565
xix 9.9 Summary: A Taxonomy of Trees and Tries 566 9.10 Exercises 566 10 Graphs and Digraphs 569 10.1 Fundamental Definitions and Terminologies 570 10.2 Graph Traversal 571 10.2.1 Depth-First Traversal 572 10.2.2 Breadth-First Traversals 573 10.3 Graph Objects 575 10.4 Implementations of a Graph 576 10.4.1 Representing a Weighted Undirected or Directed Graph Using Adjacency Matrix 577 10.4.2 OOP Implementation of a Graph Using Adjacency Matrix 579 10.4.3 Methods of Weighted_DiGraph Class 581 10.4.4 OOP Implementation of a Graph Using Linked Adjacency Lists 589 10.4.5 Methods of the Wt_DiGraph Class 591 10.5 Spanning Trees of a Graph 602 10.5.1 Constructing a Spanning Tree Using Depth-First Traversals 603 10.5.2 Constructing a Spanning Tree Using Breadth-First Traversals 604 10.6 OOP Application: Determining the Shortest Path in a Weighted Digraph Using Dijkstra's Algorithm 605 10.7 Exercises 609 11 An Object-Oriented Database with B-Trees 613 11.1 Specification of People Database System 613 11.2 OOP Implementation of Simple People Database Using B-Trees 614 11.2.1 Methods of the B_Tree Class 616 11.3 Object-Oriented People Database Program 619 11.4 Limitations of Implementation 637 11.5 Exercises 638 12 Applications in Image Processing, Computer Graphics, and Computer-Aided Design 639 12.1 2-D Digital Image Compression with a Quadtrie Object.. 639 12.2 Computer-Aided VLSI Design Verification with a 4D-Tree Object 650 12.3 3-D Ray-Tracing Acceleration with an Octrie Object... 659 12.4 3-D Hidden Surface Removal with a BSP Tree Object... 674 12.5 Exercises 684
: xx Contents A C++ Fundamentals 687 A.I C++ Key Words 687 i A.2 C++ Special Characters 687! A.3 Allowed Overloaded Operators in C++ 687 ' A.4 C++ Built-in Data Types 687 A.5 Statement Formats of Some C++ Keywords 688 A.6 A Sample C++ Program 689 A.7 C++ Preprocessor Directives 689 A.8 Creating Executables for C++ Programs 690 B Assorted Library Functions for Handling Strings 691 C Example Databases 693 C.I PEOPLE and GEOMETRY Databases 693 C.I.I PEOPLE_1D 693 C.I.2 PEOPLE.2D 696 C.1.3 PEOPLE.3D 697 C.1.4 GEOMETRY_2D 698 C.I.5 GEOMETRY_3D 699 References 701 Index 704