CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.
-
Upload
hugh-greer -
Category
Documents
-
view
220 -
download
0
description
Transcript of CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.
![Page 1: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/1.jpg)
CSC 212 –Data Structures
Lecture 31:Last Word On Dictionaries
![Page 2: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/2.jpg)
Problem of the Day
What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
The highlighted phrase has fewer than 25 syllables, so this cannot exist!
![Page 3: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/3.jpg)
Comparing Data Items
Ordering entries means comparing keys Comparison depends on key’s type
Use <, >, == for numeric dataUse compareTo() for StringsWhat about Car, IMClient, or Prof instances?
Want abstract approach to this problemNo OrderedDictionary class for each key type
![Page 4: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/4.jpg)
Comparator ADT
Implements a total order relationObjects less than, equal to, or greater thanFollows rule that if a > b & b > c, then a > c
Comparator’s methods are abstract Implementation is specific to key typeBut still independent of the key class
Can reweight keys by changing how comparator orders them
![Page 5: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/5.jpg)
Comparator Interface
public interface Comparator<E> { public int compare(E x, E y);}public class StrComp implements Comparator<String> { public int compare(String x, String y){ return(x.compareTo(y));
}}
Returns integer < 0 when x < y 0 when x == y > 0 when x > y
![Page 6: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/6.jpg)
Ordered Dictionaries
Ordered dictionary constructor includes Comparator parameter Now works with any type of key!
Can rewrite binary search more abstractly as:if (c.compare(key, table[m].getKey()) > 0) { l = m + 1;} else if (c.compare(key, table[m].getKey()) < 0) { h = m - 1;} else { return m;}
![Page 7: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/7.jpg)
Ordered Dictionary
Key feature of ordered dictionary: they maintain entries in orderCan be a performance win (faster searching)But can also be an important feature
Think of how first use a dictionary:Q: “Mom, how do I spell _______?”A: “Look it up.”
Cannot do this with Dictionary ADT
![Page 8: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/8.jpg)
Ordered Dictionary Interface
public interface OrderedDictionary<K,V> extends Dictionary<K,V> { public Entry<K,V> first(); // Entry with smallest key public Entry<K,V> last(); // Entry with largest key public Iterator<Entry<K,V>> successors(K k); public Iterator<Entry<K,V>> predecessors(K k);
}
Includes all methods in Dictionary Includes first() & last() methodssuccessors() iterates over larger keyspredecessors() iterates through smaller keysMay not include entries with the key k
![Page 9: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/9.jpg)
Writing an Ordered Dictionary
public class ODict<K,V> implements... {private IndexList<Entry<K,V>> table;private Comparator<K> comp;
public Entry<K,V> first() throws EmptyDictionaryException { try { return table.get(0); } catch (NoSuchElementException e) { throw EmptyDictionaryException(“Dummkopf.”); }
}
![Page 10: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/10.jpg)
Writing an Ordered Dictionarypublic Iterator<Entry<K,V>> successors(K k) {IteratorClass<Entry<K,V>> retVal = new ...; int rank = binSearch(k);while (rank < table.size()) { if (comp.compare(k, table.get(rank).getKey)==0) { rank++; } else { break; }}for (; rank < table.size(); rank++) { retVal.addLast(table.get(rank));}return retVal;
}
![Page 11: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/11.jpg)
Why Should We Care?
We often care about ordering dataPricesSchedulesQPAsNumber of watts consumedProcessor speed
![Page 12: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/12.jpg)
Your Turn
Get back into groups and do activity
![Page 13: CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.](https://reader035.fdocuments.us/reader035/viewer/2022081806/5a4d1b447f8b9ab0599a2dbd/html5/thumbnails/13.jpg)
Before Next Lecture…
Keep up with your reading!Cannot stress this enough
Finish Week #12 Assignment Start Programming Assignment #4 Prepare for Midterm #2 next Monday
Will also be open book, open noteCovers from last midterm through this week