object-oriented data structures - rd.springer.com978-1-4612-2636-9/1.pdfc++, object-oriented data...

17
Object-Oriented Data Structures

Upload: phamliem

Post on 25-Apr-2018

221 views

Category:

Documents


1 download

TRANSCRIPT

Object-Oriented Data Structures

Saumyendra Sengupta Carl Phillip Korobkin

Object-Oriented Data Structures

With 165 Illustrations

Diskette Included

Springer-Verlag New York Berlin Heidelberg London Paris Tokyo Hong Kong Barcelona Budapest

Saumyendra Sengupta Carl Phillip Korobkin Silicon Graphics Corporation 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311

Cover photograph of the Temple of the Concordia, Agrigento, Italy. Courtesy of David Viaggi, U.S.A.

Library of Congress Cataloging-in-Publication Data Saumyendra, Sengupta.

C++, object-oriented data structures I Saumyendra Senguta, Carl Phillip Korobkin.

p. cm. Includes bibliographical references and index.

Additional material to this book can be downloaded from http://extras.springer.com.

ISBN-13: 978-1-4612-7618-0 e-ISBN-13: 987-1-4612-2636-9 DOl: I 0.1 007/978-1-4612-2636-9

1. C++ (Computer program language) 2. Object-oriented programming (Computer science) 3. Data structures (Computer science) I. Korobkin, Carl Phillip. II. Title. QA76.73.C153S28 1994 005.7'3-dc20 93-40950

Printed on acid-free paper

@1994 Springer-Verlag New York, Inc.

softcover reprint of the hardcover 1st edition 1994

All rights reserved. This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY 10010, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed is forbidden. The use of general descriptive names, trade names, trademarks, etc., in this publication, even if the former are not especially identified, is not to be taken as a sign that such names, as understood by the Trade Marks and Merchandise Marks Act, may accordingly be used freely by anyone.

Production managed by Laura Carlson; manufacturing supervised by Vincent Scelta. Photocomposed copy prepared from the authors' ~TEX files.

987654321

ISBN-13: 978-1-4612-7618-0 e-ISBN-13: 987-1-4612-2636-9

with heartfelt love and deep admiration for my mother,

Prativa Sengupta, my father,

Santosh Kumar Sengupta, and my grandparents

dedicated to the hard work and sacrifice of my grandparents,

Samuel and Frances Harrisburg William and Frieda K orobkin

Preface

This book provides a broad coverage of fundamental and advanced con­cepts of data structures and algorithms. The material presented includes a treatment of elementary data structures such as arrays, lists, stacks, and trees, as well as newer structures that have emerged to support the process­ing of multidimensional or spatial data files. These newer structures and algorithms have received increasing attention in recent years in conjunc­tion with the rapid growth in computer-aided design, computer graphics, and related fields in which multidimensional data structures are of great interest.

Our main objective is to mesh the underlying concepts with application examples that are of practical use and are timely in their implementations. To this end, we have used mainly the Abstract Data Structure (or Abstract Data Type (ADT)) approach to define structures for data and operations. Object-oriented programming (OOP) methodologies are employed to im­plement these ADT concepts. In OOP, data and operations for an ADT are combined into a single entity (object). ADTs are used to specifiy the objects-arrays, stacks, queues, trees, and graphs. OOP allows the pro­grammer to more closely mimic the real-world applications. This OOP is more structured and modular than previous attempts. OOP has become de facto state-of-the-art in the 1990s.

We use the C++ programming language throughout this book. C++ is an extension of the popular C programming language and has gained considerable popularity within the software engineering community. For our purposes, C++ provides facilities for data abstraction and object-oriented programming while retaining the convenient notation and flexibility of C.

Each abstract data type introduced contains useful information from con­cept to application. Throughout this book, structures and algorithms are accompanied with compilable C++ OOP implementations as well as per­formance analysis information, providing a means to make practical com­parisons and application decisions. The implementations presented make heavy use of pointers and dynamic memory allocation, although static ar­ray implementations are also presented. All implementations are compilable using either the Borland International C++ (.cpp) or AT&T C++ (.c++) compilers.

