Data Structures in C
Total Page:16
File Type:pdf, Size:1020Kb
Data Structures in C Lecture Notes for CIS*2520 University of Guelph DRAFTJudi McCuaig August 26, 2017 2 Foreward This document was originally written as supplement to the slides and lectures for CIS*2520. Recently(early 2017) it has been revised to serve as the main reference document for a distance education version of CIS*2520. Students using this material are assumed to have a background in programming in C. Special thanks to David McCaughan, Pascal Matsakis and Xining Li for sharing slides and presentations from their offerings CIS*2520. D. McCaughan's materials influenced the overall design of the course materials signifi- cantly. Dr. Matsakis and and Dr. Li's work helped greatly to improve the material on computational complexity, heaps, and priority queues. Contributors The latex source for this book was placed on github on March 24, 2017. The work has been improved by contributions from the following persons: • Lauren Good This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. DRAFT Contents 1 Introduction 1 1.1 Why Software Design.........................................1 1.2 Software Development- CIS 2520 Style................................1 1.3 Test driven development.......................................3 1.4 Extending Activities..........................................5 2 Abstraction 7 2.1 Introduction..............................................7 2.2 Real World Abstraction........................................7 2.2.1 Implementation........................................8 2.3 Abstraction in Software Design....................................9 2.3.1 Abstract Data Types..................................... 11 2.3.2 Worked Example: The Fraction ADT............................. 11 2.4 Information Hiding (Encapsulation).................................. 13 2.5 Extending Activities.......................................... 13 3 List ADT 15 3.1 Introduction.............................................. 15 3.2 Linked Lists.............................................. 15 3.2.1 Implementation Details.................................... 16 3.2.2 Nodes............................................. 17 3.2.3 Operations........................................... 17 3.2.4 Adding Elements....................................... 18 3.2.5 Deleting Nodes........................................ 19 3.2.6 Other Core List Operations.................................. 19 3.3 Array implementation for a list.................................... 20 3.4 Array Lists vs Linked Lists......................................DRAFT 20 3.5 List Iterator.............................................. 21 3.6 Extending Activities.......................................... 21 4 Stack ADT 23 4.1 Why use stacks?............................................ 25 4.2 Operations on a Stack ADT..................................... 25 4.3 Array Implementation of a Stack................................... 27 4.4 Applications.............................................. 28 4.5 Resources............................................... 29 4.6 Extending Activities.......................................... 29 5 Queues 31 5.1 Introduction.............................................. 31 5.2 Implementation: Encapsulate List ADT............................... 32 5.3 Array Implementation of a Queue................................... 33 5.4 Additional Resources......................................... 35 3 4 CONTENTS 5.5 Extending Activities.......................................... 35 6 Associative Arrays 37 6.1 Introduction.............................................. 37 6.2 Implementation............................................ 37 6.2.1 Array Implementation..................................... 37 6.2.2 Linked List Implementation.................................. 38 6.3 Operations on Associative Arrays................................... 39 6.4 Applications.............................................. 41 6.4.1 Lending Library........................................ 41 6.5 Extending Activities.......................................... 42 7 Hash Tables 43 7.1 Introduction.............................................. 43 7.2 Hash Table Operations........................................ 44 7.3 Hash Table Characteristics...................................... 44 7.4 Hash Functions............................................ 45 7.4.1 Before Hashing: Preconditioning............................... 45 7.4.2 Hashing by Truncation.................................... 45 7.4.3 Hashing by Folding...................................... 45 7.4.4 Hashing by Division...................................... 45 7.5 Collision Resolution for Hashing.................................... 46 7.5.1 Open Addressing....................................... 46 7.5.2 Separate Chaining....................................... 48 7.6 Additional Resources......................................... 48 7.7 Extending Activities.......................................... 48 8 Introduction to Trees 51 8.1 Simple Rooted Trees......................................... 51 8.2 Operations on Trees.......................................... 52 8.2.1 Tree Insertions......................................... 52 8.2.2 Removing nodes from a tree................................. 54 8.2.3 Tree Traversal......................................... 54 8.3 Binary Trees.............................................. 55 8.3.1 C Structure for BinaryDRAFT Tree ADT............................... 55 8.3.2 Recursive Helper Functions.................................. 56 8.3.3 Wrapper functions....................................... 61 8.4 Types of Binary Trees......................................... 62 8.5 Extending Activities.......................................... 63 9 Heaps 65 9.1 Introduction.............................................. 65 9.2 Operations............................................... 66 9.2.1 Heap Insert.......................................... 66 9.2.2 Heap Delete.......................................... 66 9.3 Array Implementation of a Heap................................... 67 9.3.1 Adding Elements....................................... 67 9.3.2 Removing Elements...................................... 71 9.4 Extending Activities.......................................... 73 CONTENTS 5 10 Priority Queue 75 10.1 Uses.................................................. 75 10.2 Implementation............................................ 75 10.3 Operations............................................... 75 10.4 Varying Priority and Starvation Prevention.............................. 76 10.5 Extending Activities.......................................... 76 11 Trees 77 11.1 Balanced Trees............................................ 77 11.2 Binary Search Trees.......................................... 79 11.2.1 AVL trees........................................... 79 11.2.2 Case B: Imbalance to the left -> Right rotation....................... 87 11.2.3 Case C: Imbalance in left subtree of right child double rotation.............. 87 11.3 B-Trees................................................ 90 11.4 Summary................................................ 92 11.5 Additional Resources......................................... 92 11.6 Extending Activities.......................................... 92 12 Sorting 95 12.1 Bubble Sort.............................................. 96 12.1.1 Bubble Sort Pseudocode................................... 97 12.2 Selection Sort............................................. 98 12.2.1 Seletion Sort Pseudocode................................... 99 12.3 Insertion Sort............................................. 99 12.3.1 Selection Sort Pseudocode.................................. 100 12.4 Shell Sort............................................... 100 12.4.1 Shell Sort Pseudocode.................................... 101 12.5 Quick Sort............................................... 102 12.5.1 Quick Sort Pseudocode.................................... 103 12.6 Merge Sort............................................... 103 12.6.1 Merge Sort Pseudocode.................................... 104 12.7 Additional Resources......................................... 105 12.8 Self-Test Questions.......................................... 105 13 Algorithmic Complexity and Complexity Analysis 109 13.0.1 Determining Complexity.................................... 109 13.0.2 Big O notation........................................ 111 13.0.3 Estimating Complexity.................................... 111 13.0.4 Complexity in the realDRAFT world................................. 111 13.1 Analysis of Algorithms using Big O.................................. 112 13.1.1 Analyzing recursive functions................................. 113 13.1.2 O(1) and O(N)........................................ 114 13.1.3 O(logN)............................................ 114 13.1.4 O(NlogN)........................................... 115 13.1.5 O(n2)............................................. 116 13.1.6 O(2n)............................................. 116 13.1.7 Reasonable vs Unreasonable algorithms........................... 117 13.2 Additional Resources for Computational Complexity......................... 118 13.3 Extending Activities.......................................... 118 6 CONTENTS DRAFT Chapter 1 Introduction Welcome to CIS 2520! A data structures course is often the first course in a series of courses on software design. During this class we will examine a variety of abstract