Top Banner
LECTURE 12: MAPS, SEARCHING, & MAP ADT CSC 213 – Large Scale Programming
28

Lecture 12: MAPS, Searching, & Map ADT

Feb 16, 2016

Download

Documents

rocio

CSC 213 – Large Scale Programming. Lecture 12: MAPS, Searching, & Map ADT. Searching. Search for unknown data in most cases Consider the reverse: why search for what you have? Seek data related to search terms used Already have idea, want web pages containing terms - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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#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

#Smart Parrot

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

Did you grab the key to open the chests?We captured the ship, drank their rum, & stole treasure chests. Life is good.#Smart Parrot

Did you grab the key to open the chests?#Smart Parrot

You know, I could just eat you.Did you grab the key to open the chests?#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 keyAlso defines usual Collection methodsisEmpty() & size()Several Iterable methods are also definedMethods to use are entries(), keys(), & values()Works with expected data for use in for-each loops#

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 treasures?We got another ship, even more rum, & I got the key, too. Its party time.#

Too Smart ParrotGreat! What about keys to these treasures?

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

Too Smart ParrotGreat! What about keys to these treasures?

#

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

#At Most 1 Value Per KeyEntrys have unique keys in a MapIf key exists, replace previous pair in put(key, value)#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 exceptionsWhen nothing found return null since this is normal#List (Sequence)-Based MapUsing a List 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

#List (Sequence)-Based MapLists perspective of Map that it holds Positions

elements#List (Sequence)-Based MapLists perspective of Map that it holds Positions

Entrys#List-Based Map Performanceget & remove both take ____ time

#List-Based Map Performanceget & remove both take O(n) time Scans entire list when key is not foundput takes ______ time#List-Based Map Performanceget & remove both take O(n) time Scans entire list when key is not foundput takes O(n) time alsoGo through entire List 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 today at 5PMCan go on Angel and start week #5 assignmentDue next Tuesday as is the usual caseThis Friday is first deadline for project phase #1Good design saves time; start doing it nowContinue 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?#