cs 261 - winter 2009 abstract data types. container classes over the years, programmers have...

26
CS 261 - Winter 2009 Abstract Data Types

Upload: kelly-stokes

Post on 04-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

CS 261 - Winter 2009

Abstract Data Types

Page 2: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Container Classes

• Over the years, programmers have identified a small number of different ways of organizing collections of data.

• These container abstractions are now the fundamental heart of the study of data structures.

• Examples, Stack, Queue, Set, Map, etc

Page 3: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Three Levels of Abstraction

• There are at least three levels of abstraction in the study of data structures

• ADT - Abstract Data Type, language independent

• Interface - the interface in a particular library of containers

• Implementation - the various implementations in a particular library

Page 4: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

The ADT view(Abstract Data Type)

• Every data type can be described in a way that is independent of language/library

• E.G., A Stack is a collection that has the property that an item removed is the most recently entered item.

• Properties that are true regardless of the names given to operations in library

Page 5: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Metaphors

• ADT view are often described by metaphors (e.g., stack of plates). Easy to remember. Easy to understand.

Page 6: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

The Interface View

• Gives specific names to operations

struct stack;

void initStack (struct stack * stk);

void pushStack (struct stack * stk, double val);

double topStack (struct stack * stk);

void popStack (struct stack * stk);

int isEmptyStack (struct stack * stk);

Page 7: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Additional Information

• The interface view gives only signatures

• Must also attach meanings (LIFO properties of stack, etc)

• Can also attach expected execution times (want push and pop to be constant time).

• Both more and less informative than ADT view

Page 8: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

The Implementation View

void pushStack (struct stack * stk, double val) {

stk->data.add(val);

}

int stackIsEmpty (struct stack * stk) {

return dyArraySize(stk->data) == 0:

}

Page 9: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

The Study of Data Structures

• As we study data structures we move constantly between all three views

• The ADT view is constant from one language to the next

• The interface view allows you to compare implementations

• The implementation allows you to see how it is done

Page 10: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

The Classic ADTs

• Bag, Ordered Bag - simple collections

• Stack, Queue, Deque - ordered by insertion

• Set - unique elements, fast test

• Map (Dictionary) - key/value associations

• Priority Queue - ordered by importance

Page 11: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

BAG as Design Pattern

• Problem: Need to maintain an unordered collection of elements, without needing to know how it is organized

• Forces: Require insertion, test and removal - time of insertion is unimportant

• Counter-forces: Time of insertion IS important

• Solution: Provide abstract interface

Page 12: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

BAG interface

• Provide functions for operations, effectively hiding implementation (names may vary)

addBag (container, value)

testBag (container, value)

removeBag (container, value)

sizeBag (container, value)

Page 13: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Stack as Design Pattern

• Problem: Maintain collection in Last-In, First-Out format

• Forces: LIFO

• Counter-forces: non-LIFO

• Solution: again, abstract Stack interface

Page 14: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

The Classic Implementation Techniques

• Arrays and Dynamic Arrays (Vectors)

• Linked Lists

• Binary Trees, Balanced Trees

• Heaps

• Hash Tables

• Skip Lists

• Etc etc etc

Page 15: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Now, your first worksheet

• As a way to introduce C and the worksheet idea, we will make implementations of a simple BAG and STACK using an array

• We will do these together, in class - won’t always be that way

• Worksheets are handed in, and are lightly graded.

Page 16: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

First look at C - interface file

# define EleType double# define EQ(a, b) (a == b)

struct arrayBag { EleType data[100]; int count;};

void initBag (struct arrayBag * b); … etc

Page 17: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

First function - initialize

void initBag (struct arrayBag * b)

{

}

Page 18: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Add to bag

void addBag (struct arrayBag * b, EleType v)

{

}

Page 19: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Test for contains

int testBag (struct arrayBag * b, EleType v)

{

}

Page 20: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Remove from bag

void removeBag (struct arrayBag * b, EleType v)

{

}

Page 21: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Return size of collection

int bagSize (struct arrayBag * b)

{

}

Page 22: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

How about stack?

void pushStack (struct arrayBag * b, EleType v)

{

}

Page 23: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Test for stack empty

int isStackEmpty (struct arrayBag * b)

{

}

Page 24: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Top of stack

EleType topStack (struct arrayBag * b)

{

}

Page 25: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

Pop top element

void popStack (struct arrayBag * b)

{

}

Page 26: CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing

That’s all for today

• Hand in your worksheets, put your name on them

• See you on thursday