LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

13
LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures

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

LECTURE 37:ORDERED DICTIONARYCSC 212 – Data Structures

Normal Computer Bartender

I’ll have a Manhattan

No problem.That’ll be

$2 billion

¾ oz sweet vermouth2½ oz bourbon 1 dash bitters1 maraschino cherry1 twist orange peel

not avalue

Dictionary-based Bartender

I’ll have a Manhattan

No problem.That’ll be

$2 billion

key

value

What we normally associate with word Dictionary Maintains ordered list of key-value pairs

Must maintain Entrys ordered by their key Faster searching provides performance win

Q: “Mom, how do I spell _______?”A: “Look it up.”

Efficiency gains not just for find & findAll Entrys with same key can be stored in any

order Requires that keys (searched data) be in order

only

Ordered Dictionary

Ordered Dictionary

Iterators should respect ordering of Entrys Should not be a problem, if Entrys stored

in order Search time is O(log n) when O(1) access

time Array-based structure required to hold Entrys

To get immediate access, needs to access by index

Two structures possible: IndexList & Sequence

Finds key using divide-and-conquer approach First of many times we will use this approach

Algorithm has problems solved using recursion Base case 1: No Entrys remain to find the key (Base case 2: At data’s midpoint is matching

key) Recursive Step 1:

If midpoint’s key too high, recursively check lower half

Recursive Step 2: Check upper half, when midpoint’s key too low

Binary Search

Binary Search

low and high are parameters with range to check Would be called with 0 & size() – 1, initially No match possible when l > h

Compare key with one at midpoint of low & high Consider steps for find(7):

1 3 4 5 7 8 9 11 14 16 18 19

1 3 4 5 7 8 9 11 14 16 18 19

1 3 4 5 7 8 9 11 14 16 18 19

1 3 4 5 7 8 9 11 14 16 18 19

0

0

0

0

ml h

ml h

ml h

lm h

find relies on binary search; takes O(log n) time Should also use binary search for findAll() findAll must check for matches at neighbors,

also

remove & insert could use binary search add() may shift elements to make hole for

element Could also need to shift elements in remove() Still take O(n) total time in worst case for each

Using Ordered Dictionary

8 8 10 10 10 10 16 19 22 99

Comparing Data Items

Keeping Entrys 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

Comparable<E> Interface

Standard part of Java from very early in language Interface is in java.lang package

Assumes have a total order relation Follows basic concepts as we normally use

them Requires that if a > b & b > c, then a > c

Defines single method used for comparison compareTo(E obj) compares instance with obj Returns int which is either negative, zero,

positive

public class Prof implements Comparable<Prof> {private String name;

/** Keep Prof instances alphabetized */public int compareTo(Prof obj) { if (name.equals(obj.name)) {/* Return 0 if equal */ return 0; } if (name.equals(“Hertz”)) { /* Best kept first */ return -19316; } else if (obj.name.equals(“Hertz”)) { return 112; } return name.compareTo(obj.name);}

Comparable Example

Could require that keys be Comparable Could then reuse class with many, many types Includes many standard types like String &

Integer Use compareTo() in binary search for

simplicityint c = k.compareTo(list.get(m).getKey());if (c > 0) { binarySearch(k, m + 1, h);} else if (c < 0) { binarySearch(k, l, m - 1);} else { return m;}

Ordered Dictionaries

Continue week #13 assignment Due at usual time, whatever that may be

Work on programming project #4 (due 12/1)

Read section 9.5 in the book What else could we do with a Dictionary? What if we do not have a good hash

supplier? Are we doomed to poor performance?

Before Next Lecture…