View
6
Download
0
Category
Preview:
Citation preview
Hank Childs, University of OregonNovember 19, 2019
Lecture 15:Abstract Data Types
__. __ .__ __. __ ___ ___ ______ ___ .__ __. _______ _______ ___ .___________. ___| | | | | \ | | | | \ \ / / / | / \ | \ | | | \ | \ / \ | | / \| | | | | \| | | | \ V / | ,----' / ^ \ | \| | | .--. | | .--. | / ^ \ `---| |----` / ^ \| | | | | . ` | | | > < | | / /_\ \ | . ` | | | | | | | | | / /_\ \ | | / /_\ \| `--' | | |\ | | | / . \ __ | `----.__ / _____ \ | |\ | | '--' | | '--' | / _____ \ | | / _____ \\______/ |__| \__| |__| /__/ \__\ (_ ) \______(_ ) /__/ \__\ |__| \__| |_______/ |_______/ /__/ \__\ |__| /__/ \__\
_______.___________..______ __ __ ______ .___________. __ __ .______ _______ _______. / | || _ \ | | | | / || || | | | | _ \ | ____| / | | (----`---| |----`| |_) | | | | | | ,----'`---| |----`| | | | | |_) | | |__ | (----` \ \ | | | / | | | | | | | | | | | | | / | __| \ \
.----) | | | | |\ \----.| `--' | | `----. | | | `--' | | |\ \----.| |____.----) ||_______/ |__| | _| `._____| \______/ \______| |__| \______/ | _| `._____||_______|_______/
Logistics
• VisityourtestFridayNov22,1015am-1230pm• There_is_labthisweek• Nextweek:weirdweek– Labcanceled– NolectureThursNov28th (Thanksgiving)
• Only5lecturesleft(includingthisone)…wehavelotstodo.
Thisistheremainderoftoday’slecture
• Abstractdatatypes– Thinkaboutitfromtheperspectiveoftheuser– NOTtheimplementor
Datatypes
• Simpledatatypes– float,double,int,char,unsignedchar
• Complexdatatypes– Definedwithstructs
• Abstractdatatype– Accomplishedthroughfunctioncalls– Youdon’thavetoknowthedetails
Abstractdatatypes
• Twopieces:– Definebehavior(viafunctionprototypes)– Defineimplementation(viafunctions)
• Youcanhavemorethanoneimplementationforagivenbehavior
(Bad)Example:OneInterface,MultipleImplementations
Badexamplesincewearetalkingaboutdatatypes… thisisjustafunctionthatworksondata
BetterExample:Store/Fetch
• Abstractdatatypehastwomethods:– Store• Takesa“key”anda“value”
– Fetch• Takesa”key”,andreturnsa“value
• Example:– Key==UOID– Value==studentstruct
DETOUR
• Wearegoingtospendthenext15slides&30-40minutestalkingaboutanexample.
• ThisexamplewillthenbeusedtoshowADTs.• TheADTwilldostore/fetch• Weneedtwoideasforhowtodostore/fetch– Arrays– Hashtables
• Observation:• Idon’tneedtoknowanythingaboutADT
• Howisitimplemented?
• HowlongdoesStoretake?
• HowlongdoesFetchtake?
Initialize(&adt);
ToMotivateADT,WeNeedExamplesofDataStructuresThatCanDoStore/Fetch• TwoExamples:– Array– HashTable
OneDataStructureforStore/Fetch:Array• Observation:• Notverygeneric(int key,Studentvalue)
• WhynotpassinStoreFetchArray *insteadofvoid*?
• A:needthislater
voidArrayInitialize(StoreFetchArray *arr)
ComplexityforArray
• Store:O(1)• Fetch:O(N)
HashTable
• Idea:– Createabigarraywithkeysandvalues• (Justlikelastslide!)
– But:don’tinsertstartingfromthebeginning– Instead:insertinto“random”placesinthearray– Nottrulyrandom,asitneedstobereproducible– Typical:takekeyandperformsomemathoperationonit
Thiswillbeawhirlwindintrotohashtables.Wewillreturntothisidealater.
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
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)
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”
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
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”
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
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!
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!
AnotherDataStructureforStore/Fetch:HashTable
• Observation:• Stillnotverygeneric.(int key,Studentvalue)
ComplexityforHashTable
• Store:itdepends– Thingsgowell:O(1)– Thingsgopoorly:O(n)
• Fetch:itdepends– Thingsgowell:O(1)– Thingsgopoorly:O(N)
• Getsintonewtopic… expectedperformance.
ToMotivateADT,WeNeedExamplesofDataStructuresThatCanDoStore/Fetch• TwoExamples:– Array– HashTable
DispatchTable
• Dispatchtable:allowsfor“methods”tobedefineonADT
• Donewithastructthatcontainspointerstofunctions
ADTstruct /dispatchtable&functioncalls
ConstructingADTfromStoreFetchArray orHashTable
ThisiswhyArrayStore/ArrayFetchneededtobevoid*
ADTinaction
Datastructureforspecificdatatype
(example:arrayofStudents)
Datastructurefor*any*datatype
(example:arrayof“void*”)
Abstractdatastructureforspecificdatatype
(example:searchforstudentnames)
Abstractdatastructurefor*any*datatype
(example:searchfor“void*”)
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)
Specificdatastructureforspecifictype
Specificdatastructureforgenerictype
Abstractdatatypes
Nomanagement/organization
ofdata
adt.c
• Fileadt.c isonline.Let’slookatitforawhile.
Project3A
• 3Aisalittlemismatched.• Iamreferringto“Fruit”asadispatchtable,butnotethatFruitisnotreallyanabstractdatatype.Itismorean“abstracttype.”
• Butthemachinery(dispatchtable)isthesame.
Project3A
Project3A
Stacks
Stacks• Adatastructure• 2methods:pushandpop• Sometimesathird:peek
Example:StackofIntegers
Stack:AsymptoticComplexity
• Push:O(1)• Pop:O(1)• Store:O(1)• Fetch:O(n)–àpopeachelementandlook,andthenrestore?
Recommended