1 / 30

Lecture 10: MAPS, Searching, & Map ADT

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?

bunny
Download Presentation

Lecture 10: 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 10:MAPS, Searching, & Map ADT

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

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

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

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

  6. Smart Parrot Do you have the keyso we canget its valuables? …

  7. Smart Parrot Do you have the keyso we canget its valuables? You know, I could just eat you.

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

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

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

  11. 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 • Several Iterablemethods are also defined • Methods to use are entries(), keys(), & values() • Iterates over expected data so can use in for(-each) loops • Also defines usual Collectionmethods • isEmpty() & size()

  12. Too Smart Parrot 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 valuables? 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 valuables? We got another ship, even more rum, & I got the key, too. It’s party time.

  15. Too Smart Parrot Great! What about keys to these valuables? …

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

  17. At Most 1 Value Per Key • Entrys have unique keys in a Map • If key exists, put(key,value)replaces existing Entry • Returns prior value forkey in the Map so its not lost • If before call key not in Map, null returned

  18. Smart Parrot 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! Well, I tried the key on all the treasures like you suggested. Nothing happened!

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

  21. 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 • This is normal; return null when nothing found

  22. Sequence-Based Map • Using Sequence 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

  23. Sequence-Based Map • Sequence’s perspective of Mapthat it holds Positions elements

  24. Sequence-Based Map • Outside view of Map and how it is stored Positions Entrys

  25. Sequence-Based Map Performance • get & removeboth take ____ time

  26. Sequence-Based Map Performance • get & remove both take O(n) time • Scans entire Sequencewhen key is not found

  27. Sequence-Based Map Performance • get & remove both take O(n) time • Scans entire Sequencewhen key is not found • puttakes ______ time

  28. Sequence-Based Map Performance • get & remove both take O(n) time • Scans entire Sequencewhen key is not found • puttakes O(n)time also • Go through Sequenceto 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?

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

  30. Before Next Lecture… • Week #4 assignment due Tuesday at 5PM • 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? • Monday is when lab project phase #1 due • Will have time in lab, but then will be the weekend

More Related