Professional Documents
Culture Documents
Heaps
Heaps
2
5
Heaps
insert(k, x)
inserts an entry with key
k and value x
removeMin()
removes and returns the
entry with smallest key
Heaps
Additional methods
min()
returns, but does not
remove, an entry with
smallest key
size(), isEmpty()
Applications:
Standby flyers
Auctions
Stock market
Can we do better?
Algorithm PQ-Sort(S, C)
Input sequence S, comparator C
for the elements of S
Output sequence S sorted in
increasing order according to C
P priority queue with
comparator C
while S.isEmpty ()
e S.remove (S. first ())
P.insertItem(e, e)
while P.isEmpty()
e P.removeMin().getKey()
S.addLast(e)
Heaps
Heaps
Heaps
6
7
last node
6
Heap-order Property
Heaps
(4, C)
(5, A)
(15, K)
16, X)
(6, Z)
(9, F)
(25, J)
(14, E)
(7, Q)
(12, H)
Heaps
(11, S)
(20, B)
(8, W)
last
node
10
Height of a Heap
depth keys
0
h1
2h1
Heaps
11
(2, Sue)
(5, Pat)
(9, Jeff)
(6, Mark)
(7, Anna)
Heaps
12
2
5
9
Heaps
insertion node
2
5
9
6
7
13
Upheap
5
9
1
7
5
9
Heaps
2
7
6
14
Upheap
Since a heap has height O(log n), upheap runs in O(log n) time.
1
5
9
Heaps
15
Upheap
Example:
(4, C)
(5, A)
(6, Z)
(15, K)
16, X)
(9, F)
(25, J)
(14, E)
(7, Q)
(12, H)
(11, S)
(20, B)
(8, W)
(4, C)
(5, A)
(15, K)
16, X)
(6, Z)
(9, F)
(25, J)
(14, E)
(7, Q)
(12, H)
Heaps
(11, S)
(20, B)
(8, W)
(2, T)
16
Upheap
Example (Continues ):
(4, C)
(5, A)
(15, K)
16, X)
(6, Z)
(9, F)
(25, J)
(14, E)
(7, Q)
(12, H)
(11, S)
(2, T)
(8, W)
(20, B)
(4, C)
(5, A)
(15, K)
16, X)
(2, T)
(9, F)
(25, J)
(14, E)
(7, Q)
(12, H)
Heaps
(11, S)
(6, Z)
(8, W)
(20, B)
17
Upheap
Example (Continues ):
(2, T)
(5, A)
(15, K)
16, X)
(4, C)
(9, F)
(25, J)
(14, E)
(7, Q)
(12, H)
Heaps
(11, S)
(6, Z)
(8, W)
(20, B)
18
2
5
9
6
7
w
last node
5
6
Return the root entry
w
Replace the root key (entry in
9
fact) with the key of the last
node w
Remove w
new last node
Restore the heap-order
property (discussed next)
Heaps
19
Downheap
After replacing the root key with the key k of the last
node, the heap-order property may be violated.
Algorithm downheap restores the heap-order property by
swapping key k along a downward path from the root.
If T has no right child, then the swapping (if needed) starts at the left
7
5
5
6
9
Heaps
20
Downheap
Since a heap has height O(log n), downheap runs in O(log n) time.
7
5
9
5
6
9
Heaps
21
Downheap
Removal Example:
(4, C)
(5, A)
(6, Z)
(15, K)
16, X)
(9, F)
(25, J)
(14, E)
(12, H)
(7, Q)
(11, S)
(20, B)
(13, W)
(4, C)
(13, W)
(5, A)
(15, K)
16, X)
(6, Z)
(9, F)
(25, J)
(14, E)
(12, H)
(7, Q)
(20, B)
(11, S)
Heaps
22
Downheap
(13, W)
(5, A)
(6, Z)
(15, K)
16, X)
(9, F)
(25, J)
(14, E)
(7, Q)
(12, H)
(20, B)
(11, S)
(5, A)
(13, W)
(15, K)
16, X)
(6, Z)
(9, F)
(25, J)
(14, E)
(12, H)
(7, Q)
(20, B)
(11, S)
Heaps
23
Downheap
(5, A)
(9, F)
(15, K)
16, X)
(6, Z)
(13, W)
(25, J)
(14, E)
(12, H)
(7, Q)
(20, B)
(11, S)
(5, A)
(9, F)
(15, K)
16, X)
(6, Z)
(12, H)
(25, J)
(14, E)
(13, W)
(7, Q)
(20, B)
(11, S)
Heaps
24
Heaps
25
Heap Performance
Operation
size(), isEmpty()
Complexity
O(1)
min()
O(1)
insert()
O(log n)
removeMin()
O(log n)
Heaps
26
Heap-Sort
Algorithm PQ-Sort(S, C)
Input sequence S, comparator C for the elements of S
Output sequence S sorted in increasing order according to C
P priority queue with comparator C
while S.isEmpty ()
e S.removeFirst ()
P.insert (e, )
while P.isEmpty()
e P.removeMin().getKey()
S.addLast(e)
Heaps
27
Heap-Sort
this is actually the ith removal (that is, some elements may have already been removed)
where 1 <= i <= n.
28
Heap-Sort
Heaps
29
Heap-Sort
Consider a priority
queue with n items
implemented by
means of a heap
Heaps
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
30
Array-based Heap
Implementation
2
5
Heaps
31
3
8
2
5
7
3
8
2
5
2
3
8
4
5
7
Heaps
6
32
Bottom-up Heap
Construction
33
Bottom-up Heap
Construction
Consequently,
n = 2h+1 1
h = log(n + 1) - 1
Heaps
34
Bottom-up Heap
Construction
2i 1
2i 1
2i11
Heaps
35
Bottom-up Heap
Construction
2i 1
2i 1
2i11
Heaps
36
16
15
12
23
20
Step 1
25
16
5
15
11
12
27
7
23
20
Step 2
Heaps
37
Example (contd.)
25
16
5
15
11
12
27
9
23
20
15
16
4
25
6
12
11
20
9
23
27
Step 2 is concluded
Heaps
38
Example (contd.)
7
15
16
4
25
6
12
11
20
9
23
27
Step 3
15
16
5
25
8
12
11
20
9
23
27
39
Example (end)
10
4
15
16
5
25
8
12
11
20
9
23
27
Step 4
4
5
15
16
7
25
10
8
12
11
20
9
23
27
40
Analysis
Heaps
41
Analysis
Heaps
42
Analysis
Heaps
43