Presentation is loading. Please wait.

Presentation is loading. Please wait.

LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.

Similar presentations


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

1 LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures

2 Normal Computer Bartender I’ll have a Manhattan No problem. That’ll be $2 billion ¾ oz sweet vermouth 2½ oz bourbon 1 dash bitters 1 maraschino cherry 1 twist orange peel

3 not a value Dictionary-based Bartender I’ll have a Manhattan No problem. That’ll be $2 billion key value

4  What we normally associate with word Dictionary  Maintains ordered list of key-value pairs  Must maintain Entry s 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  Entry s with same key can be stored in any order  Requires that keys (searched data) be in order only Ordered Dictionary

5  Iterators should respect ordering of Entry s  Should not be a problem, if Entry s stored in order  Search time is O(log n) when O(1) access time  Array-based structure required to hold Entry s  To get immediate access, needs to access by index  Two structures possible: IndexList & Sequence

6  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 Entry s 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

7  l ow and h igh 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 m idpoint of l ow & h igh  Consider steps for find(7): 13457 8 91114161819 1 3 457891114161819 134 5 7891114161819 1345 7 891114161819 0 0 0 0 m l h m l h m l h l  m  h

8  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 8810 16192299

9 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

10 Comparable 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

11 public class Prof implements Comparable { 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

12  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 simplicity int 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

13  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…


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

Similar presentations


Ads by Google