Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.

Similar presentations


Presentation on theme: "CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries."— Presentation transcript:

1 CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries

2 Problem of the Day 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!

3 Comparing Data Items Ordering entries means comparing keys Comparison depends on key’s type  Use, == for numeric data  Use compareTo() for Strings  What about Car, IMClient, or Prof instances? Want abstract approach to this problem  No OrderedDictionary class for each key type

4 Comparator ADT Implements a total order relation  Objects less than, equal to, or greater than  Follows rule that if a > b & b > c, then a > c Comparator’s methods are abstract  Implementation is specific to key type  But still independent of the key class Can reweight keys by changing how comparator orders them

5 Comparator Interface public interface Comparator { public int compare(E x, E y); } public class StrComp implements Comparator { public int compare(String x, String y){ return(x.compareTo(y)); } Returns integer 0 when x > y

6 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; }

7 Ordered Dictionary Key feature of ordered dictionary: they maintain entries in order  Can 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

8 Ordered Dictionary Interface public interface OrderedDictionary extends Dictionary { public Entry first(); // Entry with smallest key public Entry last(); // Entry with largest key public Iterator > successors(K k); public Iterator > predecessors(K k); } Includes all methods in Dictionary  Includes first() & last() methods  successors() iterates over larger keys  predecessors() iterates through smaller keys  May not include entries with the key k

9 Writing an Ordered Dictionary public class ODict implements... { private IndexList > table; private Comparator comp; public Entry first() throws EmptyDictionaryException { try { return table.get(0); } catch (NoSuchElementException e) { throw EmptyDictionaryException(“Dummkopf.”); } }

10 Writing an Ordered Dictionary public Iterator > successors(K k) { IteratorClass > 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; }

11 Why Should We Care? We often care about ordering data  Prices  Schedules  QPAs  Number of watts consumed  Processor speed

12 Your Turn Get back into groups and do activity

13 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 note  Covers from last midterm through this week


Download ppt "CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries."

Similar presentations


Ads by Google