Table Of ContentData Structures: A Pseudocode
Approach with C,
Second Edition
Richard F. Gilberg & Behrouz A. Forouzan
This is an electronic version of the print textbook. Due to electronic rights restrictions,
some third party content may be suppressed. Editorial review has deemed that any suppressed
content does not materially affect the overall learning experience. The publisher reserves the right
to remove content from this title at any time if subsequent rights restrictions require it. For
valuable information on pricing, previous editions, changes to current editions, and alternate
formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for
materials in your areas of interest.
Data Structures: A Pseudocode Approach with C, Second Edition
Richard F. Gilberg
Behrouz A. Forouzan
Senior Product Manager: Senior Marketing Manager: Senior Manufacturing Coordinator:
Alyssa Pratt Karen Seitz Trevor Kallop
Senior Acquisitions Editor: Associate Product Manager: Cover Designer:
Amy Yarnevich Mirella Misiaszek Abby Scholz
Production Editor: Editorial Assistant:
BobbiJo Frasca Amanda Piantedosi
COPYRIGHT © 2005 Course Technology, a division taping, Web distribution, or information storage and
of Thomson Learning, Inc. Thomson Learning™ is a retrieval systems—without the written permission of the
trademark used herein under license. publisher.
Printed in the United States of America For permission to use material from this text
or product, submit a request online at
1 2 3 4 5 6 7 8 9 QWT 09 08 07 06 05 http://www.thomsonrights.com
For more information, contact Course Technology, Any additional questions about permissions can be
25 Thomson Place, Boston, Massachusetts, 02210. submitted by email to thomsonrights@thomson.com
Or find us on the World Wide Web at: Disclaimer
www.course.com Course Technology reserves the right to revise this
publication and make changes from time to time in
ALL RIGHTS RESERVED. No part of this work covered its content without notice.
by the copyright hereon may be reproduced or used in
any form or by any means—graphic, electronic, or ISBN-13: 978-0-534-39080-8
mechanical, including photocopying, recording, ISBN-10: 0-534-39080-3
Dedication Page
To my wife Evelyn for her support over the years.
Richard Gilberg
To Mark Bauer for his help when I needed it.
Behrouz Forouzan
Contents
Part 1 Introduction 1 Nested Loops 30
Linear Logarithmic 31
Chapter 1 Basic Concepts 5
Quadratic 31
1.1 Pseudocode 5 Dependent Quadratic 31
Algorithm Header 6 Big-O Notation 32
Purpose, Conditions, and Return 7 Standard Measures of Efficiency 33
Statement Numbers 7 Big-O Analysis Examples 35
Variables 8 Add Square Matrices 35
Statement Constructs 8 Multiply Square Matrices 36
Sequence 8 1.7 Key Terms 38
Selection 9
1.8 Summary 38
Loop 9
Algorithm Analysis 9 1.9 Practice Sets 39
Pseudocode Example 9 Exercises 39
Problems 41
1.2 The Abstract Data Type 10
Projects 42
Atomic and Composite Data 10
Data Type 11 Chapter 2 Recursion 45
Data Structure 11 2.1 Factorial—A Case Study 45
Abstract Data Type 12 Recursion Defined 46
1.3 Model for an Abstract Data Type 14 Iterative Solution 47
ADT Operations 14 Recursive Solution 47
ADT Data Structure 15 2.2 Designing Recursive Algorithms 48
1.4 ADT Implementations 15 The Design Methodology 48
Array Implementations 15 Limitations of Recursion 50
Linked List Implementations 15 Design Implementation—Reverse
Pointers to Linked Lists 17 Keyboard Input 50
1.5 Generic Code for ADTs 17 2.3 Recursive Examples 52
Pointer to void 18 Greatest Common Divisor 52
Pointer to Function 23 GCD Design 53
Defining Pointers to Functions 24 GCD C Implementation 53
Using Pointers to Functions 24 Fibonacci Numbers 54
Design 55
1.6 Algorithm Efficiency 28
Fibonacci C Implementation 56
Linear Loops 29
Logarithmic Loops 29
iv
Contents v
Prefix to Postfix Conversion 57 Push Stack 97
Design 58 Pop Stack 98
Prefix to Postfix Stack Top 99
C Implementation 61 Empty Stack 100
The Towers of Hanoi 65 Stack Count 101
Recursive Towers of Hanoi Design 65 Destroy Stack 101
Towers of Hanoi 3.5 Stack Applications 102
C Implementation 69 Reversing Data 103
2.4 Key Terms 71 Reverse a List 103
Convert Decimal to Binary 106
2.5 Summary 71
Parsing 107
2.6 Practice Sets 72
Postponement 110
Exercises 72
Infix to Postfix Transformation 110
Problems 74
Evaluating Postfix Expressions 118
Projects 75
Backtracking 122
Part II Linear Lists 77 Goal Seeking 122
Eight Queens Problem 125
Chapter 3 Stacks 79
3.6 How Recursion Works 134
3.1 Basic Stack Operations 80 3.7 Key Terms 138
Push 80
3.8 Summary 138
Pop 80
3.9 Practice Sets 139
Stack Top 81
Exercises 139
3.2 Stack Linked List
Problems 141
Implementation 83
Projects 143
Data Structure 83
Chapter 4 Queues 147
Stack Head 83
Stack Data Node 83 4.1 Queue Operations 148
Stack Algorithms 84 Enqueue 148
Create Stack 84 Dequeue 148
Push Stack 85 Queue Front 149
Pop Stack 86 Queue Rear 150
Stack Top 87 Queue Example 150
Empty Stack 88
4.2 Queue Linked List Design 151
Full Stack 89
Data Structure 152
Stack Count 89
Queue Head 152
Destroy Stack 89
Queue Data Node 153
3.3 C Language Implementations 90 Queue Algorithms 153
Insert Data 92 Create Queue 154
Push Stack 93 Enqueue 154
Print Stack 93 Dequeue 155
Pop Character 94 Retrieving Queue Data 156
3.4 Stack ADT 95 Empty Queue 157
Data Structure 95 Full Queue 157
ADT Implementation 95 Queue Count 158
Stack Structure 95 Destroy Queue 158
Create Stack 96
vi Contents
4.3 Queue ADT 159 Internal Insert Function 217
Queue Structure 159 Remove Node 219
Queue ADT Algorithms 159 Internal Delete Function 220
Search List 221
4.4 Queuing Theory 166
Internal Search Function 222
4.5 Queue Applications 168
Retrieve Node 224
Categorizing Data 168
Empty List 225
Categorizing Data Design 169
Full List 225
Categorizing Data—
List Count 226
C Implementation 170
Traverse 226
Queue Simulation 175
Destroy List 227
Events 176
5.4 Application 228
Data Structures 176
Data Structure 228
Output 177
Application Functions 229
Simulation Algorithm 178
Mainline 229
4.6 Key Terms 183
Print Instructions 229
4.7 Summary 183 Build List 231
4.8 Practice Sets 184 Process User Requests 233
Exercises 184 Get User Choice 234
Problems 187 Print List 235
Projects 188 Search List 236
Compare Year 237
Chapter 5 General Linear Lists 193
Testing Insert and Delete Logic 238
5.1 Basic Operations 193 Testing Insert Logic 238
Insertion 193 Testing Delete Logic 238
Deletion 194
5.5 Complex Implementations 239
Retrieval 194
Circularly Linked Lists 239
Traversal 194
Doubly Linked Lists 240
5.2 Implementation 195 Insertion 241
Data Structure 195 Deletion 243
Head Node 196 Multilinked Lists 244
Data Node 196 Insert 246
Algorithms 197 Delete 247
Create List 197
5.6 Key Terms 248
Insert Node 198
Delete Node 202 5.7 Summary 248
List Search 205 5.8 Practice Sets 249
Retrieve Node 208 Exercises 249
Empty List 208 Problems 251
Full List 209 Projects 253
List Count 210
Part III Non-Linear Lists 263
Traverse List 210
Destroy List 212
Chapter 6 Introduction to Trees 265
5.3 List ADT 213
6.1 Basic Tree Concepts 265
ADT Functions 214
Terminology 266
Create List 216
User Representation 268
Add Node 217
Contents vii
6.2 Binary Trees 270 Internal Insert Function 314
Properties 271 Delete a BST 315
Height of Binary Trees 271 Internal Delete Function 316
Balance 272 Retrieve a BST 318
Complete and Nearly Complete Internal Retrieve Function 319
Binary Trees 273 Traverse a BST 320
Binary Tree Traversals 273 Internal Traverse Function 321
Depth-first Traversals 274 Empty a BST 321
Breadth-first Traversals 278 Full BST 322
Expression Trees 280 BST Count 322
Infix Traversal 280 Destroy a BST 323
Postfix Traversal 281 Internal Destroy Function 323
Prefix Traversal 282 7.4 BST Applications 324
Huffman Code 282 Integer Application 324
6.3 General Trees 287 Student List Application 328
Insertions into General Trees 287 Program Design 329
FIFO Insertion 287 7.5 Threaded Trees 334
LIFO Insertion 287
7.6 Key Terms 336
Key-sequenced Insertion 287
7.7 Summary 336
General Tree Deletions 288
Changing a General Tree to a 7.8 Practice Sets 337
Binary Tree 288 Exercises 337
Problems 339
6.4 Key Terms 290
Projects 339
6.5 Summary 290
Chapter 8 AVL Search Trees 341
6.6 Practice Sets 292
Exercises 292 8.1 AVL Tree Basic Concepts 341
Problems 295 AVL Tree Balance Factor 342
Projects 296 Balancing Trees 343
Case 1: Left of Left 344
Chapter 7 Binary Search Trees 299
Case 2: Right of Right 345
7.1 Basic Concepts 299 Case 3: Right of Left 346
7.2 BST Operations 301 Case 4: Left of Right 347
Traversals 301 8.2 AVL Tree Implementations 348
Searches 302 Insert into AVL Tree 348
Find the Smallest Node 302 AVL Tree Insert Algorithm 350
Find the Largest Node 303 AVL Tree Left Balance
BST Search 303 Algorithm 351
Insertion 305 Rotate Algorithms 351
Deletion 307 AVL Tree Delete Algorithm 352
7.3 Binary Search Tree ADT 309 Delete Right Balance 354
Data Structure 311 Adjusting the Balance Factors 355
Head Structure 311 8.3 AVL Tree Abstract Data Type 356
Node Structure 311 AVL Tree Data Structure 357
Algorithms 311 Head Structure 357
Create a BST 313 Node Structure 357
Insert a BST 313
viii Contents
AVL Tree Algorithms 357 Internal Reheap Up Function 405
Create an AVL Tree 359 Delete a Heap 405
Insert an AVL Tree 360 Internal Reheap Down
Delete an AVL Tree 365 Function 406
Retrieve an AVL Tree 370 9.4 Heap Applications 407
Traverse an AVL Tree 372 Selection Algorithms 408
Internal Traverse Function 373 Priority Queues 409
Empty an AVL Tree 373 Design 409
Full AVL Tree 374 Implementation 411
AVL Tree Count 374
9.5 Key Terms 417
Destroy AVL Tree 375
9.6 Summary 417
Internal Destroy Function 375
9.7 Practice Sets 418
8.4 Application—Count Words 376
Exercises 418
Data Structure 376
Problems 420
Program Design 376
Projects 420
Count Words Program 377
Build List 378 Chapter 10 Multiway Trees 423
Get Word 380
10.1 M-way Search Trees 423
Compare Words 381
10.2 B-trees 425
Print Words 382
B-tree Implementation 427
8.5 Key Terms 384
B-tree Insertion 427
8.6 Summary 384 Insert Node 429
8.7 Practice Sets 384 Search Node 432
Exercises 384 Split Node 433
Problems 386 Insertion Summary 435
Projects 386 B-tree Deletion 435
Delete Node 437
Chapter 9 Heaps 389
Delete Entry 439
9.1 Basic Concepts 389 Delete Mid 439
Definition 389 Reflow 440
Maintenance Operations 391 Balance 442
Reheap Up 391 Combine 444
Reheap Down 392 Traverse B-tree 446
9.2 Heap Implementation 394 B-tree Search 448
Algorithms 396 10.3 B-tree ADT 449
Reheap Up 396 B-tree Data Structure 449
Reheap Down 396 Head Structure 449
Build a Heap 397 Node Structure 450
Insert a Node into a Heap 398 Header File 451
Delete a Node from a Heap 400 Algorithms 453
9.3 Heap ADT 401 B-tree Search 453
Heap Structure 401 Internal Search Function 454
Heap Algorithms 402 B-tree Traverse 455
Create a Heap 403 Internal Traverse Function 456
Insert a Heap 404 B-tree Insert 457
Description:This second edition expands upon the solid, practical foundation established in the first edition of the text. A new four-part organizational structure increases the flexibility of the text, and all material is presented in a straightforward manner accompanied by an array of examples and visual diag