CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ Maps and Dictionaries Map ADT Linked list implementation of Maps 6/30/2016 CS210-Summer 2005, Lecture 15 1 Adaptable Priority Queues Suppose we have an online trading system where orders to purchase and sell a given stock are stored in two priority queues (one for sell orders and one for buy orders) as (p,s) entries: The key, p, of an order is the price The value, s, for an entry is the number of shares A buy order (p,s) is executed when a sell order (p’,s’) with price p’<p is added (the execution is complete if s’>s) A sell order (p,s) is executed when a buy order (p’,s’) with price p’>p is added What if someone wishes to cancel their order before it executes? What if someone wishes to update the price or number of shares for their order? 6/30/2016 CS210-Summer 2005, Lecture 15 2 Methods of the Adaptable PQ ADT remove(e): Remove and return entry e. replaceKey(e,k): Replace with k and return the key of entry e of P; an error condition occurs if k is invalid (that is, k cannot be compared with other keys). replaceValue(e,x): Replace with x and return the value of entry e of P. 6/30/2016 CS210-Summer 2005, Lecture 15 3 Example Operation insert(5,A) insert(3,B) insert(7,C) min() key(e2) remove(e1) replaceKey(e2,9) replaceValue(e3,D) remove(e2) 6/30/2016 Output e1 e2 e3 e2 3 e1 3 C e2 P (5,A) (3,B),(5,A) (3,B),(5,A),(7,C) (3,B),(5,A),(7,C) (3,B),(5,A),(7,C) (3,B),(7,C) (7,C),(9,B) (7,D),(9,B) (7,D) CS210-Summer 2005, Lecture 15 4 Locating Entries In order to implement the operations remove(k), replaceKey(e), and replaceValue(k), we need fast ways of locating an entry e in a priority queue. We can always just search the entire data structure to find an entry e, but there are better ways for locating entries. 6/30/2016 CS210-Summer 2005, Lecture 15 5 Location-Aware Entries A locator-aware entry identifies and tracks the location of its (key, value) object within a data structure Main idea: Since entries are created and returned from the data structure itself, it can return location-aware entries, thereby making future updates easier 6/30/2016 CS210-Summer 2005, Lecture 15 6 List Implementation A location-aware list entry is an object storing key value position (or rank) of the item in the list In turn, the position (or array cell) stores the entry nodes/positions header 2 c 4 c 5 c trailer 8 c entries 6/30/2016 CS210-Summer 2005, Lecture 15 7 Heap Implementation A location-aware heap entry is an object storing 2 d key value position of the entry in the underlying heap In turn, each heap position stores an entry Back pointers are updated during entry swaps 6/30/2016 4 a 8 g 6 b 5 e CS210-Summer 2005, Lecture 15 9 c 8 Performance Using location-aware entries we can achieve the following running times: Method Unsorted List size, isEmpty O(1) insert O(1) min O(n) removeMin O(n) remove O(1) replaceKey O(1) replaceValue O(1) 6/30/2016 Sorted List O(1) O(n) O(1) O(1) O(1) O(n) O(1) CS210-Summer 2005, Lecture 15 Heap O(1) O(log n) O(1) O(log n) O(log n) O(log n) O(1) 9 Maps and Dictionaries The primary use of map or dictionary is to store elements so that they can be located quickly using keys. The motivation for such searches is that each element stores additional useful information besides the search key, but the only way to get at that information is to use the search key. Example: Bank account information. 6/30/2016 CS210-Summer 2005, Lecture 15 10 Maps and Dictionaries Like priority queues, maps and dictionaries store key-value pairs, called entries. Maps require that each key be unique, while dictionaries allow multiple entries to have the same key, just like priority queues. Total order relation is always required for keys in priority queues, it is optional for dictionaries. 6/30/2016 CS210-Summer 2005, Lecture 15 11 The Map ADT A map stores key-value pairs (k, v), which we call entries, where k is the key and v is its corresponding value. The map ADT requires each key be unique. We allow both the keys and the values stored in a map to be of any Object type. 6/30/2016 CS210-Summer 2005, Lecture 15 12 Map ADT size(): Return the number of entries in M. isEmpty(): Test whether M is empty get(k): if the map has an entry e with key k, then returns the value of e, else returns null put(k, v): if map does not have entry with key k, then inserts the entry (k, v) to M and return null. Otherwise replace with v the existing value of the entry with key k and return the old value. remove(k): remove from M the entry with key k and returns its value. If M has no such entry then return null. keys(): returns an iterator of the keys stored in M. values(): returns an iterator of the values associated with keys stored in M. 6/30/2016 CS210-Summer 2005, Lecture 15 13 Map ADT When get(k), put(k, v) and remove(k) are performed on a map that has no entry with key equal to k, we use the convention of returning null. What is the disadvantage of doing this? 6/30/2016 CS210-Summer 2005, Lecture 15 14 Maps in the java.util.package java.util package includes an interface for the Map ADT. Interface java.util.Map does not have any methods to directly return iterators of a Maps keys or values, but it does have methods to return a set of keys or values, which can in turn provide an iterator. 6/30/2016 CS210-Summer 2005, Lecture 15 15 A Simple List-Based Map implementation A simple way of implementing a map is to store its n entries in a list S, implemented as a doubly linked list. get(k), put(k,v) and remove(k), involves simple scan down S looking for an entry with key k. Each of the above methods take O(n) time on a Map with n entries, because each method involves searching through the entire list in the worst case. 6/30/2016 CS210-Summer 2005, Lecture 15 16