adts

20
CS 307 Fundamentals of Computer Scienc e 1 Abstract Data Types many slides taken from Mike Scott, UT Austin

Upload: pablo-penan-loza

Post on 14-Dec-2015

214 views

Category:

Documents


0 download

DESCRIPTION

adts

TRANSCRIPT

Page 1: ADTs

CS 307 Fundamentals of Computer Science

1

Abstract Data Types

many slides taken from Mike Scott, UT Austin

Page 2: ADTs

CS 307 Fundamentals of Computer Science

2

Data Structures Data structure is a representation of data and

the operations allowed on that data.

Page 3: ADTs

CS 307 Fundamentals of Computer Science

3

Abstract Data Types In Object Oriented Programming data and

the operations that manipulate that data are grouped together in classes

Abstract Data Types (ADTs) or data structures or collections store data and allow various operations on the data to access and change it

Page 4: ADTs

CS 307 Fundamentals of Computer Science

4

Why Abstract? Specify the operations of the data structure

and leave implementation details to later– in Java use an interface to specify operations

many, many different ADTs– picking the right one for the job is an important

step in design– "Get your data structures correct first, and the

rest of the program will write itself."              -Davids Johnson

High level languages often provide built in ADTs,– the C++ STL, the Java standard library

Page 5: ADTs

CS 307 Fundamentals of Computer Science

5

The Core Operations Every Collection ADT should provide a way to:

– add an item– remove an item– find, retrieve, or access an item

Many, many more possibilities– is the collection empty– make the collection empty– give me a sub set of the collection– and on and on and on…

Many different ways to implement these items each with associated costs and benefits

Page 6: ADTs

CS 307 Fundamentals of Computer Science

6

Implementing ADTs when implementing an ADT the operations

and behaviors are already specified– think Java interface

Implementer’s first choice is what to use as the internal storage container for the concrete data type– the internal storage container is used to hold the

items in the collection– often an implementation of an ADT– initially slim pickings for choice of storage

containers: arrays anyone?

Page 7: ADTs

CS 307 Fundamentals of Computer Science

7

The Grand Tour Why study ADTs? Why reimplement some of them? How many of you will actually go out and

create your own linked list ADT from scratch?

Remember, the primary goal is to learn how to learn how to use and create ADTs – also learn the behavior of some of the more

conventional ADTs

Page 8: ADTs

CS 307 Fundamentals of Computer Science

8

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

Page 9: ADTs

CS 307 Fundamentals of Computer Science

9

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

Page 10: ADTs

CS 307 Fundamentals of Computer Science

10

Stacks Collection with access only to the last

element inserted Last in first out insert/push remove/pop top make empty

TopData4

Data3

Data2

Data1

Page 11: ADTs

CS 307 Fundamentals of Computer Science

11

Queues Collection with access only to the item that

has been present the longest Last in last out or first in first out enqueue, dequeue, front priority queues and deque

Data4Data3Data2Data1

Front Back

Page 12: ADTs

CS 307 Fundamentals of Computer Science

12

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

Page 13: ADTs

CS 307 Fundamentals of Computer Science

13

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

} Root

Page 14: ADTs

CS 307 Fundamentals of Computer Science

14

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

Page 15: ADTs

CS 307 Fundamentals of Computer Science

15

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?

Page 16: ADTs

CS 307 Fundamentals of Computer Science

16

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

Page 17: ADTs

CS 307 Fundamentals of Computer Science

17

The Java Collection Interface

boolean isEmpty()

int size()

boolean add(Object x)

boolean contains(Object x)

boolean remove(Object x)

void clear()

Object[] toArray()

Iterator iterator()

Page 18: ADTs

CS 307 Fundamentals of Computer Science

18

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?

Page 19: ADTs

CS 307 Fundamentals of Computer Science

19

Page 20: ADTs

CS 307 Fundamentals of Computer Science

20

Example - ArrayListjava.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.