Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 – Data Structures Lecture 29: Dictionary ADT.

Similar presentations


Presentation on theme: "CSC 212 – Data Structures Lecture 29: Dictionary ADT."— Presentation transcript:

1 CSC 212 – Data Structures Lecture 29: Dictionary ADT

2 Maps key to 1 or more values Used for  Google, credit card authorizations, DNS  Databases Like Map, Dictionary works with Entry  Key used for searching  Value is data you care about

3 Example OperationOutputDictionary insert(5,A)(5,A)(5,A) insert(7,B)(7,B)(5,A),(7,B) insert(2,C)(2,C)(5,A),(7,B),(2,C) insert(8,D)(8,D)(5,A),(7,B),(2,C),(8,D) insert(2,E)(2,E)(5,A),(7,B),(2,C),(8,D),(2,E) find(7)(7,B)(5,A),(7,B),(2,C),(8,D),(2,E) find(4)null(5,A),(7,B),(2,C),(8,D),(2,E) find(2)(2,C)(5,A),(7,B),(2,C),(8,D),(2,E) findAll(2)(2,C),(2,E)(5,A),(7,B),(2,C),(8,D),(2,E) size()5(5,A),(7,B),(2,C),(8,D),(2,E) remove(find(5))(5,A)(7,B),(2,C),(8,D),(2,E) find(5)null(7,B),(2,C),(8,D),(2,E)

4 Dictionary ADT Methods find(k)  Return an Entry with key k, if exists, else, return null findAll(k)  Return Iterator over all Entrys with key k insert(k, v)  Instantiates Entry with key k & value v and adds it to dictionary remove(e)  Remove Entry e from dictionary entries()  Return Iterator over all Entrys in dictionary values()  Return Iterator over all values in dictionary size(), isEmpty()

5 Dictionary Implementation Similar to how we implement Map  Usually use Sequence or hash table 2 different “flavors” of implementation  Ordered – Entrys ordered by key and value  Unordered – Resembles my desk Ordered Dictionary does not use hash  Hard to maintain order using hash  Already has tricks to limit searching costs

6 Unordered Dictionary Sequence-based implementation  Adds Entrys at first rank/Position – insert needs O(1) time  Traverse entire Sequence to find given key – find & remove needs O(n) time  Good only when search & removal are rare Hash table-based implementation  Must hash multiple Entrys with same key  Otherwise identical to how Map implemented

7 List-based Implementation public Entry insert(K k, V v) { Entry e = new *Entry (k, v); seq.insertFirst(e); return e; } public Entry remove(Entry e) { for (Position > pos : seq.positions()) { if (pos.element().equals(e)) { seq.remove(pos); return e; } } // Uh-oh, e is not in the Dictionary! throw new InvalidEntryException(“Idiot!”); }

8 Ordered Dictionary Sorts Entrys by key and value  Iterator should respect this order  Must also know ordering of key & value (We will discuss this more on Wednesday)  Hard to do with hash tables Speeds search for certain implementations  O(log n) search time if O(1) access time  Does not work if Sequence uses a linked list

9 Binary Search Find key k by halving candidates at each step Compare with key at m idpoint of l ow and h igh  If m ’s key too low, l = m + 1  If m ’s key too high, h = m – 1  If l > h, no match exists 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

10 Using Ordered Sequence find uses binary search; takes O(log n) time insert finds closest Entry with binary search  Calls seq.add() to add at rank & maintain ordering  seq.add() may shift n  2 Entrys, for O(n) total time remove finds matching Entry via binary search  Calls seq.remove() to remove from Sequence  May shift n  2 Entrys, for O(n) total time Good for searching rarely changing data

11 findAll findAll() always takes O(n) time  Dictionary “flavor” does not change it If unordered, iterate over each Entry  If key matches, add value to Iterator If ordered, use binary search to find match  Must then go through all neighboring keys  If all Entrys have same key, takes O(n) time

12 Your Turn Get back into groups and do activity

13 Before Next Lecture… Keep up with your reading! Start Week #12 Assignment Complete Programming Assignment #3 Prepare for Midterm #2 next Monday  Will also be open book, open note  Covers from last midterm through this week Note: Wednesday’s lecture has changed


Download ppt "CSC 212 – Data Structures Lecture 29: Dictionary ADT."

Similar presentations


Ads by Google