300 likes | 419 Vues
In this lecture, we dive into the essentials of searching data structures, focusing on how computers perform searches and the goals behind them. We explore Abstract Data Types (ADTs) used for searching, specifically the Map ADT, and how it functions to enable efficient data management. The session highlights methods for adding, removing, and accessing data within Maps, and discusses the use of sequence-based maps. Additionally, we touch on practical search scenarios and the importance of key-value pairs, ensuring students grasp the fundamental concepts necessary for large-scale programming.
E N D
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? • 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
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 Do you have the keyso we canget its valuables? We captured the ship,drank their rum, & stole treasure chests. Life is good.
Smart Parrot Do you have the keyso we canget its valuables? …
Smart Parrot Do you have the keyso we canget its valuables? 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 • 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()
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 valuables? 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 valuables? 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 valuables? …
Too Smart Parrot Great! What about keys to these valuables? You know, I hear you taste like chicken.
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
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 • This is normal; return null when nothing found
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
Sequence-Based Map • Sequence’s perspective of Mapthat it holds Positions elements
Sequence-Based Map • Outside view of Map and how it is stored Positions Entrys
Sequence-Based Map Performance • get & removeboth take ____ time
Sequence-Based Map Performance • get & remove both take O(n) time • Scans entire Sequencewhen key is not found
Sequence-Based Map Performance • get & remove both take O(n) time • Scans entire Sequencewhen key is not found • puttakes ______ time
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?
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 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