LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.
-
Upload
dale-barton -
Category
Documents
-
view
217 -
download
0
Transcript of LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.
![Page 1: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/1.jpg)
LECTURE 38:ORDERED DICTIONARYCSC 212 – Data Structures
![Page 2: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/2.jpg)
MAP VS. DICTIONARY
![Page 3: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/3.jpg)
Collection of Entrys key – searched for value – cared about
Implemented with: List w/ Entrys in
order they were added
List w/ Entrys in increasing order of keys
Hash table
Collection of Entrys key – searched for value – cared about
Implemented with: List w/ Entrys in
order they were added
List w/ Entrys in increasing order of keys
Hash table
Map ADT Dictionary ADT
MAP VS. DICTIONARY
![Page 4: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/4.jpg)
Collection of Entrys key – searched for value – cared about
Implemented with: List w/ Entrys in
order they were added
List w/ Entrys in increasing order of keys
Hash tablekey in at most 1 Entry
Collection of Entrys key – searched for value – cared about
Implemented with: List w/ Entrys in
order they were added
List w/ Entrys in increasing order of keys
Hash tableEntrys can share
key
Map ADT Dictionary ADT
MAP VS. DICTIONARY
![Page 5: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/5.jpg)
Map ADT Dictionary ADT
MAP VS. DICTIONARY
![Page 6: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/6.jpg)
Comparing Data Items
Keeping Entrys ordered means comparing keys Cannot rely upon equals() for all
comparisonsNeed 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
![Page 7: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/7.jpg)
Comparable<E> Interface
In Java as a standard from java.langDefines single method used for
comparison compareTo(E obj) compares instance with obj
Returns int which is either negative, zero, positive
![Page 8: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/8.jpg)
class Team implements Comparable<Team> {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; } else if (myPoints > oPoints) { return 1; }}
COMPARABLE Example
![Page 9: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/9.jpg)
class Team implements Comparable<Team> {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
![Page 10: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/10.jpg)
Entrys maintained in increasing order of key Use array-based List for efficient
searching Simplify process: keys must be Comparable
Ordered Dictionary
![Page 11: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/11.jpg)
Subinterface of Dictionary Classes will define all methods in Dictionary
Use anywhere that Dictionary could be used
Adds efficiency of O(log n) search timesInterface also defines the following
methods: Entry<K,V> first(); Entry<K,V> last(); Iterator<Entry<K,V>> successors(K k); Iterator<Entry<K,V>> predecessors(K k);
ORDEREDDICTIONARY ADT
![Page 12: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/12.jpg)
Simplify life by requiring keys by Comparable Using generic types we can do this Use a special bounded generic type for key
class ODict<K extends Comparable<K>,V> implements OrderedDictionary<K,V> {
ODict<String, Car> happy; ODict<Team, String> glad; ODict<Scanner, Double> sad;
Writing ORDEREDDICTIONARY
![Page 13: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/13.jpg)
class ODict<K extends Comparable<K>,V> implements... {/** Array-based list we use to store the Entrys */private IndexList<Entry<K,V>> table;
public Entry<K,V> 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
![Page 14: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/14.jpg)
public Iterator<Entry<K,V>> successors(K k) {IndexList<Entry<K,V>> 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<K,V> 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 Entrys in order }
// Return the Iterator from the Iterable return retVal.iterator();
}
More ORDEREDDICTIONARY
![Page 15: LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649cfa5503460f949cc07b/html5/thumbnails/15.jpg)
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…