Download presentation
Presentation is loading. Please wait.
Published byBeryl Sullivan Modified over 8 years ago
1
LECTURE 36: DICTIONARY CSC 212 – Data Structures
2
At simplest level, Map is collection of Entry s These key-value pairs serve as the basic data in a Map size() & isEmpty() methods analyzes Entry s Searchable data stored using Map s put() adds an Entry so key is mapped to the value get() retrieves value associated with key from Map remove() deletes the key & its value (an Entry ) At most one value per key using a Map What is a MAP?
3
MAP great when you want only one value for a key Credit card number goes to one account One person has a given social security number Only one definition for a word in the dictionary Where MAPS are Used
5
May want to associate multiple values per key key mapped to List of values is possible solution user But this means MAP’s user must handle complexity Limitations of MAP
6
very DICTIONARY ADT very similar to MAP Searching through data is goal of each Both are collections of Entry s Associating keys with values using both concepts DICTIONARY can map multiple values to one key Like (real-world) dictionaries with definitions & words Legal to associate only 1 value to a given key “awesome” Also many Entry s with same key but different value “cool”“cool” Dictionary ADT
7
Changes for Dictionary MapDictionary V put(k,v)Entry insert(k,v) V remove(k)Entry remove(e) V get(k) Entry find(k) Iterable findAll(k)
8
Simplest implementation uses unordered List Question is: PositionList or IndexList Consider the work we will be doing find & findAll may scan through List Check each Position to see if it contains a match find method will stop at first matching key Entire List scanned for matching keys in findAll Time needed for PositionList: Time needed for IndexList: Using an Unordered List
9
May scan through List in call to remove Check each element to see if it is a match Stop when we find Entry and remove it insert adds Entry to end of the List Keys are not unique, so no checks are needed Entry created for key-value pair is returned Time needed for PositionList: Time needed for IndexList: Unordered List Details
10
Great when adds are common & frequent… …but searching & removing slower than molasses When would we ever need this implementation? Unordered List
11
Great when adds are common & frequent… …but searching & removing slower than molasses When would we ever need this implementation? Phone records E-mail backups Tax receipts by the IRS Bank’s copy of your credit card orders Unordered List
12
Could also use a hash table Entry s definitely unordered within the table We hope it provides O(1) time for most methods find() & remove() unchanged from before Others handle duplicate keys like a collision Until null or AVAILABLE found check the indices insert() will just keep trying the next index More may exist that findAll() needs to find Hash Based Approach
13
Start week #13 assignment Due at usual time, whatever that may be Work on programming project #4 (due 12/1) 2 nd Chance Midterm Thurs., 9:30 – 10:20 in WTC205 Finish section 9.3 of the book How else could we implement a Dictionary? What if we do not have a good hash supplier? Are we doomed to poor performance? Before Next Lecture…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.