Download presentation
Presentation is loading. Please wait.
Published byประวิตร เคนเนะดิ Modified over 5 years ago
1
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Removing from Heap Heap Sort Heap Construction Top down Heap Construction Bottom up Heap Construction Adaptable PQ 7/28/2019 CS210-Summer 2005, Lecture 14
2
Heaps An efficient realization of a priority queue uses a data structure called a heap. Heap allows us to perform both insertions and removals in logarithmic time. Heap stores entries in a binary tree rather than in a list. 7/28/2019 CS210-Summer 2005, Lecture 14
3
Heaps and Priority Queues
We can use a heap to implement a priority queue We store a (key, element) item at each node. We keep track of the position of the last node (2, Sue) (5, Pat) (6, Mark) (9, Jeff) (7, Anna) 7/28/2019 CS210-Summer 2005, Lecture 14
4
Insertion into a Heap (2, C) Method insert of the priority queue ADT corresponds to the insertion of an entry (k,x) to the heap The insertion algorithm consists of three steps Add a new node z (the new last node) Store e at z Restore the heap-order property (discussed next) (5, A) (6, Z) z (15,K) (9, F) insertion node (2, C) (5, A) (6, Z) z (15,K) (9, F) (1, D) 7/28/2019 CS210-Summer 2005, Lecture 14
5
Upheap After the insertion of a new entry e, the heap-order property may be violated Algorithm upheap restores the heap-order property by swapping e along an upward path from the insertion node Upheap terminates when the entry e reaches the root or a node whose parent has a key smaller than or equal to key k of e. Since a heap has height O(log n), upheap runs in O(log n) time (2, C) (1, D) (5, A) (1,D) (5, A) (2, C) z z (15,K) (9, F) (6, Z) (9, F) (6, Z) (15,K) 7/28/2019 CS210-Summer 2005, Lecture 14
6
Removal from a Heap Method removeMin of the priority queue ADT corresponds to the removal of the root entry from the heap The removal algorithm consists of three steps Replace the root entry with the entry of the last node w Remove w Restore the heap-order property (discussed next) (2, C) (5, A) (6, Z) w (15,K) (9, F) last node (9, F) (5, A) (6, Z) w (15,K) new last node 7/28/2019 CS210-Summer 2005, Lecture 14
7
Downheap After replacing the root entry with the entry e of the last node, the heap-order property may be violated Algorithm downheap restores the heap-order property by swapping entry e along a downward path from the root Upheap terminates when key k of entry e reaches a leaf or a node whose children have keys greater than or equal to k Since a heap has height O(log n), downheap runs in O(log n) time (9, F) (5, A) (5, A) (6, Z) (9, F) (6, Z) w w (15,K) (15,K) 7/28/2019 CS210-Summer 2005, Lecture 14
8
Heap-Sort Consider a priority queue with n items implemented by means of a heap methods insert and removeMin take O(log n) time methods size, isEmpty, and min take time O(1) time Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time The resulting algorithm is called heap-sort Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort 7/28/2019 CS210-Summer 2005, Lecture 14
9
Heap Construction We can construct a heap storing n entries in O(nlogn) time, by means of n successive insert operations. However if all the n key-value pairs to be stored in the heap are given in advance, there is an alternative bottom up construction method that runs in O(n) time. 7/28/2019 CS210-Summer 2005, Lecture 14
10
Bottom up Heap Construction
16 15 4 12 6 7 23 20 25 5 11 27 9 8 14 16 4 15 12 6 7 23 20 25 5 11 27 9 8 14 7/28/2019 CS210-Summer 2005, Lecture 14
11
Bottom up Heap Construction
16 4 15 12 6 7 8 20 25 5 11 27 9 23 14 16 15 4 12 6 7 8 20 25 5 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14
12
Bottom up Heap Construction
16 4 15 12 6 7 8 20 25 5 11 27 9 23 14 16 15 4 12 6 7 8 20 25 5 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14
13
Bottom up Heap Construction
16 4 15 12 5 7 8 20 25 6 11 27 9 23 14 16 15 4 12 5 7 8 20 25 6 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14
14
Bottom up Heap Construction
16 4 15 12 5 7 8 20 25 6 11 27 9 23 14 16 15 4 12 5 7 8 20 25 6 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14
15
Bottom up Heap Construction
16 4 15 12 5 7 8 20 25 6 11 27 9 23 14 16 15 4 12 5 7 8 20 25 6 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14
16
Bottom up Heap Construction
16 4 5 12 6 7 8 20 25 15 11 27 9 23 14 16 5 4 12 6 7 8 20 25 15 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14
17
Bottom up Heap Construction
4 7 5 12 6 9 8 20 25 15 11 27 16 23 14 16 5 4 12 6 7 8 20 25 15 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14
18
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? 7/28/2019 CS210-Summer 2005, Lecture 14
19
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. 7/28/2019 CS210-Summer 2005, Lecture 14
20
Example Operation Output P insert(5,A) e1 (5,A)
insert(3,B) e2 (3,B),(5,A) insert(7,C) e3 (3,B),(5,A),(7,C) min() e2 (3,B),(5,A),(7,C) key(e2) 3 (3,B),(5,A),(7,C) remove(e1) e1 (3,B),(7,C) replaceKey(e2,9) 3 (7,C),(9,B) replaceValue(e3,D) C (7,D),(9,B) remove(e2) e2 (7,D) 7/28/2019 CS210-Summer 2005, Lecture 14
21
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. 7/28/2019 CS210-Summer 2005, Lecture 14
22
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 7/28/2019 CS210-Summer 2005, Lecture 14
23
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 trailer header nodes/positions 2 c 4 c 5 c 8 c entries 7/28/2019 CS210-Summer 2005, Lecture 14
24
Heap Implementation A location-aware heap entry is an object storing
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 2 d 4 a 6 b 8 g 5 e 9 c 7/28/2019 CS210-Summer 2005, Lecture 14
25
Performance Using location-aware entries we can achieve the following running times: Method Unsorted List Sorted List Heap size, isEmpty O(1) O(1) O(1) insert O(1) O(n) O(log n) min O(n) O(1) O(1) removeMin O(n) O(1) O(log n) remove O(1) O(1) O(log n) replaceKey O(1) O(n) O(log n) replaceValue O(1) O(1) O(1) 7/28/2019 CS210-Summer 2005, Lecture 14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.