Heap
Heap
Chapter 2
Syllabus Topics
• Heap Operations
• Implementation of Priority Queue and Heap
Neepa Shah
Neepa Shah 1
Chap 2: Heap and Priority Q 01-10-2020
• Additional methods
▫ min()
▫ int size()
▫ int isEmpty()
Neepa Shah
• In Operating Systems
▫ Scheduling jobs
• In Simulators
▫ Scheduling the next event (smallest event time)
Neepa Shah
Neepa Shah 2
Chap 2: Heap and Priority Q 01-10-2020
Example
Operation Output Priority Queue
insert(5, A) [= (5, A)] {(5, A)}
Insert(9, C) [= (9, C)] {(5, A), (9, C)}
Insert(3, B) [= (3, B)] {(3, B), (5, A), (9, C)}
Insert(7, D) [= (7, D)] {(3, B), (5, A), (7, D), (9, C)}
min() e3 {(3, B), (5, A), (7, D), (9, C)}
removeMin() e3 {(5, A), (7, D), (9, C)}
size() 3 {(5, A), (7, D), (9, C)}
removeMin() e1 {(7, D), (9, C)}
removeMin() e4 {(9, C)}
removeMin() e2 {}
removeMin() “error” {}
isEmpty() true {}
Neepa Shah
Neepa Shah
Neepa Shah 3
Chap 2: Heap and Priority Q 01-10-2020
Neepa Shah
• Performance: • Performance:
▫ insert takes O(1) ▫ insert takes O(n) time
time ▫ removeMin and min
▫ removeMin and take O(1) time
min take O(n)
time
Neepa Shah
Neepa Shah 4
Chap 2: Heap and Priority Q 01-10-2020
Comparison
Neepa Shah
10
Neepa Shah 5
Chap 2: Heap and Priority Q 01-10-2020
11
Insertion-Sort Contd.
• Insertion-sort is the variation of PQ-sort where
the priority queue is implemented with a sorted
sequence
• Running time of Insertion-sort:
1. Inserting the elements into the priority queue with n
insert operations takes time proportional to
1 + 2 + …+ n
2. Removing the elements in sorted order from the
priority queue with a series of n removeMin operations
takes O(n) time
• Insertion-sort runs in O(n2) time
Neepa Shah
12
Insertion-Sort Example
Sequence S Priority queue P
Input: (7,4,8,2,5,3,9) ()
Phase 1
(a) (4,8,2,5,3,9) (7)
(b) (8,2,5,3,9) (4,7)
(c) (2,5,3,9) (4,7,8)
(d) (5,3,9) (2,4,7,8)
(e) (3,9) (2,4,5,7,8)
(f) (9) (2,3,4,5,7,8)
(g) () (2,3,4,5,7,8,9)
Phase 2
(a) (2) (3,4,5,7,8,9)
(b) (2,3) (4,5,7,8,9)
.. .. ..
. . .
(g) (2,3,4,5,7,8,9) ()
Neepa Shah
Neepa Shah 6
Chap 2: Heap and Priority Q 01-10-2020
13
Neepa Shah
32 91 12 55 74 73 18
Neepa Shah 7
Chap 2: Heap and Priority Q 01-10-2020
SWAP !
32 91 12 55 74 73 18
16
Selection-Sort
• Selection-sort is the variation of PQ-sort where the
priority queue is implemented with an unsorted
sequence
• Running time of Selection-sort:
1. Inserting the elements into the priority queue with n insert
operations takes O(n) time
2. Removing the elements in sorted order from the priority
queue with n removeMin operations takes time proportional
to
n + n-1 + … + 2 + 1
• Selection-sort runs in O(n2) time
Neepa Shah
Neepa Shah 8
Chap 2: Heap and Priority Q 01-10-2020
17
Selection-Sort Example
Sequence S Priority Queue P
Input: (7,4,8,2,5,3,9) ()
Phase 1
(a) (4,8,2,5,3,9) (7)
(b) (8,2,5,3,9) (7,4)
.. .. ..
. . .
(g) () (7,4,8,2,5,3,9)
Phase 2
(a) (2) (7,4,8,5,3,9)
(b) (2,3) (7,4,8,5,9)
(c) (2,3,4) (7,8,5,9)
(d) (2,3,4,5) (7,8,9)
(e) (2,3,4,5,7) (8,9)
(f) (2,3,4,5,7,8) (9)
(g) (2,3,4,5,7,8,9) ()
Neepa Shah
18
Neepa Shah
Neepa Shah 9
Chap 2: Heap and Priority Q 01-10-2020
Heaps
5 6
9 7
20
Neepa Shah
Neepa Shah 10
Chap 2: Heap and Priority Q 01-10-2020
21
Neepa Shah
21
22
(2, ABC)
Neepa Shah
Neepa Shah 11
Chap 2: Heap and Priority Q 01-10-2020
23
What is a “heap”?
• Definition of heap:
A balanced, left-justified binary tree in
which no node has a value greater than /
lesser than the value in its parent
Neepa Shah
24
n-2
n-1
n
Balanced Balanced Not balanced
Neepa Shah
Neepa Shah 12
Chap 2: Heap and Priority Q 01-10-2020
25
Neepa Shah
26
Neepa Shah
12 12 12
8 3 8 12 8 14
Blue node has heap Blue node has heap Blue node does not have
property property heap property
Neepa Shah 13
Chap 2: Heap and Priority Q 01-10-2020
27
Neepa Shah
3 1 12
8 13 4 12 8 14
Blue node has heap Blue node has heap Blue node does not have
property property heap property
28
Neepa Shah
Examples
Neepa Shah 14
Chap 2: Heap and Priority Q 01-10-2020
29
Height of a Heap
• Theorem: A heap storing n keys has height O(log n)
Proof: (we apply the complete binary tree property)
▫ Let h be the height of a heap storing n keys
▫ Since there are 2i keys at depth i = 0, … , h - 1 and at least one key at
depth h, we have n 1 + 2 + 4 + … + 2h-1 + 1
▫ Thus, n 2h , i.e., h log n
depth keys
0 1
1 2
h-1 2h-1
h 1
Neepa Shah
30
Neepa Shah
12 14
8 14 8 12
Blue node does not have Blue node has heap
heap property property
Neepa Shah 15
Chap 2: Heap and Priority Q 01-10-2020
31
Constructing a heap I
• A tree consisting of a single node is
automatically a heap
• We construct a heap by adding nodes one at a
time:
▫ Add the node just to the right of the rightmost
node in the deepest level
▫ If the deepest level is full, start a new level
• Examples:
Add a new Add a new
node here node here
Neepa Shah
32
Neepa Shah
Neepa Shah 16
Chap 2: Heap and Priority Q 01-10-2020
33
Neepa Shah
10 8 8 5
1 2 3
10 10 12
8 5 12 5 10 5
12 8 8
4
34
Heap Operations
• insert (k, x)
• deleteMin()
Neepa Shah
Neepa Shah 17
Chap 2: Heap and Priority Q 01-10-2020
35
14
14
14
36
Neepa Shah 18
Chap 2: Heap and Priority Q 01-10-2020
37
Neepa Shah
22 17
19 22 14 15
18 14 21 3 9 11
38
22 17
19 22 14 15
18 14 21 3 9 11
Neepa Shah 19
Chap 2: Heap and Priority Q 01-10-2020
Neepa Shah 39
22 17
19 21 14 15
18 14 11 3 9
Since a heap has height O(log n), downheap runs in O(log n) time
22 17
19 22 14 15
18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
• Note:
▫ The left child of index i is at index 2*i+1
▫ The right child of index i is at index 2*i+2
Neepa Shah
40
Neepa Shah 20
Chap 2: Heap and Priority Q 01-10-2020
Neepa Shah 41
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
22 22 17 19 21 14 15 18 14 11 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
9 22 17 19 22 14 15 18 14 21 3 22 25
Neepa Shah 21
Chap 2: Heap and Priority Q 01-10-2020
43
Heap Operations
• insert (k, x)
• deleteMin()
Neepa Shah
44
Neepa Shah
13 14 16 19 21 19 68 65 26 32 31
Neepa Shah 22
Chap 2: Heap and Priority Q 01-10-2020
45
Neepa Shah
31
31
46
Neepa Shah
Neepa Shah 23
Chap 2: Heap and Priority Q 01-10-2020
47
heapify(6)
heapify(5) heapify(4)
48
Neepa Shah
heapify(3) heapify(2)
heapify(1) heapify(0)
Since a heap has height O(log n), heapify runs in O(log n) time
Neepa Shah 24
Chap 2: Heap and Priority Q 01-10-2020
Sorting
• What do heaps have to do with sorting an array?
▫ Because the binary tree is balanced and left justified,
it can be represented as an array
▫ All our operations on binary trees can be represented
as operations on arrays
▫ To sort:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
Neepa Shah
49
Analysis I
• Here’s how the algorithm starts:
heapify the array;
Neepa Shah
50
Neepa Shah 25
Chap 2: Heap and Priority Q 01-10-2020
Analysis II
• Rest of the algorithm:
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
• We do the while loop n times (actually, n-1
times), because we remove one of the n nodes
each time
• Removing and replacing the root takes O(1)
time
• Therefore, the total time is n times however
long it takes the reheap method
Neepa Shah
51
Analysis III
• To reheap the root node, we have to follow one
path from the root to a leaf node (and we might
stop before we reach a leaf)
• The binary tree is perfectly balanced
• Therefore, this path is O(log n) long
▫ And we only do O(1) operations at each node
▫ Therefore, reheaping takes O(log n) times
• Since we reheap inside a while loop that we do n
times, the total time for the while loop is
n*O(log n), or O(n log n)
Neepa Shah
52
Neepa Shah 26
Chap 2: Heap and Priority Q 01-10-2020
Analysis IV
• Here’s the algorithm again:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
• Heapifying takes O(n log n) time
• The while loop takes O(n log n) time
• The total time is therefore O(n log n) + O(n log
n)
• This is the same as O(n log n) time
Neepa Shah
53
54
Heap-Sort: Conclusion
• A priority queue with n items implemented by means
of a heap
▫ The space used is O(n)
▫ Methods insert and removeMin take O(log n) time
▫ Methods size, isEmpty, and min take time O(1) time
Neepa Shah
Neepa Shah 27
Chap 2: Heap and Priority Q 01-10-2020
55
Neepa Shah
56
Example
Neepa Shah
Neepa Shah 28
Chap 2: Heap and Priority Q 01-10-2020
57
Algorithm Insert
Neepa Shah
58
Delete Maximum
Neepa Shah
Neepa Shah 29
Chap 2: Heap and Priority Q 01-10-2020
59
Algorithm Adjust
Neepa Shah
60
Heap Sort
Neepa Shah
Neepa Shah 30
Chap 2: Heap and Priority Q 01-10-2020
61
• Build-Heap / Heapify
Neepa Shah
62
Neepa Shah
Neepa Shah 31
Chap 2: Heap and Priority Q 01-10-2020
63
Neepa Shah
64
Neepa Shah
Neepa Shah 32
Chap 2: Heap and Priority Q 01-10-2020
65
Neepa Shah
66
Neepa Shah
Neepa Shah 33