Data Structures and Algorithms in C++

Similar documents
Exam study sheet for CS2711. List of topics

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

DATA STRUCTURES USING C

Converting a Number from Decimal to Binary

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Java Software Structures

Data Structure [Question Bank]

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

PES Institute of Technology-BSC QUESTION BANK

AP Computer Science AB Syllabus 1

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Algorithms and data structures

BCS2B02: OOP Concepts and Data Structures Using C++

Programming Using Python

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

10CS35: Data Structures Using C

An Introduction to Programming and Computer Science

University of Pune Revised Structure for the B. Sc. (Computer Science) Course (Second Year to be implemented from Academic Year )

Binary Heap Algorithms

Sample Syllabus (C++) CSCI 1301 Introduction to Programming Principles

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

PARALLEL PROGRAMMING

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Sample Questions Csci 1112 A. Bellaachia

Introduction to Programming System Design. CSCI 455x (4 Units)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Heaps & Priority Queues in the C++ STL 2-3 Trees

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Persistent Binary Search Trees

DIVISION OF INFORMATION TECHNOLOGY PROGRAMS AND SYSTEMS SUPPORT FALL 2015 / Spring 2016

Binary Search Trees CMPSC 122

Computing Concepts with Java Essentials

Basic Programming and PC Skills: Basic Programming and PC Skills:

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

How To Teach C++ Data Structure Programming

Formal Engineering for Industrial Software Development

Data Structures and Algorithms Written Examination

ML for the Working Programmer

Mathematics for Algorithm and System Analysis

Glossary of Object Oriented Terms

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Structural Design Patterns Used in Data Structures Implementation

CS5310 Algorithms 3 credit hours 2 hours lecture and 2 hours recitation every week

Curriculum Map. Discipline: Computer Science Course: C++

Sequences in the C++ STL

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Chapter 8: Bags and Sets

Data Structures. Level 6 C Module Descriptor

Computer. Course Description

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

1 FCS Project Submission System

Fast Sequential Summation Algorithms Using Augmented Data Structures

Linked Lists: Implementation Sequences in the C++ STL

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Dynamic Programming. Lecture Overview Introduction

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

A Comparison of Dictionary Implementations

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

FOPR-I1O23 - Fundamentals of Programming

Social Media Mining. Graph Essentials

Analysis of a Search Algorithm

Class Notes for CSCI 104: Data Structures and Object-Oriented Design

Using Web-based Tools to Enhance Student Learning and Practice in Data Structures Course

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

AGARWAL COLLEGE & WEB SERVICES TRUST. In Collaboration with KARANATAKA STATE OPEN UNIVERSITY

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

1 p a g e 1 1. Syllabus of 3 rd Semester of B.Tech. Information Technology (Batch 2014 Onwards)

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

Algorithm Homework and Test Problems

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Java EE Web Development Course Program

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Recursive Implementation of Recursive Data Structures

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Java Application Developer Certificate Program Competencies

Programming Language Pragmatics

Please consult the Department of Engineering about the Computer Engineering Emphasis.

Computer Science/Software Engineering

Android Application Development Course Program

COMPUTER SCIENCE, BACHELOR OF SCIENCE (B.S.)

CompSci-61B, Data Structures Final Exam

Open Data Structures (in pseudocode)

Node-Based Structures Linked Lists: Implementation

Universidad Carlos III de Madrid

Introduction to Algorithms and Data Structures

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Syllabus for Computer Science. Proposed scheme for B.Sc Programme under Choice Based Credit System

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Transcription:

Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich Department of Computer Science University of California, Irvine Roberto Tamassia Department of Computer Science Brown University David M. Mount Department of Computer Science University of Maryland John Wiley & Sons, Inc.

Contents 1 A C++ Primer 1 1.1 Basic C++ Programming Elements 2 1.1.1 A Simple C++ Program 2 1.1.2 Fundamental Types 4 1.1.3 Pointers, Arrays, and Structures 7 1.1.4 Named Constants, Scope, and Namespaces 13 1.2 Expressions 16 1.2.1 Changing Types through Casting 20 1.3 Control Flow 23 1.4 Functions 26 1.4.1 Argument Passing 28 1.4.2 Overloading and Inlining 30 1.5 Classes 32 1.5.1 Class Structure 33 1.5.2 Constructors and Destructors 37 1.5.3 Classes and Memory Allocation 40 1.5.4 Class Friends and Class Members 43 1.5.5 The Standard Template Library 45 1.6 C++ Program and File Organization 47 1.6.1 An Example Program 48 1.7 Writing a C++ Program 53 1.7.1 Design 54 1.7.2 Pseudo-Code 54 1.7.3 Coding 55 1.7.4 Testing and Debugging 57 1.8 Exercises 60 2 Object-Oriented Design 65 2.1 Goals, Principles, and Patterns 66 2.1.1 Object-Oriented Design Goals 66 2.1.2 Object-Oriented Design Principles 67 2.1.3 Design Patterns 70 xv

