You are on page 1of 3

Outline

Heap data structure


Heapsort, Priority Queue Extract min
Insert
Priority queue
Heapsort

Recitation 3: Heapsort, Priority 1 Recitation 3: Heapsort, Priority 2


Queue Queue

Binary Min-heap Array Representation


1
1
Nearly complete binary tree that satisfies 2 3
the heap property 2 5
5 6 7
4
tree completely filled on all levels except 3 7 6 8
8 9 10
lowest level, which is filled from the left 9 8 11
Heap property: 1 2 5 3 7 6 8 9 8 11

A[parent(x)] A[x]
Left(i) = 2i
Right(i) = 2i+1
Parent(i) = i/2
Recitation 3: Heapsort, Priority 3 Recitation 3: Heapsort, Priority 4
Queue Queue

Extract Min Remove the max item


EXTRACT - MIN(A) 12

1 min A[1]
2 A[1] A[heap - size[ A]]
8 9
3 heap - size[ A] heap - size[ A] 1
4 MIN - HEAPIFY( A,1) maintain heap property
5 return min 4 3 5 7

Extract the min element for priority queue


2 1
Simple way to sort - repeatedly extract-min.

Recitation 3: Heapsort, Priority 5 Recitation 3: Heapsort, Priority 6


Queue Queue

1
Re-establish heap property Re-establish heap property
1 9

8 9 8 7

4 3 5 7 4 3 5 1

2 2

bubble down
Recitation 3: Heapsort, Priority 7 Recitation 3: Heapsort, Priority 8
Queue Queue

Min-Heapify Insert
MIN - HEAPIFY( A, i )
1 if 2i size[ A] and A[2i ] < A[i ] left child INSERT(key, A)
2 then smallest 2i 1 heap size( A) heap size( A) + 1
3 else smallest i 2 i heap size( A)
4 if 2i + 1 size[ A] and A[2i + 1] < A[ smallest ] right child 3 while i > 1 and A[ i / 2] > key
5 then smallest 2i + 1 4 do A[i ] A[ i / 2]
6 if smallest i 5 i i / 2
7 then swap A[i ] A[ smallest ]
6 A[i ] key
8 MIN - HEAPIFY( A, smallest )
Start at new leaf, move parent down until we meet
What is the running time of MIN-HEAPIFY? a parent smaller than key.
Running time: O(lg n)
Recitation 3: Heapsort, Priority 9 Recitation 3: Heapsort, Priority 10
Queue Queue

Insert an item Re-establish heap property


9 0 12 9
1 8
2 9
8 7 15 7
3 4
4 3
4 3 5 1
5 5 8 3 5 1
6 7
7 2
2 15 8 1 2 4
9 15

Recitation 3: Heapsort, Priority 11 Recitation 3: Heapsort, Priority 12


Queue Queue

2
Re-establish heap property Priority Queue
15
Data structure for maintaining a set of elements,
each with an associated value called a key.
9 7 Example: scheduling jobs on a shared computer
When job finished, highest priority job is selected to be
executed.
8 3 5 1
One of the most popular application of heap
Insert new element in O(lg n)
2 4
Extract element with smallest (largest) key in time
O(lg n).
bubble up
Recitation 3: Heapsort, Priority 13 Recitation 3: Heapsort, Priority 14
Queue Queue

Heapsort Build-Min-Heap
HEAPSORT ( A)
1 BUILD MAX HEAP( A) BUILD MIN HEAP( A)
1 for i heap size( A) / 2 downto 1
2 for i heap size( A) downto 2
3 do swap A[1] A[i ] 2 do MIN HEAPIFY( A, i )

4 heap size( A) heap size( A) 1 Correctness:


5 MAX HEAPIFY( A,1) Invariant: All trees rooted at m>i are heaps
Running time:
Use min-heap to get decreasing seq Height of tree is lg n
Sorts in place Number of nodes at height h is at most n/2h+1
MIN-HEAPIFY runs in time O(h) when the node is of
Running time = O(n log n) + Build-max- height h.
heap time.
Recitation 3: Heapsort, Priority 15 Recitation 3: Heapsort, Priority 16
Queue Queue

Running time:
lg n
n lg n h Summary
2h +1

O(h) = O n h = O(n) Heap data structure
h=0 h =0 2 nearly complete binary tree
because A[parent(x)] A[x]

1
x
i =0
i
=
1 x
| x |< 1 Extract min, insert
O(lg n)

1
ix i 1 =
i =0 (1 x) 2
differentiation Heapsort
O(n lg n)
in place sort
x

i =0
ix = i

(1 x) 2
mult both sides by x
Priority queue
=2 when x = 1 / 2 popular application of heap - O(lg n) insert and extract
min
Recitation 3: Heapsort, Priority 17 Recitation 3: Heapsort, Priority 18
Queue Queue

You might also like