CSC 212 – Data Structures Lecture 29: Dictionary ADT.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

© 2004 Goodrich, Tamassia Hash Tables1  
Problem of the Day  What do you get when you cross a mountain climber and a grape?
© 2004 Goodrich, Tamassia Dictionaries   
LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
Chapter 9: Maps, Dictionaries, Hashing Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided.
9.3 The Dictionary ADT    … 01/25/98, Taipei, … 03/04/98, Yunlin, … 02/15/98, Douliou, … 01/20/98,
Dictionaries1 © 2010 Goodrich, Tamassia m l h m l h m l.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Data Structures Lecture 13 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Dictionaries   
CSC311: Data Structures 1 Chapter 9: Maps and Dictionaries Objectives: Map ADT Hash tables –Hash functions and hash code –Compression functions and collisions.
© 2004 Goodrich, Tamassia Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
Binary Search Trees1 Part-F1 Binary Search Trees   
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues.
Maps, Hash Tables and Dictionaries Chapter 10.1, 10.2, 10.3, 10.5.
Maps, Dictionaries, Hashing
CSC 213 – Large Scale Programming. Today’s Goals  Review a new search tree algorithm is needed  What real-world problems occur with old tree?  Why.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
CSC 213 – Large Scale Programming. Today’s Goal  Consider what will be important when searching  Why search in first place? What is its purpose?  What.
LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.
Week 6 - Friday.  What did we talk about last time?  Recursive running time  Fast exponentiation  Merge sort  Introduced the Master theorem.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
LECTURE 36: DICTIONARY CSC 212 – Data Structures.
Maps and Dictionaries Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
1 Searching the dictionary ADT binary search binary search trees.
© 2004 Goodrich, Tamassia Hash Tables1  
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CSC 212 – Data Structures Lecture 26: Hash Tables.
Maps & dictionaries Go&Ta A map models a searchable collection of key-value entries The main operations of a map are for searching, inserting,
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
Maps1 © 2010 Goodrich, Tamassia. Maps2  A map models a searchable collection of key-value entries  The main operations of a map are for searching, inserting,
CSC 212 Hash, Dictionaries, and Skip Lists. Announcements Homework #4 due Tuesday  Attending assessment conf. at UB tomorrow (Drew the short straw; New.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
1 COMP9024: Data Structures and Algorithms Week Eight: Maps and Dictionaries Hui Wu Session 2, 2014
1 COMP9024: Data Structures and Algorithms Week Nine: Maps and Dictionaries Hui Wu Session 1, 2016
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
1 COMP9024: Data Structures and Algorithms Week Eight: Maps and Dictionaries Hui Wu Session 1, 2014
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Dictionaries < > = Dictionaries Dictionaries
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Locators 3 a 1 g 4 e 12/1/2018 4:40 AM Locators Locators
Maps.
Ordered Maps & Dictionaries
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Locators 3 a 1 g 4 e 12/29/2018 7:56 AM Locators Locators
Dictionaries < > = /17/2019 4:20 PM Dictionaries
CH 9 : Maps And Dictionary
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
Dictionaries < > = Dictionaries Dictionaries
Dictionaries 二○一九年九月二十四日 ADT for Map:
Presentation transcript:

CSC 212 – Data Structures Lecture 29: Dictionary ADT

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

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)

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()

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

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

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!”); }

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

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 m l h m l h m l h l  m  h

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

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

Your Turn Get back into groups and do activity

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