All rights reserved. This book or any portion thereof may not be reproduced or used in any manner whatsoever without the express written permission of the publisher except for the use of brief quotations in a book review or scholarly journal.
3101 Hillsborough St.
Cover photo by Justin Morse.
Preface
The first time I taught CS2 - Data Structures and Algorithms at the University of Colorado, the available course textbooks that I found were either advanc ed programming books that obscured the detail s on the data structures concepts or theory books that lacked sufficient details on implementations . Over the course of the semester, I wrote copious notes to fill the gaps in our selected course textbook and provided them to my students . B y the end of the semester , I had a draft of a data structures book that was exactly the book for which I had been searching . I decided to publish the material as a self-published e-book so that it would be available as inexpensively as possible for anyone who was interested.
The intended audience for this book is second-semester computer science undergraduates. The focus is on fundamental concepts of data structures and algorithms and providing the necessary detail for students to implement the data structures presented . The content included herein is what I was able to cover in a one-semester course.
This book sets itself apart from other data structures books in the following ways:
The data structures are presented in pictures. There are pictures of arrays, linked lists, graphs, trees, and hash tables that help students visualize the algorithms on these structures. General feedback from my students was that they really appreciated the pictures I drew in class , and I have included all of them here.
There are step-by-step descriptions of how algorithm s work. These descriptions illustrat e the state of the data structure at key lines in an algorithms execution.
The algorithms are presented in a language that I call pseudocode with C++ tendencies. In other words, t here is enough detail in the pseudocode for students to convert it to C++. In many cases, basic C++ is also provided.
I am utilizing this book in my current courses, and hope that you as a student or instructor will find this book useful. Good luck, and happy programming.
Rhonda Hoenigman, PhD
University of Colorado, Boulder
2015
About the author
Rhonda Hoenigman earned her Ph.D. in Computer Science in 2012 from the University of Colorado. She has been an I nstructor in that Department since 2013. Dr. Hoenigman s research interests include predictive modeling and optimization of issues surrounding food waste and food justice systems . She has taught undergraduate courses in introductory computer programming , data structures and algorithms, discrete structures , and artificial intelligence.
Introduction
Th is book is intended for computer science students w ho understand the basics of programming and are ready to launch into a discovery of data structures, which are fundamental to an appreciation of the field of computer science. Typically, these are students with a semester of programming experience, and are in a second semester data structures course. To understand the material presented here in , the reader should have an understanding of C++ or another object-oriented language, such as Java.
The emphasis in this book is on presenting fundamental data structures and the algorithms used to access information stored in the se structures . Many of the data structures are also presented in the context of an abstract data type (ADT), which is the implementation mechanism commonly used in an object-oriented language. A lgorithms are presented as part of an ADT. Pseudo-code is used throughout the book, for both the algorithms as well as the ADT definitions.
What is a d ata structure ?
A data structure is a specialized format for organizing related information. Depending on the type of information, one data structure could provide a better arrangement for storing the data than another data structure , where better refers to the ability to access and manipulate the data efficiently . B asic data structures are itemized below, with a brief description of each :
Arrays: Fixed-length l inear sequence of similar elements , where e ach individual element can be accessed by its index.
Lists: Linear sequence of similar elements that can expand and contract as needed.
Trees: Collection of elements with a hierarchical structure.
M aps : Collection of elements that are accessed through one property of the element, known as a key .
R ecords : Composite data type that is composed of other data elements, called fields or members.
This list of data structures is by no means exhaustive , nor is each data structure independent of the other structures on the list. For example, maps are generally composed of records , and an array is commonly used to implement a map . Modifying the behavior of the basic data structure can also create new data structures. For example, a n array or list where elements can only be added and removed at the last position is called a stack .
What is an Abstract Data Type ?
An Abstract Data Type (ADT) is a collection of data elements and the allowable operations on those elements. In an ADT, the operations are encapsulated ; the user only has information about the inputs, outputs, and an explanation of the actions. The specific details of the operations are hidden. The data elements in an ADT are stored in a data structure . The algorithms to access and manipulate that data structure are implemented as methods in the ADT.
Algorithms and p seudocode
When presenting the details of how specific algorithms perform , a book must balance providing enough detail to convey the nuances of the algorithm with getting mired in the syntax of a particular programming language . For these reasons, algorithms are often presented in pseudocode in this and other books. Pseudocode is a simplified description of the algorithm generated from real code that is intended to be more readable than real computer code while still provid ing the detail necessary to understand the complexity of a specific algorithm. Just as with real code, it can take practice to read and understand pseudocode, and part of this understanding comes from an awareness of the pseudocode conventions used in a particular presentation. Pseudocode conventions used herein are described below.
Pseudocode c onventions
The algorithms in this book are presented in a language that I will call pseudocode with C++ tendencies. The pseudocode presented here in may appear informal when compared to pseudocode in other data structures and algorithms books because it preserves more of the C++ language than typical pseudocode .
Expressions are presented using = and == to represent assignment and equivalence, respectively, just as in real code.
For loops include only an initial condition and an end state : for x = 0 to A.end , where A.end is the last index in the array .
Indentation is used to specify which lines are included in an execution block.
Data types are removed from variable definitions and return values.