Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data...

39
Hank Childs, University of Oregon November 19, 2019 Lecture 15: Abstract Data Types __. __ .__ __. __ ___ ___ ______ ___ .__ __. _______ _______ ___ .___________. ___ | | | | | \| | | | \ \/ / / | / \ | \| | | \ | \ / \ | | / \ | | | | | \| | | | \ V / | ,----' /^\ | \| | | .--. | | .--. | / ^ \ `---| |----` / ^ \ | | | | | . ` | | | > < || / /_\ \ | . ` | | | | | | | | | / /_\ \ || / /_\ \ | `--' | | |\ | | |/.\ __ | `----.__ / _____ \ | |\ | | '--' | | '--' | / _____ \ || / _____ \ \______/ |__| \__| |__| /__/ \__\ (_ ) \______(_ ) /__/ \__\ |__| \__| |_______/ |_______/ /__/ \__\ |__| /__/ \__\ _______.___________..______ __ __ ______ .___________. __ __ .______ _______ _______. / | || _ \ ||||/ || || | | | | _ \ | ____| / | | (----`---| |----`| |_) | | | | | | ,----'`---| |----`| | | | | |_) | | |__ | (----` \ \ || | / | | | | | | || | | | | | / | __| \ \ .----) | || | |\ \----.| `--' | | `----. || | `--' | | |\ \----.| |____.----) | |_______/ |__| | _| `._____| \______/ \______| |__| \______/ | _| `._____||_______|_______/

Transcript of Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data...

Page 1: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Hank Childs, University of OregonNovember 19, 2019

Lecture 15:Abstract Data Types

__. __ .__ __. __ ___ ___ ______ ___ .__ __. _______ _______ ___ .___________. ___| | | | | \ | | | | \ \ / / / | / \ | \ | | | \ | \ / \ | | / \| | | | | \| | | | \ V / | ,----' / ^ \ | \| | | .--. | | .--. | / ^ \ `---| |----` / ^ \| | | | | . ` | | | > < | | / /_\ \ | . ` | | | | | | | | | / /_\ \ | | / /_\ \| `--' | | |\ | | | / . \ __ | `----.__ / _____ \ | |\ | | '--' | | '--' | / _____ \ | | / _____ \\______/ |__| \__| |__| /__/ \__\ (_ ) \______(_ ) /__/ \__\ |__| \__| |_______/ |_______/ /__/ \__\ |__| /__/ \__\

_______.___________..______ __ __ ______ .___________. __ __ .______ _______ _______. / | || _ \ | | | | / || || | | | | _ \ | ____| / | | (----`---| |----`| |_) | | | | | | ,----'`---| |----`| | | | | |_) | | |__ | (----` \ \ | | | / | | | | | | | | | | | | | / | __| \ \