The last chapters of this book contain state-of-the-art applications in the C++ programming language. For the sake of clarity, completeness,

viii Preface

and usability, every C++ program for the examples and applications given contains a main () test driver and sample output run.

Coverage of the basic data types and structured data types in C++ is provided. A brief reference for the C++ language and the standard string manipulation library are contained in appendices A and B. We do, however, assume that the reader is familiar with the C++ programming language.

This book is intended as a text for an undergraduate course in Data Structures and Advanced Programming in C++, as well as a working ref­erence for software professionals.

Organization of the Book

Each chapter is organized by section. Major sections contain discussion, illustrative examples, and full C++ implementations.

Chapter 1 Concepts of Function-Oriented and Object-Oriented Data Struc­tures is an overview of key concepts and terminologies for traditional and OOP structures and methodologies.

Chapter 2 Pointers, Structures, Classes, Functions, Overloaded Func­tions, and Overloaded Operators in C++ presents a review of the major concepts in the C++ programming language.

Chapter 3 Arrays and Strings introduces the concepts of ADT arrays and strings, array and string objects, and their object-oriented implemen­tations.

Chapter 4 Recursion is a treatment of the concepts and applications of recursive techniques.

Chapter 5 Lists is an introduction to ADT lists, list objects, and their object-oriented implementations using arrays and pointers.

Chapter 6 Stacks and Queues presents the concepts of ADT stacks and queues, stack and queue objects, and their object-oriented implementa­tions.

Chapter 7 Sorting and Searching is a presentation of comparative-based and digital-based techniques for sorting and searching.

Chapter 8 Trees and Tries introduces the topics of ADT tree and trie structures, tree objects, and their object-oriented applications.

Chapter 9 Multidimensional Search Trees and Search Tries expands the

Preface ix

ADT tree and trie concepts to the object-oriented applications of multidi­mensional data.

Chapter 10 Graphs and Digraphs presents the ADT graph structures, graph objects, and their object-oriented applications.

Chapter 11 An Object-Oriented Database with B-Trees is an example of a common application of the ADT B-Tree.

Chapter 12 Image Processing, Computer Graphics, and CAD shows the application of the ADT multidimensional search trees and tries to problems in visual processing, VLSI design, ray tracing, and hidden surface removal.

Appendix A C++ Fundamentals is a high-level overview of the C++ lan­guage.

Appendix B Assorted Library Functions for Handling Strings contains some useful C++ library functions for processing strings.

Appendix C Example Databases contains several simple databases used throughout the text.

Production Notes

This book was produced from camera-ready (typeset and illustrated) copy provided by the authors. The entire book was typeset and illustrated with a variety of tools running on Silicon Graphics Iris Indigo and Personal Iris workstations. These tools include the 'lEX and IffiTEX typesetting pro­grams, and the Iris Showcase and Adobe Illustrator illustration packages.

The cover image, the Temple of Concordia, was reprinted with permission granted to the authors by David Viaggi, U.S.A.

About the Code

All example programs in this book were compiled and tested using the AT&T C++ 3.0 compiler under the Silicon Graphics IRIX operating sys­tem (based on AT&T UNIX System V) and using the Borland Turbo C++ 3.1 compiler under IBM PCjMS-DOS. The AT&T C++ 3.0 compiler is not backwardly compatible with its earlier versions (such as 2.0 and 1.0). Therefore, the code presented in this book is not compilable with these earlier compiler releases. Some codes that are compilable using the AT&T C++ 3.0 compiler are not compilable using the the Borland Turbo C++

x Preface

3.1 compiler because some "scoping rules" required by the AT&T C++ 3.0 are not recognized by the Borland Turbo C++ 3.1. In addition, the authors cannot guarantee that the code is compilable (without modification) with future compiler releases.

The authors have attempted to provide code free of errors. Each program has been compiled, executed, and examined for bugs. We are confident that the product is of a high quality, but cannot be responsible for errors that may be discovered.

Trademark Recognition

There are a number of names used throughout the book that are recognized trademarks:

