Presentation is loading. Please wait.

Presentation is loading. Please wait.

LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.

Similar presentations


Presentation on theme: "LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures."— Presentation transcript:

1 LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures

2 MAP VS. DICTIONARY

3  Collection of Entry s  key – searched for  value – cared about  Implemented with:  List w/ Entry s in order they were added  List w/ Entry s in increasing order of keys  Hash table  Collection of Entry s  key – searched for  value – cared about  Implemented with:  List w/ Entry s in order they were added  List w/ Entry s in increasing order of keys  Hash table Map ADTDictionary ADT MAP VS. DICTIONARY

4  Collection of Entry s  key – searched for  value – cared about  Implemented with:  List w/ Entry s in order they were added  List w/ Entry s in increasing order of keys  Hash table  key in at most 1 Entry  Collection of Entry s  key – searched for  value – cared about  Implemented with:  List w/ Entry s in order they were added  List w/ Entry s in increasing order of keys  Hash table  Entry s can share key Map ADTDictionary ADT MAP VS. DICTIONARY

5 Map ADTDictionary ADT MAP VS. DICTIONARY

6 Comparing Data Items  Keeping Entry s ordered means comparing keys  Cannot rely upon equals() for all comparisons  Need to find smaller, bigger, & even-steven-equals  Use, == when keys limited to numeric type  String also has simple method: compareTo()  Do not want to rewrite for each key type  But this requires a general way to compare keys

7 Comparable Interface  In Java as a standard from java.lang  Defines single method used for comparison  compareTo(E obj) compares instance with obj  Returns int which is either negative, zero, positive

8 class Team implements Comparable { private int wins, losses, lossesInOTSO; private int points() { return (wins * 2) + (lossesInOTSO); } /** Order Team instances in standings */ public int compareTo(Team o) { int myPoints = points(); int oPoints = o.points(); if (myPoints == oPoints) { return 0; } else if (myPoints oPoints) { return 1; } } COMPARABLE Example

9 class Team implements Comparable { private int wins, losses, lossesInOTSO; private int points() { return (wins * 2) + (lossesInOTSO); } /** Order Team instances in standings */ public int compareTo(Team o) { int myPoints = points(); int oPoints = o.points(); return (myPoints - oPoints); } Simpler COMPARABLE

10  Entry s maintained in increasing order of key  Use array-based List for efficient searching  Simplify process: keys must be Comparable Ordered Dictionary

11  Subinterface of Dictionary  Classes will define all methods in Dictionary  Use anywhere that Dictionary could be used  Adds efficiency of O(log n) search times  Interface also defines the following methods:  Entry first();  Entry last();  Iterator > successors(K k);  Iterator > predecessors(K k); ORDEREDDICTIONARY ADT

12  Simplify life by requiring keys by Comparable  Using generic types we can do this  Use a special bounded generic type for key class ODict,V> implements OrderedDictionary { ODict happy; ODict glad; ODict sad;  Writing ORDEREDDICTIONARY

13 class ODict,V> implements... { /** Array-based list we use to store the Entry s */ private IndexList > table; public Entry first() throws EmptyDictionaryException { // Check if we need to throw an Exception if (table.isEmpty()) { throw new EmptyDictionaryException(“No Entry”); } else { // Return the Entry with the smallest key return table.get(0); } } Writing ORDEREDDICTIONARY

14 public Iterator > successors(K k) { IndexList > retVal = // instantiation goes here // Loop from Entry with largest key back to Entry with smallest key for (int i = table.size()–1; i >= 0; i--) { Entry ent = table.get(i); // Stop once we find an Entry with a smaller or equal key if (k.compareTo(ent) >= 0) { break; } retVal.add(0, ent); // Keep Entry s in order } // Return the Iterator from the Iterable return retVal.iterator(); } More ORDEREDDICTIONARY

15  Finish week #13 assignment  Due at usual time, whatever that may be  Work on programming project #4 (due 12/1)  See you at lab tomorrow and Happy Thanksgiving! Before Next Lecture…


Download ppt "LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures."

Similar presentations


Ads by Google