An Intuitive Introduction to Data Structures

An Intuitive Introduction to Data Structures

An Intuitive Introduction to Data Structures Brian Heinold Department of Mathematics and Computer Science Mount St. Mary’s University ©2019 Brian Heinold Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License Contents 1 Running times of algorithms6 1.1 Analyzing algorithms............................................7 1.2 Brief review of logarithms.........................................9 1.3 Big O notation................................................ 10 1.4 Examples................................................... 10 1.5 A catalog of common running times................................... 12 1.6 A few notes about big O notation.................................... 16 1.7 Exercises................................................... 17 2 Lists 20 2.1 Dynamic arrays............................................... 20 2.2 Implementing a dynamic array...................................... 21 2.3 Linked lists.................................................. 25 2.4 Implementing a linked list......................................... 26 2.5 Working with linked lists......................................... 32 2.6 Comparison of dynamic arrays and linked lists............................ 35 2.7 Making the linked list class generic................................... 37 2.8 The Java Collections Framework..................................... 39 2.9 Iterators*................................................... 41 2.10 Exercises................................................... 42 3 Stacks and Queues 45 3.1 Implementing a stack class........................................ 46 3.2 Implementing a queue class........................................ 48 3.3 Stacks, queues, and deques in the Collections Framework..................... 50 3.4 Applications................................................. 52 3.5 Exercises................................................... 53 4 Recursion 56 4.1 Introduction................................................. 56 4.2 Several recursion examples........................................ 57 4.3 Printing the contents of a directory................................... 60 4.4 Permutations and combinations..................................... 60 4.5 Working with recursion.......................................... 62 2 CONTENTS 3 4.6 The Sierpinski triangle........................................... 65 4.7 Towers of Hanoi............................................... 67 4.8 Summary................................................... 70 4.9 Exercises................................................... 70 5 Binary Trees 74 5.1 Implementing a binary tree........................................ 74 5.2 A different approach............................................ 84 5.3 Applications................................................. 85 5.4 Exercises................................................... 86 6 Binary Search Trees, Heaps, and Priority Queues 88 6.1 Binary Search Trees............................................. 88 6.2 Implementing a BST............................................ 89 6.3 More about BSTs.............................................. 96 6.4 Java’s Comparable interface....................................... 97 6.5 Heaps..................................................... 101 6.6 Implementing a heap............................................ 102 6.7 Running time and applications of heaps................................ 107 6.8 Priority Queues............................................... 107 6.9 Priority queues and heaps in the Collections Framework...................... 109 6.10 Exercises................................................... 111 7 Sets, Maps and Hashing 113 7.1 Sets...................................................... 113 7.2 Hashing.................................................... 116 7.3 Implementing a set with hashing..................................... 117 7.4 Maps...................................................... 121 7.5 Using hashing to implement a map................................... 121 7.6 Sets and maps in the Collections Framework............................. 123 7.7 Applications of sets and maps...................................... 124 7.8 Exercises................................................... 126 8 Graphs 130 8.1 Terminology................................................. 131 8.2 Implementing a graph class........................................ 131 8.3 Searching................................................... 135 8.4 Digraphs................................................... 137 8.5 Weighted graphs............................................... 140 8.6 Kruskal’s algorithm for minimum spanning trees........................... 142 8.7 Exercises................................................... 144 9 Sorting 148 CONTENTS 4 9.1 Selection Sort................................................ 148 9.2 Bubble Sort.................................................. 150 9.3 Insertion Sort................................................ 151 9.4 Shellsort.................................................... 152 9.5 Heapsort................................................... 153 9.6 Merge Sort.................................................. 154 9.7 Quicksort................................................... 156 9.8 Counting Sort................................................ 159 9.9 Comparison of sorting algorithms.................................... 159 9.10 Sorting in Java................................................ 161 9.11 Exercises................................................... 163 Index 165 Preface This book is about data structures. Data structures are ways of storing and organizing information. For example, lists are a simple data structure for storing a collection of data. Some data structures make it easy to store and maintain data in a sorted order, while others make it possible to keep track of connections between various items in a collection. Choosing the right data structure for a problem can make the difference between solving and not solving the problem. The book covers the internals of how they work and covers how to use the ones built into Java. Chapter 1 covers the analysis of running times, which is important because it allows us to estimate how quickly algorithms will run. Chapter 2 covers two approaches to lists, namely dynamic arrays and linked lists. Chapter 3 covers stacks and queues, which are simple and important data structures. Chapter 4 covers recursion, which is not a data structure, but rather an important approach to solving problems. Chapter 5 is about binary trees, which are used to store hierarchical data. Chapter 6 is about two particular types of binary trees, heaps and binary search trees, which are used for storing data in sorted order. Chapter 7 covers hashing and two important data structures implemented by hashing—sets and maps. Chapter 8 covers graphs, which are a generalization of trees and are particularly important for modeling real-world objects. Chapter 9 covers sorting algorithms. Chapters 1 through 5 form the basis for the other chapters and should be done first and probably in the order given. The other chapters can be done in any order. This book is based on the Data Structures and Algorithms classes I’ve taught. I used a bunch of data structures and introductory programming books as references the first time I taught the course, and while I liked parts of all the books, none of them approached things quite in the way I wanted, so I decided to write my own book. I’ve tried to keep explanations short and to the point. A guiding theme of this book is that the best way to understand a data structure or algorithm is to implement it. Generally, for each data structure and algorithm I provide a succinct explanation of how it works, followed by an implementation and discussion of the issues that arise in implementing it. After that, there is some discussion of efficiency and applications. The imple- mentations are designed with simplicity and understanding in mind. They are not designed to be used in the real world, as any real-world implementation would have tons of practical issues to consider, which would clutter things up too much. In addition, my approach to topics is a lot more intuitive than it is formal. If you are looking for a formal approach, there are many books out there that take that approach. Please send comments, corrections, and suggestions to [email protected]. Last updated October 9, 2019. 5 Chapter 1 Running times of algorithms Consider the problem of finding all the words that can be made from the letters of a given word. For instance, from the main word computer, one can make put, mop, rope, and compute, and many others. An algorithm to do this would be useful for programming a Scrabble computer player or just for cheating at word games. One approach would be to systematically generate all combinations of letters from the main word and check each to see if each is a real word. For instance, from computer, we would start by generating co, cm, cp, etc., going all the way to computer, looking for each in a word list to see which are real words. A different approach would be to loop through the words in the word list and check if each can be made from the letters of the main word. So we would start with aardvark, abaci, aback, etc., checking each to see if it comes from the main word. Which one is better? We could go with whatever approach is easier to program. Sometimes, this is a good idea, but not here. There is a huge difference in performance

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    167 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us