fundamentals of data structures in c++

9
FUNDAMENTALS OF DATA STRUCTURES IN C + + Ellis Horowitz University of Southern California Sartaj Sahni University of Florida Dinesh Mehta University of Tennessee COMPUTER SCIENCE PRESS An imprint of W. H. Freeman and Company New York

Upload: others

Post on 28-Apr-2022

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FUNDAMENTALS OF DATA STRUCTURES IN C++

FUNDAMENTALS OF DATA STRUCTURES IN C + +

Ellis Horowitz University of Southern California

Sartaj Sahni University of Florida

Dinesh Mehta University of Tennessee

COMPUTER SCIENCE PRESS An imprint of W. H. Freeman and Company

New York

Page 2: FUNDAMENTALS OF DATA STRUCTURES IN C++

CONTENTS

PREFACE xv

CHAPTER 1 BASIC CONCEPTS 1 1.1 Overview: System Life Cycle 1 1.2 Object-Oriented Design 4

1.2.1 Algorithmic Decomposition versus OO Decomposition 1.2.2 Fundamental Definitions and Concepts of OO Programming 1.2.3 Evolution of Programming Languages and History of C++

1.3 Data Abstraction and Encapsulation 6 1.4 Basics of C++ 11

1.4.1 Program Organization in C++ 11 1.4.2 Scope in C++ 12 1.4.3 C++Statements and Operators 13 1.4.4 Data Declarations in C++ 14 1.4.5 Comments in C++ 15 1.4.6 Input/Output in C++ 15 1.4.7 Functions in C++ 17 1.4.8 Parameter Passing in C++ 18 1.4.9 Function Name Overloading in C++ 19 1.4.10 Inline Functions 19 1.4.11 Dynamic Memory Allocation in C++ 20

vi

Page 3: FUNDAMENTALS OF DATA STRUCTURES IN C++

Contents vii

1.5 Algorithm Specification 20 1.5.1 Introduction 20 1.5.2 Recursive Algorithms 25

1.6 Performance Analysis And Measurement 30 1.6.1 Performance Analysis 30 1.6.2 Performance Measurement 51 1.6.3 Generating Test Data 60

1.7 References And Selected Readings 64

CHAPTER 2 ARRAYS 66 2.1 Abstract Data Types and the C++Class 66

2.1.1 An Introduction to the C++ Class 66 2.1.2 Data Abstraction and Encapsulation in C++ 67 2.1.3 Declaring Class Objects and Invoking Member Functions 68 2.1.4 Special Class Operations 68 2.1.5 Miscellaneous Topics 73 2.1.6 ADTs and C++Classes 73

2.2 The Array as an Abstract Data Type 75 2.3 The Polynomial Abstract Data Type 77

2.3.1 Polynomial Representation 80 2.3.2 Polynomial Addition 82 2.3.3 Disadvantages of Representing Polynomials by Arrays 84

2.4 Sparse Matrices 87 2.4.1 Introduction 87 2.4.2 Sparse Matrix Representation 88 2.4.3 Transposing a Matrix 89 2.4.4 Matrix Multiplication 93 2.4.5 Disadvantages of Representing Sparse Matrices by Arrays 98

2.5 Representation of Arrays 100 2.6 The String Abstract Data Type 104

2.6.1 String Pattern Matching: A Simple Algorithm 105 2.6.2 String Pattern Matching: The KMP Algorithm 106

2.7 References and Selected Readings 111 2.8 Additional Exercises 111

CHAPTER 3 STACKS AND QUEUES 119 3.1 Templates in C++ 119

3.1.1 Template Functions 119 3.1.2 Using Templates to Represent Container Classes 121

3.2 The Stack Abstract Data Type 124 3.3 The Queue Abstract Data Type 131 3.4 Subtyping and Inheritance in C++ 137 3.5 A Mazing Problem 140

Page 4: FUNDAMENTALS OF DATA STRUCTURES IN C++

viii Contents

3.6 Evaluation of Expressions 147 3.6.1 Expressions 147 3.6.2 Postfix Notation 148 3.6.3 Infix to Postfix 150

3.7 Multiple Stacks And Queues 154 3.8 Selected Readings And References 158 3.9 Additional Exercises 158

CHAPTER 4 LINKED LISTS 161 4.1 Singly Linked Lists 161 4.2 Representing Lists in C++ 165

4.2.1 Defining a List Node in C++ 165 4.2.2 Designing a List in C++ 166 4.2.3 Pointer Manipulation in C++ 170 4.2.4 List Manipulation Operations 171

4.3 A Reusable Linked List Class 174 4.3.1 Implementing Linked Lists with Templates 175 4.3.2 Linked List Iterators 176 4.3.3 Linked List Operations 179 4.3.4 Reusing a Class 182

