Download presentation
Presentation is loading. Please wait.
1
Data Structures and Analysis (COMP 410)
David Stotts Computer Science Department UNC Chapel Hill
2
Sets and Maps
3
SET ADT Set is an abstract type add: SET x elt SET
remove: SET x elt SET contains : SET x elt boolean size, empty, etc. The add will enforce uniqueness… only one instance of an element may be in a SET A variant of SET called MULTISET will allow multiple instances of an element, then you need a count function for an element (contains is count>0 )
4
MAP Map is an abstract type We insert/retrieve items using the key
a collection stores “pairs” (key,value) We insert/retrieve items using the key Goal is to use the associated value We are using this in the PrQUE spot in min bin heap found from priority want to retrieve and use the data (process, name, diagnosis, etc.)
5
MAP ADT Map is an abstract type get: MAP x key value
put: MAP x key x value MAP remove: MAP x key MAP hasKey : MAP x key Boolean keys: MAP SET of key values: MAP “blob” of value size, empty, etc. “put” will enforce uniqueness of keys… only one of each key several keys may MAP to the same value “keys” returns a SET since keys are unique “values” returns a list/collection of the values (and the elements in the collection might not be unique)
6
MAP is like a Function MAP is like a mathematical function… A math function f(x) y give a domain element x, get back a range element y MAP(k) v or, get(k) v give a key k, get back a value v
7
MAP is like a Function Math functions are often continuous
f(x) 3x where x is a real can’t enumerate all elements of the domain In a MAP the function is discrete, a set of ordered pairs (domain, range) { (123,”dave”), (234,”jane”), (345,”kim”), (456,“pete”), (567,“amy”), (678,”bob”) } keys (domain elements) are a finite enumerable set
8
MAP is like a Function 1 to 1 Keys must be unique
Each key maps to only one value Here is a MAP that might represent SSN mapped to names 1 to 1 Keys must be unique Values are chosen to be unique 123 jane 234 dave No kim 567 pete 345 bob 456 678 amy keys values
9
MAP is like a Function No Not necessarily 1 to 1 Keys must be unique
Each key maps to only one value Here is a MAP that might represent product mapped to price No Not necessarily 1 to 1 Keys must be unique Values can be duplicated fuji 2.50 gala 2.25 redD 1.75 honey goldD 1.25 mac keys values
10
MAP Implementation MAP can be implemented like SET
We store a (key,value) pair rather than only a key Organized as a BST (balanced) we get this put: O(log N) time, worst case and average get: O(log N) time, worst case and average hasKey: O(log N) time, worst case and average remove: O(log N) time, worst case and average
11
MAP Implementation Java Collections has Tree Map Hash Map TreeMap
MAP elements stored in balanced BST Hash Map MAP elements stored using Hash Table Next chapter, after break (chapter 5)
12
Hash MAP Why use hashing for MAP ? Average case times:
put: O(1) get: O(1) hasKey: O(1) remove: O(1) Worst case times: (the downside) put: O(N) get: O(N) hasKey: O(N) remove: O(N) We will see that with care, these are rare
13
Beyond this is just templates
END Beyond this is just templates
14
Example 9 21 18 11 19 3 12 24 16 41
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.