Professional Documents
Culture Documents
1 2
Max-heap
We store the keys in the internal nodes only
key(parent) ≥ key(child)
5
40
15 9
35 26
16
15 19 17 20
1 13 14 12 11 8
1
Height of a Heap Notice that ….
Downheap
Removal From a Heap
2
Downheap Continues Downheap Continues
9 10
3
Heap Insertion
Upheap
• Swap parent-child keys out of order
13 14
4
Heap Construction Bottom-up Heap
Construction
We could insert the Items one at the time with
a sequence of Heap Insertions:
n • We can construct a heap
Σ log k = O(n log n) storing n given keys using
k=1
a bottom-up construction
17 18
1 9 6 3
2 2 2
4 5 4 5 4 10
7 6 10 8 9 6 10 8 9 6 5 8
HEAP 1 9 3 1 7 3 1 7 3
HEAP
7↔9 5 ↔ 10
HEAP
19 20
5
Example 1
2 Example 1
2
4 10
! !
9 10
9 6 5 8
10
7 6 5 8
1 7 3 2
Finally: 2 ↔ 10
9 1 4 3
9 8
5
4↔9
4
7
This is not a heap ! 4↔7 10
1 7
1 4
2↔8
2
5 8
10
2
! ! 9 8
9 10
7 6 5 2
7 6 5 8
21 1 4 3
22
1 4 3
25 5 11 27
16 15 4 12 6 7 23 20 16 15 4 12 6 9 23 20
25 5 11 27 15 4 6 23
16 15 4 12 6 7 23 20 16 25 5 12 11 9 27 20
23 24
6
Example 2 Example 2
20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]
20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]
10
7 8 4 6
15 4 6 23 15 5 8 23
16 25 5 12 11 9 27 20 16 25 7 12 11 9 27 20
4 6 5 6
15 5 8 23 15 7 8 23
16 25 7 12 11 9 27 20 16 25 10 12 11 9 27 20
25 26
3 swaps 2
level 0
at most
2 swaps level 1 h
At level i the number of swaps is
4 5
1 swap 7 3 10 8
level 2
≤ h–i for each node
0 swaps level 3
1 9 6
27 28
7
h
Consider Σ j 2-j :
Σ j 2-j = 1/2 + 2 1/4 + 3 1/8 + 4 1/16 + … So, the number of swaps is ≤ O(n)
= 1/2 + 1/4 + 1/8 + 1/16 + … <= 1
+ 1/4 + 1/8 + 1/16 + … <= 1/2
+ + 1/8 + 1/16 + … <= 1/4
Σ j 2-j <= 2
29 30
So 2h Σ j 2-j <= 2. 2h = 2 n O(n)
Implementing a Heap
with an Array Example
A heap can be nicely represented by an array list 1
H
(array-based), 1
2
where the node at rank i has 2 3 D I
3
4 5 6 4
7 B
5
E
6
L
7
O
and i
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 H D I B E L O A C F G H N
8
Reminder ….. Implementation of a Priority
Left child of T[i] T[2i] if 2i ≤ n Queue with a Heap
Right child of
T[2i+1] if 2i + 1 ≤ n
T[i]
Parent of T[i] T[i div 2] if i>1
The Root T[1] if T≠0
Leaf? T[i] TRUE if 2i > n
1 n = 11
2 3
I
4 5 6 7
8 9 10 11
33 34
9
Application: Sorting
Heap Sort When there are i nodes left in the PQ: log i
n
= O(n log n)
remove root O(1)
n re-arrange O(log n)
times
remove root O(1)
L M
37 38
L
HeapSort in Place
39 40
10
Another type of Heap … removeMin()
descendants 65 80
25 37 8 15
5 57 36 45 59 20 14 32 18
65 80 28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17
25 37 8 15
57 36 45 59 20 14 32 18
28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17
41 42
removeMin() removeMin()
smallest 17 8
65 80 65 80
25 37 8 15 25 37 17 15
57 36 45 59 20 14 32 18 57 36 45 59 20 14 32 18
28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 28 30 34 27 39 38 45 50 15 12 13 10 30 31 16
smallest
43 44
11
5
removeMin() Insert(x)
65 80
25 37 8 15
57 36 45 59 20 14 32 18
28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17
8
x
65 80 If in Max-level:
25 37 10 15 If parent is greater, swap and BubbleUpMin
If parent is smaller, BubbleUpMax
57 36 45 59 20 14 32 18
BubbleUpMin
28 30 34 27 39 38 45 50 15 12 13 17 30 31 16
If grandparent is greater, swap
BubbleUpMin(grandparent)
BubbleUpMax
If grandparent is smaller, swap
Check parent BubbleUpMax(grandparent)
45 46
5 5
Insert(13) Insert(13)
65 80 65 80
25 37 8 15 25 37 8 15
57 36 45 59 20 14 32 18 57 36 45 59 20 14 32 18
28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17 13 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17
13 28
If in Max-level:
If parent is greater, swap and BubbleUpMin
If parent is smaller, BubbleUpMax
BubbleUpMin
If grandparent is greater, swap
BubbleUpMin(grandparent)
47 48
12
5 5
Insert(13) Insert(61)
65 80 65 80
13 37 8 15 25 37 8 15
57 36 45 59 20 14 32 18 57 36 45 59 20 14 32 18
25 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17 28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17
28 61
If in Max-level:
If parent is greater, swap and BubbleUpMin
If parent is smaller, BubbleUpMax
BubbleUpMax
If grandparent is smaller, swap
BubbleUpMax(grandparent)
49 50
5
Insert(61)
65 80
25 37 8 15
61 36 45 59 20 14 32 18
28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17
What is the cost of
57
If in Max-level: removeMin() AND removeMax()
If parent is greater, swap and BubbleUpMin
If parent is smaller, BubbleUpMax With this heap ?
BubbleUpMax
If grandparent is smaller, swap
BubbleUpMax(grandparent)
51 52
13