![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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/1.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/2.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/5.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/6.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/7.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/8.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/16.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/21.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/23.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/24.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/25.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649f135503460f94c26d22/html5/thumbnails/26.jpg)
That’s all for today
• Hand in your worksheets, put your name on them
• See you on thursday