.----) | | | | |\ \----.| `--' | | `----. | | | `--' | | |\ \----.| |____.----) ||_______/ |__| | _| `._____| \______/ \______| |__| \______/ | _| `._____||_______|_______/

Page 2: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Logistics

• VisityourtestFridayNov22,1015am-1230pm• There_is_labthisweek• Nextweek:weirdweek– Labcanceled– NolectureThursNov28th (Thanksgiving)

• Only5lecturesleft(includingthisone)…wehavelotstodo.

Page 3: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Thisistheremainderoftoday’slecture

• Abstractdatatypes– Thinkaboutitfromtheperspectiveoftheuser– NOTtheimplementor

Page 4: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Datatypes

• Simpledatatypes– float,double,int,char,unsignedchar

• Complexdatatypes– Definedwithstructs

• Abstractdatatype– Accomplishedthroughfunctioncalls– Youdon’thavetoknowthedetails

Page 5: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Abstractdatatypes

• Twopieces:– Definebehavior(viafunctionprototypes)– Defineimplementation(viafunctions)

• Youcanhavemorethanoneimplementationforagivenbehavior

Page 6: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

(Bad)Example:OneInterface,MultipleImplementations

Badexamplesincewearetalkingaboutdatatypes… thisisjustafunctionthatworksondata

Page 7: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

BetterExample:Store/Fetch

• Abstractdatatypehastwomethods:– Store• Takesa“key”anda“value”

– Fetch• Takesa”key”,andreturnsa“value

• Example:– Key==UOID– Value==studentstruct

Page 8: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

DETOUR

• Wearegoingtospendthenext15slides&30-40minutestalkingaboutanexample.

• ThisexamplewillthenbeusedtoshowADTs.• TheADTwilldostore/fetch• Weneedtwoideasforhowtodostore/fetch– Arrays– Hashtables

Page 9: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

• Observation:• Idon’tneedtoknowanythingaboutADT

• Howisitimplemented?

• HowlongdoesStoretake?

• HowlongdoesFetchtake?

Initialize(&adt);

Page 10: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

ToMotivateADT,WeNeedExamplesofDataStructuresThatCanDoStore/Fetch• TwoExamples:– Array– HashTable

Page 11: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

OneDataStructureforStore/Fetch:Array• Observation:• Notverygeneric(int key,Studentvalue)

• WhynotpassinStoreFetchArray *insteadofvoid*?

• A:needthislater

voidArrayInitialize(StoreFetchArray *arr)

Page 12: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

ComplexityforArray

• Store:O(1)• Fetch:O(N)

Page 13: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

• Idea:– Createabigarraywithkeysandvalues• (Justlikelastslide!)

– But:don’tinsertstartingfromthebeginning– Instead:insertinto“random”placesinthearray– Nottrulyrandom,asitneedstobereproducible– Typical:takekeyandperformsomemathoperationonit

Thiswillbeawhirlwindintrotohashtables.Wewillreturntothisidealater.

Page 14: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

NUL NUL NUL NUL NUL NUL NUL NUL NUL NUL

Tableindex

Key

Student

Page 15: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 …34 -1 -1 -1 -1 -1

NUL NUL NUL NUL xFF NUL NUL NUL NUL NUL

Tableindex

Key

Student

Store:UO_ID:951001234Student:“xFF”

Idea:TurnUO_IDintoanindex.

Inthiscase,%10.(muchmorecomplexideas)

Page 16: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 …34 -1 ..66 -1 -1 -1

NUL NUL NUL NUL xFF NUL xAF NUL NUL NUL

Tableindex

Key

Student

Store:UO_ID:951003266Student:“xAF”

Page 17: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 …34 ...44 ..66 -1 -1 -1

NUL NUL NUL NUL xFF xAA xAF NUL NUL NUL

Tableindex

Key

Student

Store:UO_ID:951012344Student:“xAA”

Idea:Slot4isfull… justusethe

nextslot(slot5).Keepgoinguntilyoufind

one,includingwraparounds

Page 18: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 …34 ...44 ..66 …45 -1 -1

NUL NUL NUL NUL xFF xAA xAF xB8 NUL NUL

Tableindex

Key

Student

Store:UO_ID:951012345Student:“xB8”

Page 19: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 …34 ...44 ..66 …45 -1 -1

NUL NUL NUL NUL xFF xAA xAF xB8 NUL NUL

Tableindex

Key

Student

Fetch:UO_ID:951045323

EASY:NULL

Page 20: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 …34 ...44 ..66 …45 -1 -1

NUL NUL NUL NUL xFF xAA xAF xB8 NUL NUL

Tableindex

Key

Student

Fetch:UO_ID:951012345

Morework… walkfromindex5to6to7… foundit!

Page 21: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

HashTable

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 …34 ...44 ..66 …45 -1 -1

NUL NUL NUL NUL xFF xAA xAF xB8 NUL NUL

Tableindex

Key

Student

Fetch:UO_ID:951012355

Morework… walkfromindex5to6to7to8… not

there!

Page 22: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

AnotherDataStructureforStore/Fetch:HashTable

• Observation:• Stillnotverygeneric.(int key,Studentvalue)

Page 23: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

ComplexityforHashTable

• Store:itdepends– Thingsgowell:O(1)– Thingsgopoorly:O(n)

• Fetch:itdepends– Thingsgowell:O(1)– Thingsgopoorly:O(N)

• Getsintonewtopic… expectedperformance.

Page 24: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

ToMotivateADT,WeNeedExamplesofDataStructuresThatCanDoStore/Fetch• TwoExamples:– Array– HashTable

Page 25: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

DispatchTable

• Dispatchtable:allowsfor“methods”tobedefineonADT

• Donewithastructthatcontainspointerstofunctions

Page 26: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

ADTstruct /dispatchtable&functioncalls

Page 27: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

ConstructingADTfromStoreFetchArray orHashTable

ThisiswhyArrayStore/ArrayFetchneededtobevoid*

Page 28: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

ADTinaction

Page 29: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Datastructureforspecificdatatype

(example:arrayofStudents)

Datastructurefor*any*datatype

(example:arrayof“void*”)

Abstractdatastructureforspecificdatatype

(example:searchforstudentnames)

Abstractdatastructurefor*any*datatype

(example:searchfor“void*”)

Page 30: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

WhenDoIUseWhat?

Datastructureforspecificdatatype

(example:arrayofStudents)

Datastructurefor*any*datatype

(example:arrayof“void*”)

Abstractdatastructureforspecificdatatype

(example:searchforstudentnames)

Abstractdatastructurefor*any*datatype

(example:searchfor”void*”)

Whenwritingsomethingformyspecificprogram

WhenIhavedevelopedadatastructureIwantotherstouse

WhenIwanttosolveaproblemformany,manypeople

Never?(butalsowhatwejustdid)

Page 31: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Specificdatastructureforspecifictype

Specificdatastructureforgenerictype

Abstractdatatypes

Nomanagement/organization

ofdata

Page 32: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

adt.c

• Fileadt.c isonline.Let’slookatitforawhile.

Page 33: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Project3A

• 3Aisalittlemismatched.• Iamreferringto“Fruit”asadispatchtable,butnotethatFruitisnotreallyanabstractdatatype.Itismorean“abstracttype.”

• Butthemachinery(dispatchtable)isthesame.

Page 34: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Project3A

Page 35: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Project3A

Page 36: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Stacks

Page 37: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Stacks• Adatastructure• 2methods:pushandpop• Sometimesathird:peek

Page 38: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Example:StackofIntegers

Page 39: Lecture 15: Abstract Data Typesix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec15.pdf · Data types •Simple data types –float, double, int, char, unsigned char •Complex data

Stack:AsymptoticComplexity

• Push:O(1)• Pop:O(1)• Store:O(1)• Fetch:O(n)–àpopeachelementandlook,andthenrestore?