4.4 Circular Lists 183 4.5 Linked Stacks and Queues 186 4.6 Polynomials 190

4.6.1 Polynomial Representation 190 4.6.2 Adding Polynomials 190 4.6.3 Erasing Polynomials 194 4.6.4 Circular List Representation of Polynomials 195 4.6.5 Summary 199

4.7 Equivalence Classes 202 4.8 Sparse Matrices 207

4.8.1 Sparse Matrix Representation 207 4.8.2 Sparse Matrix Input 209 4.8.3 Erasing a Sparse Matrix 213

4.9 Doubly Linked Lists 217 4.10 Generalized Lists 221

4.10.1 Representation of Generalized Lists 221 4.10.2 Recursive Algorithms for Lists 225 4.10.3 Reference Counts, Shared and Recursive Lists 230

4.11 Virtual Functions and Dynamic Binding in C++ 237 4.12 Heterogeneous Lists 241 4.13 References and Selected Readings 245

Page 5: FUNDAMENTALS OF DATA STRUCTURES IN C++

Contents ix

CHAPTER 5 TREES 246 5.1 Introduction 246

5.1.1 Terminology 246 5.1.2 Representation of Trees 249

5.2 Binary Trees 252 5.2.1 The Abstract Data Type 252 5.2.2 Properties of Binary Trees 255 5.2.3 Binary Tree Representations 257

5.3 Binary Tree Traversal and Tree Iterators 261 5.3.1 Introduction 261 5.3.2 Inorder Traversal 262 5.3.3 Preorder Traversal 263 5.3.4 Postorder Traversal 263 5.3.5 Iterative Inorder Traversal 265 5.3.6 Level-Order Traversal 268 5.3.7 Traversal Without a Stack 269

5.4 Additional Binary Tree Operations 271 5.4.1 Copying Binary Trees 271 5.4.2 Testing Equality 271 5.4.3 The Satisfiability Problem 272

5.5 Threaded Binary Trees 277 5.5.1 Threads 277 5.5.2 Inorder Traversal of a Threaded Binary Tree 278 5.5.3 Inserting a Node into a Threaded Binary Tree 281

5.6 Heaps 283 5.6.1 Priority Queues 283 5.6.2 Definition of a Max Heap 285 5.6.3 Insertion into a Max Heap 286 5.6.4 Deletion from a Max Heap 288

5.7 Binary Search Trees 291 5.7.1 Definition 291 5.7.2 Searching a Binary Search Tree 292 5.7.3 Insertion into a Binary Search Tree 293 5.7.4 Deletion from a Binary Search Tree 294 5.7.5 Joining and Splitting Binary Search Trees 295 5.7.6 Height of a Binary Search Tree 298

5.8 Selection Trees 300 5.8.1 Introduction 300 5.8.2 Winner Trees 300 5.8.3 Loser Trees 301

5.9 Forests 304 5.9.1 Transforming a Forest into a Binary Search Tree 304 5.9.2 Forest Traversals 305

Page 6: FUNDAMENTALS OF DATA STRUCTURES IN C++

x Contents

5.10 Set Representation 307 5.10.1 Introduction 307 5.10.2 Union and Find Operations 307 5.10.3 Application to Equivalence Classes 316

5.11 An Object-Oriented System of Tree Data Structures 319 5.12 Counting Binary Trees 321

5.12.1 Distinct Binary Trees 322 5.12.2 Stack Permutations 323 5.12.3 Matrix Multiplication 326 5.12.4 Number of Distinct Binary Trees 327

5.13 References and Selected Readings 328

CHAPTER 6 GRAPHS 330 6.1 The Graph Abstract Data Type 330

6.1.1 Introduction 330 6.1.2 Definitions 332 6.1.3 Graph Representations 336

6.2 Elementary Graph Operations 345 6.2.1 Depth First Search 345 6.2.2 Breadth First Search 347 6.2.3 Connected Components 348 6.2.4 Spanning Trees 348 6.2.5 Biconnected Components 350

6.3 Minimum Cost Spanning Trees 356 6.3.1 Kruskal's Algorithm 357 6.3.2 Prim's Algorithm 361 6.3.3 Sollin's Algorithm 361

6.4 Shortest Paths and Transitive Closure 364 6.4.1 Single Source/All Destination: Nonnegative Edge Costs 364 6.4.2 Single Source/All Destination: General Weights 367 6.4.3 All-Pairs Shortest Paths 371 6.4.4 Transitive Closure 373

6.5 Activity Networks 378 6.5.1 Activity on Vertex (AOV) Networks 378 6.5.2 Activity on Edge (AOE) Networks 383

