Top Banner
LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures
13

LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

Dec 31, 2015

Download

Documents

Derick Reynolds
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

LECTURE 37:ORDERED DICTIONARYCSC 212 – Data Structures

Page 2: LECTURE 37: ORDERED DICTIONARY CSC 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

Page 3: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

not avalue

Dictionary-based Bartender

I’ll have a Manhattan

No problem.That’ll be

$2 billion

key

value

Page 4: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 5: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 6: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 7: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 8: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 9: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 10: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 11: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 12: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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

Page 13: LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

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…