AdobeIllustrator is a trademark of Adobe Systems, Inc. C++ 3.0 is a trademark of AT&T Corporation. IBM PC is a trademark of IBM Corporation. IRIX is a trademark of Silicon Graphics Computer Systems, Inc. Iris ImageVision is a trademark of Silicon Graphics, Inc. Iris Indigo is a trademark of Silicon Graphics Computer Systems, Inc. Iris Showcase is a trademark of Silicon Graphics Computer Systems, Inc. MS-DOS is a trademark of MicroSoft Corporation. Personal Iris is a trademark of Silicon Graphics Computer Systems, Inc. Turbo C++ is a trademark of Borland International. UNIX System V is a trademark of AT&T Corporation.

Acknowledgments

We have been fortunate by the active interest shown by our students, col­leagues, and associates in the creation of this book.

At Silicon Graphics: Marc Hannah and Jim Winget provided valuable discussions, resources, and support early on. Derrick Burns and Mark Segal reviewed the text at many stages during its development and contributed invaluably to this work - they also taught us the art of 1EXand Iffi-TEX, with which we typeset this book. Tom Davis, David Fenstemaker, Efi Fogel, Andrew Grant, and Andrew Harvey served as reviewers and contributed many excellent comments and suggestions. Christine Butler moved (and climbed) mountains.

At San Jose State University, Professor Vinh Phat reviewed the text and contributed many useful comments. We are also grateful to the numerous students for their efforts.

Jack Menedez, Frank Kretz, Vasudha Bhaskara, and Jose Oliva made

Preface xi

several contributions to the material. We are especially greatful to Martin Gilchrist, Laura Carlson, Karen

Kosztolnyik, Andrea Arnold, and David Shapiro, our editors at Springer­Verlag New York, and other staff members for their patience and dedicated efforts throughout the publication process.

A special thanks goes to Amy and Nate Smith at SCI Yarmouth (Maine) who made their home and their Indigo available for some "out of town" work on the manuscript.

While many people have supported our efforts, no one has given more of themselves than our families.

Thanks to the Sengupta family: my mother Prativa, brothers Saurendra and Nityananda, sisters Bani, Arati, Subhra, and Snigha, wife Juthika, daughters Lopamudra, Ballori, and Kasturi, and my father-in-law Dina Bandhu Roy. (S.S.)

Thanks to the Korobkin and Harrisburg families for their unwavering support, especially my parents Shirley and Harvey and brothers Paul and Steve. (C.K.)

S. Sengupta C. Korobkin

December 1993

Contents

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++ 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 Defining 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

xiv Contents

2.4.1

2.4.2 2.4.3 2.4.4 2.4.5 2.4.6

Defining a Member Function of a Class Outside Its Scope ............. . Defining an Object of a Class .. . Accessing a Member of a Class . . Friend of a Class and Inheritance . Derived Class and Multiple Inheritance Nested Class .......... .

34 34 35 37 38 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

3 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

Contents xv

3.4.4 String Object and Its OOP Implementation 78 3.5 OOP Application: An Object-Oriented Database 84 3.6 Exercises ...................... 94

4 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 N-Queens 120 4.7 Key Points for Using Recursion 125 4.8 Exercises ............ 126

5 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 CircLinked_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

Contents 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 GeneraLTree 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

xviii Contents

9

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

Contents 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 Traversals . . . . . . . . . 571

10.2.1 Depth-First Traversals. . 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 WLDiGraph 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 ofImplementation 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 A.l C++ Key Words ............ . A.2 C++ Special Characters ........ . A.3 Allowed Overloaded Operators in C++ A.4 C++ Built-in Data Types ........... . A.5 Statement Formats of Some C++ Keywords . . . A.6 A Sample C++ Program ......... . A.7 C++ Preprocessor Directives . . . . . . . A.8 Creating Executables for C++ Programs .

B Assorted Library Functions for Handling Strings

C Example Databases C.l PEOPLE and GEOMETRY Databases.

C.l.l PEOPLE-lD C.l.2 PEOPLE-2D .. . C.l.3 PEOPLE-3D .. . C.l.4 GEOMETRY _2D . C.l.5 GEOMETRY_3D. . .....

References

Index

687 687 687 687 687 688 689 689 690

691

693 693 693 696 697 698 699

701

704