# Class 42: Introduction to Abstract Data Types and Data Structures

Back to Polymorphism Example. On to Some List ADTs.

Held: Tuesday, 15 April 2003

Summary: Today we begin our in-depth investigation of abstract data types and data structures by considering some of the general rules for data type design. We also start building a partial hierarchy of data types.

Related Pages:

Notes:

• Are there any questions on exam 3?

Overview:

• Organizing Data.
• Example: Arrays.
• An ADT Hierarchy.
• Collections.

## Organizing Data

• As you saw from some of the algorithms we've written way that you organize your information can have a significant effect on the running time of your algorithms.
• For example, we can find the element in the middle of an array in O(1) time, but 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 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.
• Sorry, no clever equivalent to the 6P's.
• 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
• Expected running time: O(1)
• We want to get elements of arrays
• Expected running time: O(1)
• We want to get the size of arrays
• Expected running time: O(1)
• What else?

## An ADT Hierarchy

• Given the wonders of inheritance, we may want to think about the data types and structures we define in terms of a hierarchy.
• We start with one big division:
• Primitive types, like `int`
• Compound types, like `Rational` or `Vector`.
• 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
a. Array
...
```

### Collections

• Most data structures are used to collect a number of objects
• Arrays
• Lists
• Can you think of others?
• Hence, most data structures we create will be variations (subinterfaces) of a general `Collection` interface.
• Java now includes such an interface, but we'll design our own.
• 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. (By number, by name, by ....).

## History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Back to Polymorphism Example. On to Some List ADTs.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:21:01 2003.
The source to the document was last modified on Thu Jan 16 15:16:15 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.42.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu