data structures - gbv
TRANSCRIPT
Data Structures A Pseudocode Approach with С
Richard F. Gilberg De Anza College
Behrouz A. Forouzan De Anza College
PWS Publishing Company
I(T)P An International Thomson Publishing Company Boston 'Albany 'Bonn 'Cincinnati 'London 'Madrid
Melbourne • Mexico City • New York • Paris • San Francisco Singapore «Tokyo -Toronto -Washington
Preface хШ.
1 Introduction 1 1-1 Pseudocode 2
Algorithm Header 2 Purpose, Conditions, and Re tu rn 3 S ta tement Numbers 4 Variables 4 Algorithm Analysis 5 Sta tement Const ructs 5
Sequence 5 Selection 6 Loop 6
Pseudocode Example 6 1-2 The Abstract Data Type 7
Atomic and Composite Data 8 Data S t ruc ture 9 Abstract Data Type 9
1-3 A Model For An Abstract Data Type 1 1
ADT Operations 12 ADT Data S t ruc ture 12
1-4 Algorithm Efficiency 1 2 Linear Loops 14 Logarithmic Loops 14 Nested Loops 15
Linear Logarithmic 15 Dependent Quadratic 16 Quadratic 16
Big-O Notation 17 S tandard Measures of Efficiency 18 Big-O Analysis Examples 19
Add Matrices 19 Multiply Matrices 20
Co
1-5 1-6
ntents |
Summary 2 2 Pract ice S e t s 2 3 Exercises 23 Problems 25 Projects 25
2 Searching 27 2 - 1
2 - 2
2 - 3
Linear Lis t Searches 2 8 Sequential Search 28
Sequential Search Algorithm 28 Variations on Sequential
Searches 31 Sentinel Search 31 Probability Search 32 Ordered List Search 33
Binary Search 34 Target Found 34 Target Not Found 35 Binary Search Algorithm 37
Analyzing Linear Search Algorithms 38 Sequential Search 38 Binary Search 39
Linear Search С Algorithms 4 0 Sequential Search In С 40 Binary Search In С 41 Hashed Lis t Searches 4 2 Basic Concepts 43 Hashing Methods 44
Direct Method 44 Subtraction Method 46 Modulo-Division Method 46 Digit-Extraction Method 47 Midsquare Method 47
V
vi Contents
Folding Methods 48 Rotation Method 48 Pseudorandom Method 50
Hashing Algorithm 50 2 - 4 Collision Resolut ion 5 1
Open Addressing 53 Linear Probe 54 Quadratic Probe 55 Pseudorandom Collision
Resolution 55 Key Offset 56 Linked List Resolution 57 Bucket Hashing 58 Combination Approaches 59
2-5 Summary 6 0 2 - 6 Pract ice S e t s 6 1
Exercises 61 Problems 62 Projects 63
3 Linear Lists 65 3 - 1 Linear Lis t Concepts 6 6
Insertion 67 Deletion 67 Retrieval 68 Traversal 68
3 - 2 Linked Li s t s 6 9 Nodes 69 Linked List Data S t ruc ture 70
Head Node Structure 71 Data Node Structure 71
Pointers to Linked Lists 72 3 - 3 Linked Lis t Algorithms 7 2
Create List 74 Insert Node 76
Insert Into Empty List 76 Insert at Beginning 77 Insert in Middle 77 Insert at End 79 Insert Node Algorithm 79
Delete Node 79 Delete First Node 81 General Delete Case 82 Delete Node Algorithm 83
Search List 84 Retrieve Node 87 Empty List 87 Full List 88
List Count 88 Traverse List 89 Destroy List 91
3 - 4 Process ing A Linked Lis t 9 1 Add Node 93 Remove Node 94 Print List 95
Testing Insert and Delete Logic 96 3 - 5 Linear Lis t Appl icat ions 9 7
Append Linked Lists 97 Array of Linked Lists 100
3 - 6 Complex Linked Lis t Structures 1 0 2
Header Nodes 102 Circularly-Linked Lists 103 Doubly-Linked Lists 104
Doubly-Linked List Insertion 105 Doubly-Linked List Deletion 109
Multilinked Lists 110 Multilinked List Insert 112 Multilinked List Delete 113
3 - 7 Bui lding A Linked List—С Implementat ion 1 1 4
Data S t ruc ture 114 Application Funct ions 115
Main 115 Instructions 115 Build List 116 Compare Data 117 Process User Requests 118 Get User's Choice 119 Print List 120 Search List 121
3 - 8 Linked Lis t Abstract Data Type 1 2 2
List ADT Funct ions 123 Create List 124 Add Node 125 Insert Node 126 Remove Node 128 Delete Node 128 Search List 129 Internal Search Function 129 Retrieve Node 132 Empty List 133 Full List 133 List Count 134 Traverse 134 Destroy List 136
Contents vii
3-9 Summary 137 3-Ю Practice Sets 137
Exercises 137 Problems 140 Projects 141
4 Stacks 149 4-1 Basic Stack Operations 150
Push 150 Pop 150 Stack Top 151
4-2 Stack—Linked-List Implementation 153
Data Structure 153 Stack Head 153 Stack Data Node 153
Stack Algorithms 153 Create Stack 157 Push Stack 158 Pop Stack 159 Stack Top 160 Empty Stack 161 Full Stack 161 Stack Count 162 Destroy Stack 162
4-3 Stack Applications 163 Reversing Data 163
Reverse a List 164 Convert Decimal to Binary 164
Parsing 165 Postponement 167
Infix To Postfix Transformation 167 Evaluating Postfix Expressions 1 73
Backtracking 175 Goal Seeking 175 Eight Queens Problem 178
4-4 Eight Queens Problem—С Implementation 181
Mainline Logic 182 Get Board Size 183 Fill Board 183 Guarded 185 Print Board 186
4-5 ADT—Linked-List Implementation 188
Data Structure 188 Stack ADT Implementation 188
Stack Structure 188
Create Stack 190 Push Stack 190 Pop Stack 192 Stack Top 193 Empty Stack 193 Full Stack 194 Stack Count 194 Destroy Stack 195
4-6 Array Implementation of Stacks 196
Data Structures 196 Array Algorithms 197
Create Stack 197 Push Stack 198 Pop Stack 198 Stack Top 199 Empty Stack 200 Full Stack 200 Stack Count 201 Destroy Stack 201
4-7 ADT—Array Implementation 202 Create Stack Array 202 Push Stack Array 203 Pop Stack Array 204 Stack Top Array 205 Empty Stack Array 205 Full Stack Array 206 Stack Count Array 206 Destroy Stack Array 206
4-8 Summary 2 0 7 4-9 Practice Sets 2 0 8
Exercises 208 Problems 209 Projects 211
5 Queues 215 5-1 Queue Operations 2 1 6
Enqueue 216 Dequeue 216 Queue Front 217 Queue Rear 217 Queue Example 218
5-2 Queue—Linked-List Implementation 2 1 8
Data Structure 218 Queue Head 218 Queue Data Node 220
Queue Algorithms 220
viii Contents
Create Queue 223 Enqueue 224 Dequeue 225 Retrieving Queue Data 226 Empty Queue 227 Full Queue 227 Queue Count 228 Destroy Queue 228
5-3 Queuing Theory 2 2 9 5-4 Queue Applications 2 3 0
Queue Simulation 231 Events 231 Data Structures 232 Output 233 Simulation Algorithm 233
Categorizing Data 238 5-5 Categorizing Data—С
Implementation 2 4 0 Mainline Logic 240 FillQueues 241 Print Queues 242 Print One Queue 243
5-6 Queue—Linked-List Implementation 2 4 4
Queue Structure 244 Queue ADT Implementation 245
Create Queue 245 Enqueue 246 Dequeue 247 Queue Front 248 Queue Rear 249 Queue Empty 249 Full Queue 250 Queue Count 250 Destroy Queue 251
5-7 Queues—Array Implementation 2 5 2
Array Queues Implementation 253 Create Queue Array 255 Enqueue: Array
Implementation 256 Dequeue: Array
Implementation 257 Queue Front: Array
Implementation 258 Queue Rear: Array
Implementation 259 Full Queue: Array
Implementation 259
Destroy Queue: Array Implementation 260
5-8 Summary 2 6 0 5-9 Practice Sets 2 6 1
Exercises 261 Problems 263 Projects 264
6 Recursion 269 6-1 Factorial—A Case Study 2 7 0
Recursion Defined 270 Iterative Solution 271 Recursive Solution 271
6-2 How Recursion Works 2 7 3 6-3 Designing Recursive
Algorithms 2 7 5 The Design Methodology 276 Limitations of Recursion 277 Design Implementation — Reverse A
Linked List 277 6-4 Another Case Study—Fibonacci
Numbers 2 8 0 6-5 The Towers Of Hanoi 2 8 2
Recursive Towers Of Hanoi 284 6-6 С Implementations of
Recursion 2 8 8 Fibonacci Numbers 288 Towers of Hanoi 289
6-7 Summary 2 9 1 6-8 Practice Sets 2 9 2
Exercises 292 Problems 294 Projects 295
7 Introduction to Trees 297 7-1 Basic Tree Concepts 2 9 8
Terminology 298 Tree Representation 300
7-2 Binary Trees 3 0 2 Properties 304
Height of Binary Trees 304 Balance 304 Complete Binary Trees 305
Binary Tree Structure 305 7-3 Binary Tree Traversais 3 0 6
Depth-First Traversals 306
Contents ix
Preorder Traversal (NbR) 307 Inorder Traversal (LNR) 309 Postorder Traversal (LRN) 310
Breadth Traversal 312 7-4 Expression Trees 3 1 3
Infix Traversal 313 Postfix Traversal 316 Prefix Traversal 316
7-5 General Trees 3 1 6 Changing General Tree To Binary
Tree 317 Insertions Into General Trees 318
FIFO Insertion 318 LIFO Insertion 318 Key Sequenced Insertion 318
General Tree Deletions 319 7-6 Summary 3 2 0 7-7 Practice Sets 3 2 1
Exercises 321 Problems 325 Projects 325
8 Search Trees 333 8-1 Binary Search Trees 3 3 4
Definition 334 Operations On Binary Search
Trees 336 Binary Search Tree Traversals 336 Binary Search Tree Search
Algorithms 337 Insert Node 339
8-2 AVL Trees 3 4 8 AVL Balance Factor 349 Balancing Trees 350
Case 1: Left Of Left 352 Case 2: Right Of Right 353 Case 3: Right Of Left 354 Case 4: Left Of Right 354
AVL Node Structure 354 AVL Insert 355
AVL Insert Algorithm 358 AVL Left Balance Algorithm 359 Rotate Algorithms 360
AVL Delete Algorithm 361 Delete Right Balance 363 Adjusting The Balance Factors 365
8-3 AVL Abstract Data Type 3 6 6
AVL Data Structure 368 AVL Algorithms 369
Create AVL Tree 369 AVL Tree Insert 370 AVL Tree Delete 376
AVL Tree Data Processing 382 Retrieve AVL Tree Node 382 Traverse AVL Tree 384
AVL Tree Utility Functions 384 AVL Empty Tree 385 AVL Full Tree 385 AVL Count 385 Destroy AVL Tree 386
8-4 Summary 3 8 7 8-5 Practice Sets 3 8 8
Exercises 388 Problems 390 Projects 392
9 Heaps 395 9-1 Heap Definition 3 9 6 9-2 Heap Structure 3 9 6 9-3 Basic Heap Algorithms 3 9 7
ReheapUp 398 ReheapDown 400
9-4 Heap Data Structure 4 0 1 9-5 Heap Algorithms 4 0 3
ReheapUp 403 ReheapDown 404 Build Heap 405 Insert Heap 406 Delete Heap 407
9-6 Heap Applications 4 1 0 Selection Algorithms 410 Priority Queues 410
9-7 A Heap Program 4 1 3 Heap Program Design 413 Heap Functions 419
ReheapUp 419 ReheapDown 420 Insert Heap 421 Delete Heap Node 421
9-8 Summary 4 2 2 9-9 Practice Sets 4 2 3
Exercises 423 Problems 425 Projects 425
X Contents
10 Multiway Trees 429 lO-l М-Way Search Trees 4 3 0 10-2 B-Trees 4 3 1
B-Tree Insertion 433 B-Tree Insert Design 434 B-Tree Insert Node 436 Search Node 438 Split Node 439 Insert Entry 442 Insertion Summary 442
B-Tree Deletion 442 B-Tree Delete 445 Node Delete 445 Delete Entry 447 Delete Mid 447 ReFlow 448 Balance 451 Combine 453
Traverse B-Tree 455 B-Tree Search 458
10-3 Simplified B-Trees 4 5 9 2-3 Tree 459 2-3-4 Tree 460
10-4 B-tree Variations 4 6 1 B*Tree 461 В + Tree 461
10-5 Lexical Search Tree 4 6 1 Tries 463 Trie Structure 464 Trie Search 464
10-6 B-Tree Abstract Data Type 4 6 6 Header File 466 Utility Functions 468
Search 468 Traversal 470
Insert Algorithms 472 ADT Interface Function 4 72 Internal Insert Processing 474
Delete Algorithms 478 ADT Delete Interface 478 Delete Internal Function 479 Delete Middle 481 Reflow Function 482 Borrow Left or Right 484 Combine 485
10-7 Summary 4 8 6 10-8 Practice Sets 4 8 7
Exercises 487
Problems 487 Projects 488
11 Advanced Sorting Concepts 491 11-1 General Sort Concepts 4 9 2
Sort Order 492 Sort Stability 493 Sort Efficiency 494 Passes 494
11-2 Insertion Sorts 4 9 4 Straight Insertion Sort 494
Straight Insertion Sort Example 494
Insertion Sort Algorithm 495 Shell Sort 497
Shell Sort Algorithm 497 Selecting the Increment Size 501
Insertion Sort Algorithmics 501 Straight Insertion Sort 501 Shell Sort 502 Summary 503
Insertion Sort Implementation 503 Straight Insertion Sort 503 Shell Sort 504
11-3 Selection Sorts 5 0 6 Straight Selection Sort 506
Selection Sort Algorithm 506 Heap Sort 508
Heap Sort Algorithm 509 Selection Sort Algorithmics 510
Straight Selection Sort 510 Heap Sort 510 Summary 512
Selection Sort Implementation 513 Selection Sort С Code 513 Heap Sort С Code 513
11-4 Exchange Sorts 5 1 4 Bubble Sort 515
Bubble Sort Algorithm 515 Quicksort 517
Quick Sort Algorithm 519 Exchange Sort Algorithmics 524
Bubble Sort 524 Quick Sort 525 Summary 526
Contents x i
Exchange Sort Implementation 526 Bubble Sort Code 526 Quick Sort Code 527
11-5 External Sorts 5 3 0 Merging Ordered Files 530 Merging Unordered Files 532 The Sorting Process 533
Natural Merge 535 Balanced Merge 535 Polyphase Merge 535
Sort Phase Revisited 539 11-6 Summary 5 4 1 11-7 Practice Sets 5 4 2
Exercises 542 Problems 544 Projects 545
12 Graphs 549 12-1 Terminology 5 5 0 12-2 Operations 5 5 2
Add Vertex 552 Delete Vertex 552 Add Edge 553 Delete Edge 553 Find Vertex 553 Traverse Graph 553
Depth-First Traversal 554 Breadth-First 555
12-3 Graph Storage Structures 5 5 7 Adjacency Matrix 557 Adjacency List 558
12-4 Graph Algorithms 5 5 9 Create Graph 563 Insert Vertex 563 Delete Vertex 565 Insert Arc 566 Delete Arc 567 Retrieve Vertex 569 First Arc 569 Depth-First Traversal 570 Breadth-First Traversal 572
12-5 Networks 5 7 4 Minimum Spanning Tree 575
Minimum Spanning Tree Example 576
Minimum Spanning Tree Data Structure 577
Minimum Spanning Tree Pseudocode 578
Shortest Path Algorithm 580 Shortest Path —Manual
Example 580 Shortest Path Data Structure 582 Shortest Path Pseudocode 583
12-6 Abstract Data Type 5 8 4 Insert Vertex 586 Delete Vertex 587 Insert Arc 589 Delete Arc 590 Depth-First Traversal 592 Breadth-First Traversal 594
12-7 Summary 5 9 6 12-8 Practice Sets 5 9 7
Exercises 597 Problems 598 Projects 599
Appendix A ASCII Tables 603 A-l ASCII Codes (Long Form) 6 0 3 A-2 ASCII Table (Short Form) 6 0 7
Appendix В Structure Charts 609 B-l Structure Chart Symbols 6 1 0
Modules 610 Reading Structure Charts 612 Common Modules 612 Conditional Calls 613 Exclusive Or 613 Loops 613 Conditional Loops 614 Recursion 614 Data Flows and Flags 615
B-2 Structure Chart Rules 6 1 5
Appendix С Program Standards and Styles 617
C-l Global Area 6 1 8 C-2 Program Main Line 6 1 9 C-3 General Coding Standards 6 1 9
xii Contents
C-4 Variables and Structures 6 2 0 C-5 Function Definition 6 2 1
Appendix D Random Numbers 623
D-l Random Numbers In С 6 2 4 Srand 624 Rand 624
D-2 Custom Random Number Generator 6 2 4
D-3 Scaling a Random Number 6 2 5
Appendix E Standard С Libraries 629
Б-l Limits.H 6 2 9 E-2 Float.H 6 3 0
Appendix F С Function Prototypes 631
F-l Function Index 6 3 2 F-2 Character Library 6 3 3
F-3 Math Library 6 3 3 F-4 Standard I/O Library 6 3 4
General I/O 634 Formatted I/O 634 Character I/O 634 File I/O 635 System Communication 635 String I/O 635 System File Control 635
F-5 Standard Library 6 3 5 Math Functions 635 Memory Functions 635 Program Control 636 System Communication 636 Conversion Functions 636
F-6 String Library 6 3 6 Copying Data 636 Comparing Data 636 String Lengths 636
F-7 Time 6 3 7
Solutions to Selected Exercises 6 3 9
Glossary 6 5 5
Index 665