xvi Contents 2.2 Inheritance and Polymorphism 71 2.2.1 Inheritance in C++ 71 2.2.2 Polymorphism 78 2.2.3 Examples of Inheritance in C++ 79 2.2.4 Multiple Inheritance and Class Casting 84 2.2.5 Interfaces and Abstract Classes 87 2.3 Templates 90 2.3.1 Function Templates 90 2.3.2 Class Templates 91 2.4 Exceptions 93 2.4.1 Exception Objects 93 2.4.2 Throwing and Catching Exceptions 94 2.4.3 Exception Specification 96 2.5 Exercises 98 3 Arrays, Linked Lists, and Recursion 103 3.1 Using Arrays 104 3.1.1 Storing Game Entries in an Array 104 3.1.2 Sorting an Array 109 3.1.3 Two-Dimensional Arrays and Positional Games Ill 3.2 Singly Linked Lists 117 3.2.1 Implementing a Singly Linked List 117 3.2.2 Insertion to the Front of a Singly Linked List 119 3.2.3 Removal from the Front of a Singly Linked List 119 3.2.4 Implementing a Generic Singly Linked List 121 3.3 Doubly Linked Lists 123 3.3.1 Insertion into a Doubly Linked List 123 3.3.2 Removal from a Doubly Linked List 124 3.3.3 A C++ Implementation 125 3.4 Circularly Linked Lists and List Reversal 129 3.4.1 Circularly Linked Lists 129 3.4.2 Reversing a Linked List 133 3.5 Recursion 134 3.5.1 Linear Recursion 140 3.5.2 Binary Recursion 144 3.5.3 Multiple Recursion 147 3.6 Exercises 149 4 Analysis Tools 153 4.1 The Seven Functions Used in This Book 154 4.1.1 The Constant Function 154 4.1.2 The Logarithm Function 154

Contents xvii 4.1.3 The Linear Function 156 4.1.4 The N-Log-N Function 156 4.1.5 The Quadratic Function 156 4.1.6 The Cubic Function and Other Polynomials 158 4.1.7 The Exponential Function 159 4.1.8 Comparing Growth Rates 161 4.2 Analysis of Algorithms 162 4.2.1 Experimental Studies 163 4.2.2 Primitive Operations 164 4.2.3 Asymptotic Notation 166 4.2.4 Asymptotic Analysis 170 4.2.5 Using the Big-Oh Notation 172 4.2.6 A Recursive Algorithm for Computing Powers 176 4.2.7 Some More Examples of Algorithm Analysis 177 4.3 Simple Justification Techniques 181 4.3.1 By Example 181 4.3.2 The "Contra" Attack 181 4.3.3 Induction and Loop Invariants 182 4.4 Exercises 185 5 Stacks, Queues, and Deques 193 5.1 Stacks 194 5.1.1 The Stack Abstract Data Type 195 5.1.2 The STL Stack 196 5.1.3 A C++ Stack Interface 196 5.1.4 A Simple Array-Based Stack Implementation 198 5.1.5 Implementing a Stack with a Generic Linked List... 202 5.1.6 Reversing a Vector Using a Stack 203 5.1.7 Matching Parentheses and HTML Tags 204 5.2 Queues 208 5.2.1 The Queue Abstract Data Type 208 5.2.2 The STL Queue 209 5.2.3 A C++ Queue Interface 210 5.2.4 A Simple Array-Based Implementation 211 5.2.5 Implementing a Queue with a Circularly Linked List... 213 5.3 Double-Ended Queues 217 5.3.1 The Deque Abstract Data Type 217 5.3.2 The STL Deque 218 5.3.3 Implementing a Deque with a Doubly Linked List... 218 5.3.4 Adapters and the Adapter Design Pattern 220 5.4 Exercises 223

