Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the...

24
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones

Transcript of Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the...

Page 1: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Data Structures and Abstract Data Types

"Get your data structures correct first, and the rest of the program will write itself."  - David Jones

Page 2: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Abstract Data Types Abstract Data Types (aka ADTs) are

descriptions of how a data type will work without implementation details

Description can be a formal, mathematical description

Java interfaces are a form of ADTs– some implementation details start to creep in

CS 221 - Computer Science II

Page 3: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

Data Structures A Data Structure is:

– an implementation of an abstract data type and– "An organization of information, usually in

computer memory", for better algorithm efficiency."

aListList Object

size

myElements

5

A C E B A

0 1 2 3 4 5 6 7 8 9 10

Page 4: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

Data Structure Concepts Data Structures are containers:

– they hold other data– arrays are a data structure– ... so are lists

Other types of data structures:– stack, queue, tree,

binary search tree, hash table,dictionary or map, set, and on and on

– en.wikipedia.org/wiki/List_of_data_structures

Different types of data structures are optimized for certain types of operations

Page 5: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

Core Operations Data Structures have three core operations

– a way to add things– a way to remove things– a way to access things

Details of these operations depend on the data structure– Example: List, add at the end, access by

location, remove by location

More operations added depending on what data structure is designed to do

Page 6: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Implementing ADTs When implementing an ADT, the operations

and behaviors are already specified– think Java interface

But what use as the internal storage container for the concrete data type?– the internal storage container is used to hold the

items in the collection– initially slim pickings for choice of storage

containers: arrays anyone?– later add linked structures– now often an implementation of an ADT (which

use arrays or linked structures)CS 221 - Computer Science II

Page 7: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Bags and Sets Simplest ADT is a Bag

– items can be added, removed, accessed– no implied order to the items– duplicates allowed

Set– same as a bag, except duplicate elements not

allowed– union, intersection, difference, subset

CS 221 - Computer Science II

Page 8: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Lists Items have a position in this Collection

– Random access or not?

Array Lists– internal storage container is native array

Linked Listspublic class Node{ private Object data;private Node next;

} firstlast

CS 221 - Computer Science II

Page 9: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Stacks Collection with access only to the last

element inserted– Last in first out– push (insert)– pop (remove)– peek (top item)– make empty

TopData4

Data3

Data2

Data1

CS 221 - Computer Science II

Page 10: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Queues Collection with access only to the item that

has been present the longest– first in,first out or last in, last out – enqueue (insert)– dequeue (remove) – front

Data4Data3Data2Data1

Front Back

CS 221 - Computer Science II

Page 11: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Stacks and Queues in the Java Collection API

No queue in the Java collections ADT Stack extends Vector (which is almost

exactly like ArrayList)– Hmmm?

One reason the Java Collections Library is often said to be broken

no Queue in Collection API

CS 221 - Computer Science II

Page 12: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Trees Similar to a linked listpublic class TreeNode{ private Object data;private TreeNode left;private TreeNode right;

} Root

CS 221 - Computer Science II

Page 13: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Other Types of Trees Binary Search Trees

– sorted values

Heaps– sorted via a different algorithm

AVL and Red-Black Trees– binary search trees that stay balanced

Splay Trees B Trees

CS 221 - Computer Science II

Page 14: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

HashTables Take a key, apply function f(key) = hash value store data or object based on hash value Sorting O(N), access O(1) if a perfect hash

function and enough memory for table How deal with collisions?

CS 221 - Computer Science II

Page 15: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Other ADTs Maps

– a.k.a. Dictionary– Collection of items with a key and associated

values– similar to hash tables, and hash tables often

used to implement Maps

Graphs– Nodes with unlimited connections between other

nodes

Sparse vectors and sparse matrices

CS 221 - Computer Science II

Page 16: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Generic Containers ADTs or Collection classes should be

generic– only write them once, hold lots or all types of

data– Java achieves genericity through inheritance and

polymorphism

ADTs have an internal storage container– What is storing the stuff, – implementation vs. abstraction– in Java, usually holds Objects. Why?

CS 221 - Computer Science II

Page 17: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

Page 18: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

ADTs and Data Structures in Programming Languages

Modern programming languages usually have a library of data structures– Java collections framework– C++ standard template library– .Net framework (small portion of VERY large

library)– Python lists and tuples– Lisp lists

Page 19: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

Data Structures in Java Part of the Java Standard Library is the

Collections Framework– In class we will create our own data structures

and discuss the data structures that exist in Java

A library of data structures Built on two interfaces

– Collection– Iterator

http://java.sun.com/j2se/1.5.0/docs/guide/collections/index.html

Page 20: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

The Java Collection Interface A generic collection Can hold any object data type Which type a particular collection will hold is

specified when declaring an instance of a class that implements the Collection interface

Helps guarantee type safety at compile time

Page 21: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

Methods in the Collection interfacepublic interface Collection<E>{ public boolean add(E o)

public boolean addAll(Collection<? extends E> c)public void clear() public boolean contains(Object o) public boolean containsAll(Collection<?> c) public boolean equals(Object o)public int hashCode() public boolean isEmpty() public Iterator<E> iterator()public boolean remove(Object o) public boolean removeAll(Collection<?> c) public boolean retainAll(Collection<?> c)public int size() public Object[] toArray()public <T> T[] toArray(T[] a)

}

Page 22: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

The Java ArrayList Class Implements the List interface and uses an

array as its internal storage container It is a list, not an array The array that actual stores the elements of

the list is hidden, not visible outside of the ArrayList class

All actions on ArrayList objects are via the methods

ArrayLists are generic. – They can hold objects of any type!

Page 23: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

CS 221 - Computer Science II

ArrayList's (Partial) Class Diagram

AbstractCollection

Object

AbstractList

ArrayList

Iterable

Collection

List

Page 24: Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

ArrayList Specification java.util Class ArrayList

java.lang.Object | +--java.util.AbstractCollection | +--java.util.AbstractList | +--java.util.ArrayList

All Implemented Interfaces: Cloneable, Collection, List, Serializable

void add(int index, Object element) Inserts the specified element at the specified position in this list.

boolean add(Object o) Appends the specified element to the end of this list.

void clear() Removes all of the elements from this list.

boolean contains(Object elem) Returns true if this list contains the specified element.

int indexOf(Object elem) Searches for the first occurence of the given argument, testing for equality using the equals method.

boolean isEmpty() Tests if this list has no elements.

Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element.

int size() Returns the number of elements in this list.