Sie sind auf Seite 1von 3

Entry and Priority Queue ADTs

Adaptable Priority 3 a
 A priority queue stores  Priority Queue ADT:
a collection of entries insert(e)
Queues

inserts entry e
 Typically, an entry is a  removeMin()
pair (key, value), where removes the entry
the key indicates the with smallest key
priority  min()
returns, but does not
 The priority queue is remove, an entry
5 g 4 e
associated with a with smallest key
comparator C, that  size(), empty()
compares two entries
© 2010 Goodrich, Tamassia Adaptable Priority Queues 1 © 2010 Goodrich, Tamassia Adaptable Priority Queues 2

Methods of the Adaptable Priority


Example Queue ADT
Online trading system where orders to purchase and

sell a stock are stored in two priority queues (one for
 insert(e): Insert the entry e into P and
sell orders and one for buy orders) as (p,s) entries: return a position referring to this entry
 The key, p, of an order is the price
 remove(p): Remove from P the entry
The value, s, for an entry is the number of shares
referenced by position p

 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
 replace(p, e): Replace with e the
price p’>p is added (the execution is complete if s’>s) element associated with the entry
 What if someone wishes to cancel their order before referenced by p and return the position
it executes?
of the altered entry
 What if someone wishes to update the price or
number of shares for their order?
© 2010 Goodrich, Tamassia Adaptable Priority Queues 3 © 2010 Goodrich, Tamassia Adaptable Priority Queues 4
Example Locating Entries
Operation Output P
insert(5,A) p1 (5,A)  In order to implement the operations
insert(3,B) p2 (3,B), (5,A) remove(p) and replace(p), and we need
insert(7,C) p3 (3,B), (5,A), (7,C)
fast ways of locating an entry p in a
min() p2 (3,B), (5,A), (7,C)
p2.key() 3 (3,B), (5,A), (7,C) priority queue
remove(p1) – (3,B), (7,C)
replace(p2,(9,D)) p4 (7,C), (9,D)
replace(p3,(7,E)) p5 (7,E), (9,D)
remove(p4) – (7,D)

© 2010 Goodrich, Tamassia Adaptable Priority Queues 5 © 2010 Goodrich, Tamassia Adaptable Priority Queues 6

Location-Aware Entries List Implementation


 A locator-aware entry identifies and tracks  A location-aware list entry is an object storing
the location of its (key, value) object within a  key
data structure  value
position (or rank) of the item in the list
 Intuitive notion: 

 Coat claim check  In turn, the position (or array cell) stores the entry
 Valet claim ticket  Back pointers (or ranks) are updated during swaps
 Reservation number header nodes/positions trailer
 Main idea:
 Since entries are created and returned from the
data structure itself, it can return location-aware 2 c 4 c 5 c 8 c
entries, thereby making future updates easier
entries
© 2010 Goodrich, Tamassia Adaptable Priority Queues 7 © 2010 Goodrich, Tamassia Adaptable Priority Queues 8
Heap Implementation Performance
 A location-aware heap 2 d
 Improved times thanks to location-aware
entry is an object
storing 4 a 6 b entries are highlighted in red
 key Method Unsorted List Sorted List Heap
 value size, empty O(1) O(1) O(1)
 position of the entry in insert O(1) O(n) O(log n)
the underlying heap
 In turn, each heap
min O(n) O(1) O(1)
position stores an removeMin O(n) O(1) O(log n)
entry remove O(1) O(1) O(log n)
 Back pointers are replace O(1) O(n) O(log n)
8 g 5 e 9 c
updated during entry
swaps
© 2010 Goodrich, Tamassia Adaptable Priority Queues 9 © 2010 Goodrich, Tamassia Adaptable Priority Queues 10

Das könnte Ihnen auch gefallen