xviii Contents 6 List and Iterator ADTs 227 6.1 Vectors 228 6.1.1 The Vector Abstract Data Type 228 6.1.2 A Simple Array-Based Implementation 229 6.1.3 An Extendable Array Implementation 231 6.1.4 STL Vectors 236 6.2 Lists 238 6.2.1 Node-Based Operations and Iterators 238 6.2.2 The List Abstract Data Type 240 6.2.3 Doubly Linked List Implementation 242 6.2.4 STL Lists 247 6.2.5 STL Containers and Iterators 248 6.3 Sequences 255 6.3.1 The Sequence Abstract Data Type 255 6.3.2 Implementing a Sequence with a Doubly Linked List.. 255 6.3.3 Implementing a Sequence with an Array 257 6.4 Case Study: Bubble-Sort on a Sequence 259 6.4.1 The Bubble-Sort Algorithm 259 6.4.2 A Sequence-Based Analysis of Bubble-Sort 260 6.5 Exercises 262 7 Trees 267 7.1 General Trees 268 7.1.1 Tree Definitions and Properties 269 7.1.2 Tree Functions 272 7.1.3 A C++ Tree Interface 273 7.1.4 A Linked Structure for General Trees 274 7.2 Tree Traversal Algorithms 275 7.2.1 Depth and Height 275 7.2.2 Preorder Traversal 278 7.2.3 Postorder Traversal 281 7.3 Binary Trees 284 7.3.1 The Binary Tree ADT 285 7.3.2 A C++ Binary Tree Interface 286 7.3.3 Properties of Binary Trees 287 7.3.4 A Linked Structure for Binary Trees 289 7.3.5 A Vector-Based Structure for Binary Trees 295 7.3.6 Traversals of a Binary Tree 297 7.3.7 The Template Function Pattern 303 7.3.8 Representing General Trees with Binary Trees 309 7.4 Exercises 310

Contents xix 8 Heaps and Priority Queues 321 8.1 The Priority Queue Abstract Data Type 322 8.1.1 Keys, Priorities, and Total Order Relations 322 8.1.2 Comparators 324 8.1.3 The Priority Queue ADT 327 8.1.4 A C++ Priority Queue Interface 328 8.1.5 Sorting with a Priority Queue 329 8.1.6 The STL priority.queue Class 330 8.2 Implementing a Priority Queue with a List 331 8.2.1 A C++ Priority Queue Implementation using a List... 333 8.2.2 Selection-Sort and Insertion-Sort 335 8.3 Heaps 337 8.3.1 The Heap Data Structure 337 8.3.2 Complete Binary Trees and Their Representation... 340 8.3.3 Implementing a Priority Queue with a Heap 344 8.3.4 C++ Implementation 349 8.3.5 Heap-Sort 351 8.3.6 Bottom-Up Heap Construction ~k 353 8.4 Adaptable Priority Queues 357 8.4.1 A List-Based Implementation 358 8.4.2 Location-Aware Entries 360 8.5 Exercises 361 9 Hash Tables, Maps, and Skip Lists 367 9.1 Maps 368 9.1.1 The Map ADT 369 9.1.2 A C++ Map Interface 371 9.1.3 The STL map Class 372 9.1.4 A Simple List-Based Map Implementation 374 9.2 Hash Tables 375 9.2.1 Bucket Arrays 375 9.2.2 Hash Functions 376 9.2.3 Hash Codes 376 9.2.4 Compression Functions 380 9.2.5 Collision-Handling Schemes 382 9.2.6 Load Factors and Rehashing 386 9.2.7 A C++ Hash Table Implementation 387 9.3 Ordered Maps 394 9.3.1 Ordered Search Tables and Binary Search 395 9.3.2 Two Applications of Ordered Maps 399 9.4 Skip Lists 402

