1 / 28

Lecture 12: MAPS, Searching, & Map ADT

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

rocio
Download Presentation

Lecture 12: MAPS, Searching, & Map ADT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 213 – Large Scale Programming Lecture 12:MAPS, Searching, & Map ADT

  2. 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

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

  4. Smart Parrot Did you grab the keytoopen the chests? We captured the ship,drank their rum, & stole treasure chests. Life is good.

  5. Smart Parrot Did you grab the keytoopen the chests? …

  6. Smart Parrot Did you grab the keytoopen the chests? You know, I could just eat you.

  7. Search Terms • Keygets valuables • We already have key • Want valueas a result of this • Mapworks similarly • Give it keyvaluereturned • Uses Entryto do this work

  8. 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

  9. 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(); }

  10. 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

  11. Too Smart Parrot We got another ship, even more rum, & I got the key, too. It’s party time.

  12. 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.

  13. 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.

  14. Too Smart Parrot Great! What about keys to these treasures? …

  15. Too Smart Parrot Great! What about keys to these treasures? You know, I hear you taste like chicken.

  16. At Most 1 Value Per Key • Entrys have unique keys in a Map • If key exists, replace previous pair in put(key, value)

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

  18. Smart Parrot Nothing was thrown? I thought you’d crash! Well, I tried the key on all the treasures like you suggested. Nothing happened!

  19. Smart Parrot Nothing was thrown? I thought you’d crash! Cook, start the grill!We are having bird tonight!

  20. 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

  21. 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

  22. List (Sequence)-Based Map • List’s perspective of Mapthat it holds Positions elements

  23. List (Sequence)-Based Map • List’s perspective of Mapthat it holds Positions Entrys

  24. List-Based Map Performance • get& removeboth take ____time

  25. List-Based Map Performance • get& removeboth take O(n) time • Scans entire list when key is not found • puttakes ______ time

  26. 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?

  27. Lessons from Polly… • Used to convert the keyinto value • valuescannot share keyand be in same Map • When searching failure is not exceptional

  28. 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?

More Related