i SYLLABUS UNIT - I BASIC CONCEPTS AND ARRAYS Algorithm Specification, Performance Analysis and Measurement. Arrays : Abstract Data Types and the C++ Class, The Array as an Abstract Data Type, The Polynomial Abstract Data Type, Sparse Matrices, Representation of Arrays, The String Abstract Data Type. UNIT - II STACKS AND QUEUES Templates in C++, The Stack Abstract Data Type, The Queue Abstract Data Type, Subtyping and Inheritance in C++, A Mazing Problem, Evaluation of Expressions, Additional Exercises. UNIT - III LINKED LISTS Singly Linked Lists and Chains, Representing Chains in C++, The Template Class Chain, Circular Lists, Available Space Lists, Linked Stacks and Queues, Polynomials, Equivalence Classes, Sparse Matrices, Doubly Linked Lists, Generalized Lists. UNIT - IV TREES AND GRAPHS Trees : Introduction, Binary Trees, Binary Tree Traversal and Tree Integrators, Copying Binary Trees, Threaded Binary Trees, Heaps, Binary Search Trees. Graphs : The Graph Abstract Data Type, Elementary Graph Operations (dfs and bfs), Minimum Cost Spanning Trees (Prim s and Kruskal s Algorithms). UNIT - V SORTING, HASHING, EFFICIENT BINARY SEARCH TREES Insertion Sort, Quick Sort, How Fast Can we Sort, Merge Sort, Heap Sort, Sorting on Several Keys, List and Table Sorts, Summary of Internal Sorting, Static Hashing, AVL Trees, Red-Black Trees, Splay Trees, M-way Search Trees, B-Trees.
ii data structures using c++ (b.e. o.u) ii year i semester (COMMON TO CSE & IT) CONTENTS Contents UNIT - I [BASIC CONCEPTS AND ARRAYS]... 1.1-1.86 1.1 INTRODUCTION... 1.2 1.2 DATA A STRUCTURES... 1.2 1.2.1 Application of Data Structures... 1.3 1.2.2 Comparison of Data Structures... 1.4 1.3 ALGORITHM SPECIFICATION... 1.5 1.3.1 Introduction to Algorithm... 1.5 1.3.2 Definition of Algorithm... 1.5 1.3.3 Notation of Algorithm... 1.5 1.3.4 Properties of Algorithm... 1.6 1.3.5 Psuedo Code for Expressing Algorithms... 1.6 1.4 Asymptotic Notation... 1.14 1.4.1 Big-Oh Notation (Ο)... 1.14 1.4.1.1 Big-Oh Ratio Theorem... 1.16 1.4.1.2 Basic Efficiency Classes... 1.17 1.4.2 Omega Notation (Ω)... 1.19 1.4.2.1 Big Omega Ratio Theorem... 1.20 1.4.2.2 Little-Omega Notation (ω)... 1.20
iii 1.4.3 Theta Notation (θ)... 1.21 1.4.3.1 Theta Ratio Theorem... 1.22 1.4.4 Little-Oh Notation (ο)... 1.22 1.4.5 Order of Growth using Limits (Alternative Definitions for Asymptotic Notations)... 1.23 1.5 PERFORMANCE ANALYSIS AND MEASUREMENT... 1.26 1.5.1 Performance Analysis... 1.26 1.5.1.1 Space Complexity... 1.28 1.5.1.2 Time Complexity... 1.30 1.5.1.3 The Unit of Algorithm s Runun-Time... 1.31 1.5.1.4 The Order of Growth... 1.32 1.5.1.5 Best Case, Average Case and Worst Case Efficiencies... 1.33 1.5.1.6 Solved Problems... 1.34 1.5.2 Performance Measurement... 1.37 1.6 ARRAYS YS... 1.40 1.6.1 An Introduction to the C++ Class... 1.40 1.6.2 Data Abstraction and Encapsulation in C++... 1.43 1.6.2.1 Data Abstraction... 1.43 1.6.2.2 Encapsulation... 1.44 1.6.3 Declaring Class Objects and Invoking Member Functions... 1.44 1.6.4 Special Class Operations... 1.46 1.6.4.1 Constructors and Destructors... 1.46 1.6.4.1.1 Constructors Vs Destructors... 1.51 1.6.4.2 Operator Overloading... 1.51 1.6.4.2.1 Rules for Overloading Operators... 1.53
iv Contents 1.7 THE ARRAY AS AN ABSTRACT DATA A TYPE... 1.54 1.8 THE POLYNOMIAL ABSTRACT DATA A TYPE... 1.57 1.8.1 Polynomials Representation... 1.58 1.8.2 Polynomial Addition... 1.59 1.9 SPARSE MATRICES TRICES... 1.61 1.9.1 Introduction... 1.61 1.9.2 Sparse Matrix Representation... 1.61 1.9.3 Transposing a Matrix... 1.66 1.9.4 Matrix Multiplication... 1.69 1.10 REPRESENTATION TION OF ARRAYS YS... 1.72 1.11 THE STRING ABSTRACT CT DATA A TYPE... 1.78 1.11.1 String Pattern Matching : A Simple Algorithm... 1.81 1.11.2 String Pattern Matching : The Knuth Morris Pratt Algorithm... 1.82 1.11.3 KMP Failure Function... 1.83 1.11.4 The KMP Algorithm... 1.84 1.11.5 Computing The Failure Function... 1.84 1.11.6 Efficiency of the Search Algorithm... 1.86 UNIT - II [STACKS AND QUEUES]... 2.1-2.72 2.1 INTRODUCTION TO O STACKS AND QUEUES... 2.2 2.2 TEMPLATES TES IN C++... 2.3 2.2.1 Function Template... 2.4 2.2.2 Using Templates to Represent Container Classes... 2.6 2.3 THE STACK ABSTRACT DATA A TYPE... 2.9 2.3.1 Formula Based Representation... 2.13
v 2.4 THE QUEUE ABSTRACT DATA A TYPE... 2.18 2.4.1 Abstract Datatype of Queue... 2.19 2.4.2 Formula Based Representation of Queue... 2.20 2.4.3 Circular Queue... 2.25 2.5 SUBTYPE AND INHERITANCE IN C++... 2.35 2.6 A MAZING PROBLEM... 2.38 2.7 EVAL ALUATION OF EXPRESSIONS... 2.45 2.7.1 Expressions... 2.45 2.7.2 Postfix Notation... 2.47 2.7.3 Infix to Postfix... 2.50 2.8 ADDITIONAL EXERCISES... 2.55 UNIT - III [LINKED LISTS]... 3.1-3.84 3.1 INTRODUCTION... 3.2 3.2 LINKED LIST... 3.2 3.3 SINGLY Y LINKED LISTS AND CHAINS... 3.2 3.4 REPRESENTING CHAINS IN C++... 3.8 3.4.1 Defining A Node in C++... 3.8 3.4.2 Designing a Chain Class in C++... 3.10 3.4.3 Pointer Manipulation in C++... 3.13 3.4.4 Chain Manipulation Operations... 3.14 3.5 THE TEMPLATE CLASS CHAIN... 3.21 3.5.1 Implementing Chains With Templates... 3.21 3.5.2 Chain Iterator... 3.22 3.5.3 C++ Iterator... 3.24
vi Contents 3.5.4 Chain Operations... 3.27 3.5.4.1 Inserting at the Beginning of the List... 3.27 3.5.4.2 Inserting at the End of the List... 3.28 3.5.4.3 Inserting After the Given Element of the List... 3.29 3.5.4.4 Reversing a List... 3.30 3.5.4.5 Deleting Entire List... 3.31 3.5.5 Reusing a Class... 3.32 3.6 CIRCULAR LISTS... 3.33 3.7 AVAILABLE AILABLE SPACE LISTS... 3.36 3.8 LINKED STACKS AND QUEUES... 3.38 3.8.1 Stacks... 3.38 3.8.2 Queues... 3.41 3.9 POLYNOMIALS YNOMIALS... 3.45 3.9.1 Polynomial Representation... 3.45 3.9.2 Addition of Polynomials... 3.46 3.9.3 Circular List Representation of Polynomials... 3.52 3.10 EQUIVALENCE CLASSES... 3.54 3.11 SPARSE MATRICES TRICES... 3.58 3.12 DOUBLY Y LINKED LISTS... 3.63 3.13 GENERALIZED LISTS... 3.75 UNIT - IV [TREES AND GRAPHS]... 4.1-4.104 4.1 INTRODUCTION... 4.2 4.1.1 Terminology... 4.2 4.1.2 Representation of Trees... 4.6 4.1.2.1 List Representation... 4.6 4.1.2.2 Left Child-Right Sibling Representation... 4.7 4.1.2.3 Representation as a Degree Two Tree... 4.8
vii 4.2 BINARY TREES... 4.10 4.2.1 The Abstract Data Type... 4.10 4.2.1.1 Differences Between Tree and Binary Tree ree... 4.11 4.2.2 Properties of Binary Trees... 4.12 4.2.3 Binary Tree Representation... 4.15 4.2.3.1 Array Representation... 4.15 4.2.3.2 Linked Representation... 4.16 4.2.4 Common Binary Tree Operations... 4.17 4.3 BINARY TREE TRAVERSAL AND TREE ITERATORS... 4.17 4.3.1 Introduction... 4.17 4.3.2 Inorder Traversal... 4.19 4.3.3 Preorder Traversal... 4.20 4.3.4 Postorder Traversal raversal... 4.22 4.3.5 Iterative Inorder Traversal... 4.25 4.3.6 Level Order Traversal... 4.26 4.3.7 Traversal Without a Stack... 4.28 4.3.8 Program... 4.28 4.4 COPYING BINARY TREES... 4.40 4.5 THREADED BINARY TREES... 4.40 4.5.1 Threads... 4.40 4.5.2 Inorder Traversal of a Threaded Binary Tree ree... 4.44 4.5.3 Inserting a Node Into a Threaded Binary Tree ree... 4.45 4.6 HEAPS... 4.48 4.6.1 Priority Queues... 4.48 4.6.2 Definition of a Max Heap... 4.50 4.6.3 Insertion Into a Max Heap... 4.52 4.6.4 Deletion From a Max Heap... 4.54
viii Contents 4.7 BINARY SEARCH TREES... 4.56 4.7.1 Definition... 4.56 4.7.1.1 ADT of Trees... 4.57 4.7.1.2 Binary Search Tree Implementation... 4.57 4.7.2 Searching a Binary Search Tree ree... 4.58 4.7.3 Insertion Into a Binary Search Tree ree... 4.59 4.7.4 Deletion From a Binary Search Tree... 4.60 4.7.5 Joining and Splitting Binary Trees... 4.61 4.7.6 Height of a Binary Search Tree... 4.64 4.7.7 Implementation of BST... 4.65 4.8 THE GRAPH ABSTRACT CT DATA A TYPE... 4.68 4.8.1 Introduction... 4.68 4.8.2 Definitions... 4.68 4.8.2.1 Abstract Data Type for Graph... 4.69 4.8.2.2 Graph Terminology... 4.69 4.8.2.3 Properties of Graphs... 4.74 4.8.3 Graph Representations... 4.74 4.8.3.1 Adjacency Matrix... 4.75 4.8.3.2 Adjacency Lists... 4.76 4.8.3.3 Adjacency Multilists... 4.77 4.8.3.4 Weighted Edges... 4.78 4.8.3.5 C++ Graph Classes... 4.78 4.9 ELEMENTAR ARY GRAPH OPERATIONS... 4.79 4.9.1 Breadth First Search... 4.79 4.9.2 Depth First Search... 4.83 4.10 MINIMUM COST SPANNING TREES... 4.92 4.10.1 Kruskal s Algorithm... 4.93 4.10.2 Prim rim s Algorithm... 4.98
ix UNIT - V [SORTING, HASHING, EFFICIENT BINARY SEARCH TREES]... 5.1-5.154 5.1 INTRODUCTION... 5.2 5.2 INSERTION SORT... 5.2 5.3 QUICKSORT... 5.8 5.4 HOW FAST CAN WE SORT?... 5.16 5.5 MERGE SORT... 5.17 5.6 HEAP SORT... 5.31 5.7 SORTING ON SEVERAL KEYS... 5.44 5.8 LIST AND TABLE SORTS TS... 5.51 5.9 SUMMARY OF INTERNAL SORTING... 5.57 5.10 INTRODUCTION TO O HASHING... 5.59 5.11 STATIC TIC HASHING... 5.59 5.11.1 Hash Tables... 5.59 5.11.2 Hash Functions... 5.61 5.11.2.1 Division... 5.61 5.11.2.2 Mid Square... 5.62 5.11.2.3 Folding... 5.63 5.11.2.4 Digit Analysis... 5.63 5.11.2.5 Converting Keys to Integers... 5.64 5.11.3 Secure Hash Functions... 5.65 5.11.4 Overflow Handling... 5.68 5.11.4.1 Open Addressing... 5.68 5.11.4.1.1 Linear Probing... 5.69 5.11.4.1.2 Quadratic Probing... 5.75
x Contents 5.11.4.1.3 Rehashing... 5.78 5.11.4.1.4 Random Probing... 5.80 5.11.4.2 Chaining... 5.81 5.11.5 Theoretical Evaluation of Overflow Techniques... 5.84 5.12 AVL TREES... 5.85 5.12.1 AVL Tree Representation... 5.86 5.12.2 Rotations in AVL Trees... 5.87 5.12.3 Operations of AVL Trees... 5.89 5.12.3.1 Inserting Into an AVL Tree... 5.89 5.12.3.2 Deleting From an AVL VL Tree ree... 5.94 5.12.5 Drawbacks of AVL VL Trees rees... 5.110 5.13 RED BLACK TREES... 5.114 5.13.1 Definition... 5.114 5.13.2 Representation of a Red ed Black Tree ree... 5.115 5.13.2.1 Advantages and Uses of Red-Black Trees rees... 5.116 5.13.3 Searching a Red ed Black Tree ree... 5.116 5.13.4 Inserting into a Red ed Black Tree ree... 5.116 5.13.5 Deletion From a Red ed Black Tree ree... 5.123 5.13.6 Joining Red ed Black Tree ree... 5.126 5.13.7 Splitting a Red-Black Tree ree... 5.128 5.14 SPLAY Y TREES... 5.129 5.14.1 Bottom-Up Spaly Trees... 5.129 5.14.2 Top op-down Splay Trees rees... 5.133 5.15 M WAY SEARCH TREES... 5.137 5.15.1 Definition and Properties... 5.137 5.15.2 Searching an m-way Search Tree... 5.137
xi 5.16 B TREES... 5.138 5.16.1 Definition and Properties... 5.138 5.16.2 Number of Elements in a B Tree... 5.139 5.16.3 Insertion Into a B Tree... 5.140 5.16.4 Deletion From a B Tree... 5.142 5.16.5 Node Structure... 5.144 SHORT QUESTIONS AND ANSWERS Short Questions and Answers... SQ.1 - SQ.22 EXPECTED UNIVERSITY QUESTIONS Expected University Questions with Solutions... E.23 - E.34