xx Contents 9.4.1 Search and Update Operations in a Skip List 404 9.4.2 A Probabilistic Analysis of Skip Lists * 408 9.5 Dictionaries 411 9.5.1 The Dictionary ADT 411 9.5.2 A C++ Dictionary Implementation 413 9.5.3 Implementations with Location-Aware Entries 415 9.6 Exercises 417 10 Search Trees 423 10.1 Binary Search Trees 424 10.1.1 Searching 426 10.1.2 Update Operations 428 10.1.3 C++Implementation of a Binary Search Tree 432 10.2 AVL Trees 438 10.2.1 Update Operations 440 10.2.2 C++ Implementation of an AVL Tree 446 10.3 Splay Trees 450 10.3.1 Splaying 450 10.3.2 When to Splay 454 10.3.3 Amortized Analysis of Splaying ~k 456 10.4 (2,4) Trees 461 10.4.1 Multi-Way Search Trees 461 10.4.2 Update Operations for (2,4) Trees 467 10.5 Red-Black Trees 473 10.5.1 Update Operations 475 10.5.2 C++ Implementation of a Red-Black Tree 488 10.6 Exercises 492 11 Sorting, Sets, and Selection 499 11.1 Merge-Sort 500 11.1.1 Divide-and-Conquer 500 11.1.2 Merging Arrays and Lists 505 11.1.3 The Running Time of Merge-Sort 508 11.1.4 C++ Implementations of Merge-Sort 509 11.1.5 Merge-Sort and Recurrence Equations "A" 511 11.2 Quick-Sort 513 11.2.1 Randomized Quick-Sort 521 11.2.2 C++ Implementations and Optimizations 523 11.3 Studying Sorting through an Algorithmic Lens 526 11.3.1 A Lower Bound for Sorting 526 11.3.2 Linear-Time Sorting: Bucket-Sort and Radix-Sort... 528 11.3.3 Comparing Sorting Algorithms 531

Contents xxi 11.4 Sets and Union/Find Structures 533 11.4.1 The Set ADT 533 11.4.2 Mergable Sets and the Template Method Pattern... 534 11.4.3 Partitions with Union-Find Operations 538 11.5 Selection 542 11.5.1 Prune-and-Search 542 11.5.2 Randomized Quick-Select 543 11.5.3 Analyzing Randomized Quick-Select 544 11.6 Exercises 545 12 Strings and Dynamic Programming 553 12.1 String Operations 554 12.1.1 The STL String Class 555 12.2 Dynamic Programming 557 12.2.1 Matrix Chain-Product 557 12.2.2 DNA and Text Sequence Alignment 560 12.3 Pattern Matching Algorithms 564 12.3.1 Brute Force 564 12.3.2 The Boyer-Moore Algorithm 566 12.3.3 The Knuth-Morris-Pratt Algorithm 570 12.4 Text Compression and the Greedy Method 575 12.4.1 The Huffman-Coding Algorithm 576 12.4.2 The Greedy Method 577 12.5 Tries 578 12.5.1 Standard Tries 578 12.5.2 Compressed Tries 582 12.5.3 Suffix Tries 584 12.5.4 Search Engines 586 12.6 Exercises 587 13 Graph Algorithms 593 13.1 Graphs 594 13.1.1 The Graph ADT 599 13.2 Data Structures for Graphs 600 13.2.1 The Edge List Structure 600 13.2.2 The Adjacency List Structure 603 13.2.3 The Adjacency Matrix Structure 605 13.3 Graph Traversals 607 13.3.1 Depth-First Search 607 13.3.2 Implementing Depth-First Search 611 13.3.3 A Generic DFS Implementation in C++ 613 13.3.4 Polymorphic Objects and Decorator Values ~k 621

xxii Contents 13.3.5 Breadth-First Search 623 13.4 Directed Graphs 626 13.4.1 Traversing a Digraph 628 13.4.2 Transitive Closure 630 13.4.3 Directed Acyclic Graphs 633 13.5 Shortest Paths 637 13.5.1 Weighted Graphs 637 13.5.2 Dijkstra's Algorithm 639 13.6 Minimum Spanning Trees 645 13.6.1 Kruskal's Algorithm 647 13.6.2 The Prim-Jarm'k Algorithm 651 13.7 Exercises 654 14 Memory Management and B-Trees 665 14.1 Memory Management 666 14.1.1 Memory Allocation in C++ 669 14.1.2 Garbage Collection 671 14.2 External Memory and Caching 673 14.2.1 The Memory Hierarchy 673 14.2.2 Caching Strategies 674 14.3 External Searching and B-Trees 679 14.3.1 (a,b) Trees 680 14.3.2 B-Trees 682 14.4 External-Memory Sorting 683 14.4.1 Multi-Way Merging 684 14.5 Exercises 685 A Useful Mathematical Facts 689 Bibliography 697 Index 702