6.6 References and Selected Readings 393 6.7 Additional Exercises 394

CHAPTER 7 SORTING 397 7.1 Motivation 397 7.2 Insertion Sort 402 7.3 Quick Sort 405 7.4 How Fast Can We Sort? 408

Page 7: FUNDAMENTALS OF DATA STRUCTURES IN C++

Contents xi

7.5 Merge Sort 410 7.5.1 Merging 410 7.5.2 Iterative Merge Sort 416 7.5.3 Recursive Merge Sort 418

7.6 Heap Sort 423 7.7 Sorting on Several Keys 425 7.8 List and Table Sorts 431 7.9 Summary of Internal Sorting 439 7.10 External Sorting 445

7.10.1 Introduction 445 7.10.2 £-way Merging 448 7.10.3 Buffer Handling for Parallel Operation 450 7.10.4 Run Generation 456 7.10.5 Optimal Merging of Runs 458

7.11 References and Selected Readings 463

CHAPTER 8 HASHING 464 8.1 The Symbol Table Abstract Data Type 464 8.2 Static Hashing 466

8.2.1 Hash Tables 466 8.2.2 Hashing Functions 468 8.2.3 Overflow Handling 471 8.2.4 Theoretical Evaluation of Overflow Techniques 478

8.3 Dynamic Hashing 482 8.3.1 Motivation for Dynamic Hashing 482 8.3.2 Dynamic Hashing using Directories 483 8.3.3 Analysis of Directory-Based Dynamic Hashing 489 8.3.4 Directoryless Dynamic Hashing 491

8.4 References and Selected Readings 496

CHAPTER 9 HEAP STRUCTURES 497 9.1 Min-Max Heaps 497

9.1.1 Definitions 497 9.1.2 Insertion into a Min-Max Heap 499 9.1.3 Deletion of the Min Element 502

9.2 Deaps 507 9.2.1 Definition 507 9.2.2 Insertion into a Deap 508 9.2.3 Deletion of the Min Element 511

9.3 Leftist Trees 515 9.4 Binomial Heaps 522

9.4.1 Cost Amortization 522 9.4.2 Definition of Binomial Heaps 523

Page 8: FUNDAMENTALS OF DATA STRUCTURES IN C++

xii Contents

9.4.3 Insertion into a Binomial Heap 524 9.4.4 Combining Two Binomial Heaps 524 9.4.5 Deletion of Min Element 525 9.4.6 Analysis 529

9.5 Fibonacci Heaps 531 9.5.1 Definition 531 9.5.2 Deletion from an F-heap 532 9.5.3 Decrease Key 532 9.5.4 Cascading Cut 533 9.5.5 Analysis 534 9.5.6 Application to The Shortest Paths Problem 536

9.6 References and Selected Readings 539 9.7 Additional Exercise 539

CHAPTER 10 SEARCH STRUCTURES 541 10.1 Optimal Binary Search Trees 541 10.2 AVL Trees 551 10.3 2-3 Trees 567

10.3.1 Definition and Properties 567 10.3.2 Searching a 2-3 Tree 569 10.3.3 Insertion into a 2-3 Tree 569 10.3.4 Deletion from a 2-3 Tree 573

10.4 2-3-4 Trees 580 10.4.1 Definition and Properties 580 10.4.2 Top-Down Insertion 582 10.4.3 Top-Down Deletion 586

10.5 Red-Black Trees 589 10.5.1 Definition and Properties 589 10.5.2 Searching a Red-Black Tree 592 10.5.3 Top-Down Insertion 592 10.5.4 Bottom-Up Insertion 594 10.5.5 Deletion from a Red-Black Tree 598 10.5.6 Joining and Splitting Red-Black Trees 598

10.6 B-Trees 602 10.6.1 Definition of m-way Search Trees 602 10.6.2 Searching an m-way Search Tree 603 10.6.3 Definition and Properties Of A B-tree 604 10.6.4 Insertion into a B-tree 609 10.6.5 Deletion from a B-tree 611 10.6.6 Variable Size Key Values 614

10.7 Splay Trees 618 10.8 Digital Search Trees 624

10.8.1 Definition 624

Page 9: FUNDAMENTALS OF DATA STRUCTURES IN C++

10.8.2 Binary Tries 625 10.8.3 Patricia 627

10.9 Tries 630 10.9.1 Definition 630 10.9.2 Searching a Trie 633 10.9.3 Sampling Strategies 635 10.9.4 Insertion into a Trie 636 10.9.5 Deletion from a Trie 637 10.9.6 Node Structure 637

10.10 Differential Files 639 10.10.1 The Concept 639 10.10.2 Bloom Filters 642

10.11 References and Selected Readings 644

INDEX 646