290 likes | 372 Views
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
E N D
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 • Get encoded proteins given set of amino acids • Given “borrowed” credit cards, get credit limits • Exacting, but boring, work doing these searches • Make this work ideal for computers & students
Smart Parrot We captured the ship,drank their rum, & stole treasure chests. Life is good.
Smart Parrot Did you grab the keytoopen the chests? We captured the ship,drank their rum, & stole treasure chests. Life is good.
Smart Parrot Did you grab the keytoopen the chests? …
Smart Parrot Did you grab the keytoopen the chests? You know, I could just eat you.
Search Terms • Keygets valuables • We already have key • Want valueas a result of this • Mapworks similarly • Give it keyvaluereturned • Uses Entryto do this work
Entry ADT • Each instance of Position holds 1 element • Abstracts storage of items in a Collection • Useful for Lists: make elements available only • Searching needs more: data has multiple parts • First part is the key: data we have • Value: data we want is second item
Entry Interface • Need a key to get valuables • key used to search – it is what we already have • What we want is the result of search – value interface Entry<K,V> {K key();V value(); }
Map Method Madness, Mmmm… • Describes a searchable Collection • put(K key, V value)adds data as an Entry • remove(K key)removes Entry containing key • get(K key)returns valueassociated with key • Also defines usual Collectionmethods • isEmpty() & size() • Several Iterablemethods are also defined • Methods to use are entries(), keys(), & values() • Works with expected data for use in for-each loops
Too Smart Parrot We got another ship, even more rum, & I got the key, too. It’s party time.
Too Smart Parrot Great! What about keys to these treasures? We got another ship, even more rum, & I got the key, too. It’s party time.
Too Smart Parrot Great! What about keys to these treasures? We got another ship, even more rum, & I got the key, too. It’s party time.
Too Smart Parrot Great! What about keys to these treasures? …
Too Smart Parrot Great! What about keys to these treasures? You know, I hear you taste like chicken.
At Most 1 Value Per Key • Entrys have unique keys in a Map • If 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 you’d crash! Well, I tried the key on all the treasures like you suggested. Nothing happened!
Smart Parrot Nothing was thrown? I thought you’d crash! Cook, start the grill!We are having bird tonight!
Searching Through a Map • Map is a Collection of key-valuepairs • Give it key& get value in return from ADT • Now we have ADT to work with searchable data • Many searches unsuccessful • Unsuccessful search is normal, not unusual • Expected events should NOTthrow exceptions • When nothing found return nullsince this is normal
List (Sequence)-Based Map • Using a List is easiest Map implementation • Using an ADT means it allows any implementation • Uses all elements, so a List does make sense • Only needs to store Entry as the element
List (Sequence)-Based Map • List’s perspective of Mapthat it holds Positions elements
List (Sequence)-Based Map • List’s perspective of Mapthat it holds Positions Entrys
List-Based Map Performance • get& removeboth take ____time
List-Based Map Performance • get& removeboth take O(n) time • Scans entire list when key is not found • puttakes ______ time
List-Based Map Performance • get & removeboth take O(n) time • Scans entire list when key is not found • puttakes O(n)time also • Go through entire Listto see if already has key • If the key is found, replace Entry’s value • Make & add Entry if no match exists in Map • When could this be used?
Lessons from Polly… • Used to convert the keyinto value • valuescannot share keyand be in same Map • When searching failure is not exceptional
Before Next Lecture… • Week #4 assignment due today at 5PM • Can go on Angel and start week #5 assignment • Due next Tuesday as is the usual case • This Friday is first deadline for project phase #1 • Good design saves time; start doing it now • Continue to do reading in your textbook • Learn more about hash & what it means in CSC • How can we tell if a hash is any good? • Hash tables sound cool, but how do we make them?