lecture 10: maps, searching, & map adt

30
LECTURE 10: MAPS, SEARCHING, & MAP ADT CSC 213 – Large Scale Programming

Upload: bunny

Post on 22-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

CSC 213 – Large Scale Programming. Lecture 10: MAPS, Searching, & Map ADT. Today’s Goal. Consider the basics of searchable data How do we search using a computer? What are our goals while searching? ADTs used for search & how would they work? - PowerPoint PPT Presentation

TRANSCRIPT

Lecture 12: Maps

Search TermsKey gets valuablesWe already have keyWant value as a result of this

Map works similarlyGive it key value returnedUses Entry to do this work#Todays GoalConsider the basics of searchable dataHow do we search using a computer?What are our goals while searching?ADTs used for search & how would they work?Most critically, where the $&*#%$# are my keys?How does Map ADT work & enable searching?Methods to add, remove, and access data?Sequence-based Maps would be implemented how?When & why would we use Sequence-based Maps

Smart Parrot

We captured the ship, drank their rum, & stole treasure chests. Life is good.#Smart Parrot

Do you have the key so we canget its valuables?We captured the ship, drank their rum, & stole treasure chests. Life is good.#Smart Parrot

Do you have the key so we canget its valuables?#You know, I could just eat you.Smart Parrot

Do you have the key so we canget its valuables?#Entry ADTEach instance of Position holds 1 elementAbstracts storage of items in a CollectionUseful for Lists: make elements available onlySearching needs more: data has multiple partsFirst part is the key: data we haveValue: data we want is second item

#Entry InterfaceNeed a key to get valuableskey used to search it is what we already haveWhat we want is the result of search value

interface Entry { K key(); V value();}

#Map Method Madness, MmmmDescribes a searchable Collection put(K key, V value) adds data as an Entryremove(K key) removes Entry containing keyget(K key) returns value associated with keySeveral Iterable methods are also definedMethods to use are entries(), keys(), & values()Iterates over expected data so can use in for(-each) loopsAlso defines usual Collection methodsisEmpty() & size()#

We got another ship, even more rum, & I got the key, too. Its party time.Too Smart Parrot#

Too Smart ParrotGreat! What about keys to these valuables?We got another ship, even more rum, & I got the key, too. Its party time.#

Too Smart ParrotGreat! What about keys to these valuables?We got another ship, even more rum, & I got the key, too. Its party time.

#

Too Smart ParrotGreat! What about keys to these valuables?

#

Too Smart ParrotGreat! What about keys to these valuables?You know, I hearyou taste like chicken.

#At Most 1 Value Per KeyEntrys have unique keys in a MapIf key exists, put(key, value) replaces existing EntryReturns prior value for key in the Map so its not lostIf before call key not in Map, null returned#Smart Parrot

Well, I tried the key on all the treasures like you suggested.Nothing happened!#Smart Parrot

Nothing was thrown? I thought youd crash!

Well, I tried the key on all the treasures like you suggested.Nothing happened!#Smart Parrot

Nothing was thrown? I thought youd crash!

Cook, start the grill!We are having bird tonight!#Searching Through a MapMap is a Collection of key-value pairsGive it key & get value in return from ADTNow we have ADT to work with searchable dataMany searches unsuccessfulUnsuccessful search is normal, not unusualExpected events should NOT throw exceptionsThis is normal; return null when nothing found#Sequence-Based MapUsing Sequence is easiest Map implementationUsing an ADT means it allows any implementationUses all elements, so a List does make senseOnly needs to store Entry as the element

#Sequence-Based MapSequences perspective of Map that it holds Positionselements

#Sequence-Based MapOutside view of Map and how it is stored PositionsEntrys

#Sequence-Based Map Performanceget & remove both take ____ time #Sequence-Based Map Performanceget & remove both take O(n) time Scans entire Sequence when key is not found

#Sequence-Based Map Performanceget & remove both take O(n) timeScans entire Sequence when key is not foundput takes ______ time#Sequence-Based Map Performanceget & remove both take O(n) timeScans entire Sequence when key is not foundput takes O(n) time alsoGo through Sequence to see if already has keyIf the key is found, replace Entrys valueMake & add Entry if no match exists in Map

When could this be used?

#Lessons from PollyUsed to convert the key into value values cannot share key and be in same MapWhen searching failure is not exceptional

#Before Next LectureWeek #4 assignment due Tuesday at 5PMContinue to do reading in your textbookLearn more about hash & what it means in CSCHow can we tell if a hash is any good?Hash tables sound cool, but how do we make them?Monday is when lab project phase #1 dueWill have time in lab, but then will be the weekend#