LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.

Post on 17-Dec-2015

218 views 0 download

Transcript of LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.

LECTURE 38:ORDERED DICTIONARYCSC 212 – Data Structures

MAP VS. DICTIONARY

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

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

Map ADT Dictionary ADT

MAP VS. DICTIONARY

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

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

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

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

Entrys maintained in increasing order of key Use array-based List for efficient

searching Simplify process: keys must be Comparable

Ordered Dictionary

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

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

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

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

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…