# Class 29: Introduction to Data Structures

Back to Discussion of Assignment 3. On to Introduction to Lists.

Held Monday, March 13, 2000

Overview

Today we begin our in-depth investigation of data structures by considering some of the general rules for data structure design. We also start building a hierarchy of data structures.

Notes

• Phase 3 of the project is due tomorrow. Are there questions? Do you want to use some class time for discussion?
• One group has sent me a draft of the rules interface. We'll walk through parts of it together.
• Are there questions on exam 2?
• I will not be grading homework 3.
• I will, however, make sure that you turned something reasonable in.
• Some notes are available online.

Contents

Summary

• Introduction to data structures
• Four key issues: functionality, implementation, efficiency, applications
• Compound data types
• Collections
• Iterated Collections
• Keyed Tables

## Organizing Data

• As you saw from some of the algorithms we visited last week, the way that you organize your information can have a significant effect on the running time of your algorithms.
• We can find the element in the middle of an array in O(1) time. It takes O(n) time to find the element in the middle of a list.
• Hence, we often define data structures that help us organize information.
• As you might guess, there is a close relationship between data structures and algorithms.
• Typically, we use data structures to create efficient implementations of the operations needed by algorithms.
• Some data structures suggest new algorithms.
• As in many other cases, we separate the implementation implementation and specification when talking about ways to organize information.
• Abstract Data Type (ADT) is the term typically used for the specification. For most data types, we specify the core operations in the ADT. We will almost always use interfaces for ADTs.
• Data Structure is the term typically used for the implementation. We will almost always use classes for data structures.
• But people are casual with terminology.
• Note that their are typically five issues we will consider when we work with ADTs and data structures:
• Their overall concept: what grounds the design of this ADT or structure;
• Their functionality: what ``methods'' the structures provide (the ADT);
• Their implementation: how they do what they do;
• Their efficiency: how fast they are (or with how much memory they consume); and
• Their applications: how we might use them.
• How do we build new data structures? Using other data structures, including Java's built-in arrays.

## Arrays, Revisited

• We'll begin by reconsidering arrays.
• What are the key operations that you might use to describe arrays?
• Don't read the next few lines until we've had a chance to discuss them.
• We want to create new arrays
• Input: size of the array
• Input: type of the elements in the array
• We want to set elements of arrays
• Running time: O(1)
• We want to get elements of arrays
• Running time: O(1)
• We want to get the size of arrays
• Running time: O(1)
• What else?

• Given the wonders of inheritance, we may want to think about the data types and structures we define in terms of a hierarchy.
• Primitive types
• Compound types
• We then move on to a division within the compound types
• Some structures, like lists and arrays, are used to collect an arbitrary number of values.
• Other structures, such as a Point, may collect values, but only have a fixed number.
• Here's what we have so far:
```I. Type
A. Primitive
1. Numeric
a. Discrete
i. byte
ii. short
iii. char
iv. int
v. long
b. Continuous
...
2. Non-numeric
a. boolean
...
B. Compound
1. Fixed
a. Point
b. StudentInfo
...
2. Collection
...
```

### Collections

• Most data structures are used to collect a number of objects (just like arrays).
• Hence, most data structures we create will be variations (subinterfaces) of a general `Collection` interface.
• What methods might such an interface include? We'll need to consider purpose and detailed meaning (perhaps even the legendary ``6 P's''.
• Here are some basics (without detailed definitions):
• We might create collections.
• We might add elements to the collection.
• We might delete elements from the collection.
• We might check if there is room for more elements.
• We might clear all the elements in the collection.
• Are there others you can think of?
• I tend to start with very simple collections and then immediately split into
• Iterated Collections: Collections in which you can step through the values, one-by-one.
• Tables: Collections in which you can look up values.

## History

Tuesday, 18 January 2000

• Created as a blank outline.

Monday, 13 March 2000

• Filled in the details. The sections on arrays and collections were taken from outline 29 of CSC152 99F

Back to Discussion of Assignment 3. On to Introduction to Lists.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.