Professional Documents
Culture Documents
Heaps
Properties
Deletion, Insertion, Construction
Implementation of the Heap
Implementation of Priority Queue
using a Heap
An application: HeapSort
Heaps (Min-heap)
Complete binary tree that stores a collection of keys
(or key-element pairs) at its internal nodes and that satisfies
the additional property:
REMEMBER:
key(parent) key(child)
Max-heap
key(parent) key(child)
40
35
26
15
1
19
13
14
17
12
11
20
8
16
Height of a Heap
Theorem: A heap storing n keys has height O(log n)
Proof:
depth keys
0
h-1
2h-1
at least 1
5
Notice that .
We could use a heap to implement a priority queue
We store a (key, element) item at each node
removeMin():
Remove the root
Re-arrange the heap!
(2, Sue)
(5, Pat)
(9, Jeff)
(6, Mark)
(7, Anna)
Downheap
Downheap Continues
Downheap Continues
10
End of Downheap
Heap Insertion
The key to insert is 6
12
Heap Insertion
13
Upheap
Swap parent-child keys out of order
14
Upheap Continues
15
End of Upheap
removeMin()
17
Insert(key, value)
18
Heap Construction
We could insert the items one at the time with
a sequence of Heap Insertions:
n
log k
= O(n log n)
k=1
19
Bottom-up Heap
Construction
We can construct a heap
storing n given keys using a
bottom-up construction
20
Construction of a Heap
Idea: Recursively re-arrange each sub-tree
in the heap starting with the leaves
21
Example 1 (Max-Heap)
--- keys already in the tree ---
36
2
10
10
79
10
5 10
22
Example 1
10
3
49
47
23
Example 1
Finally:
10
2 8
24
Example 2 Min-Heap
{14,9,8,25,5,11,27,16,15,4,12,6,7,23,20}
16
15
25
16
12
5
15
23
11
12
20
27
7
23
20
25
Example 2 Min-Heap
{14,9,8,25,5,11,27,16,15,4,12,6,7,23,20}
15
16
4
25
6
12
11
20
7
9
4
25
27
15
16
23
6
12
11
20
7
23
27
26
Example 2 Min-Heap
{14,9,8,25,5,11,27,16,15,4,12,6,7,23,20}
4
15
16
5
25
7
12
11
20
8
23
27
14
4
15
16
5
25
7
12
11
20
8
23
27
27
Example 2 Min-Heap
{14,9,8,25,5,11,27,16,15,4,12,6,7,23,20}
4
5
15
16
9
25
14
7
12
11
20
8
23
27
28
-j
1
29
Number of swaps
3 swaps
at most
2 swaps
1 swap
0 swaps
level 0
5
h=4
10
level 1
level 2
level 3
h is the max level
level i
--------
h - i
swaps
30
h i
(h
i)2i
i=0
31
(h i)2i
i=0
j=0
j=0
= j 2h-j = 2h j 2-j
Consider j 2-j :
+ 3 1/8 + 4 1/16 +
+ 1/8 + 1/16 + <= 1
+ 1/8 + 1/16 + <= 1/2
+ 1/8 + 1/16 + <= 1/4
<= 2
j 2-j
So 2h j 2-j <= 2. 2h = 2 n
O(n)
32
2h
j/2
j=1
2h+1
Where h is O(log n)
O(n)
33
Implementing a Heap
with an Array
4
5
34
10
11
12
13
35
T[2i]
if
2i n
Right child of
T[i]
T[2i+1]
if
2i + 1 n
Parent of T[i]
T[i div 2]
if
i > 1
The Root
T[1]
if
n>0
Leaf? T[i]
TRUE
if
2i > n
n = 11
36
T[2i+1]
if
2i +1 n-1
Right child of
T[i]
T[2i+2]
if
2i + 2 n-1
Parent of T[i]
T[(i-1) div 2]
if
i > 0
The Root
T[0]
if
N>0
Leaf? T[i]
TRUE
if
n = 11
1
3
2
4
5
9
10
37
38
(upheap)
O(log n)
O(log 1)
O(log n)
39
Application: Sorting
Heap Sort
Construct initial heap
n
times
O(n)
remove root
O(1)
re-arrange
O(log n)
remove root
O(1)
re-arrange
O(log (n-1))
41
TOT =
log i
i=1
O(n log n)
42
HeapSort in Place
Instead of using a secondary data structure P to sort a sequence S,
We can execute heapsort in place by dividing S in two parts, one
representing the heap, and the other representing the sequence.
The algorithm is executed in two phases:
Phase 1: We build a max-heap so to occupy the whole structure.
Phase 2: We start with the part sequence empty and we
grow it by removing at each step i (i=1..n) the max value from the heap
and by adding it to the part sequence, always maintaining
the heap properties for the part heap.
43
RemoveMax()
22
19
20
15
2
13
10
14
11
7
19
20
15
2
13
10
14
4
11
8
22
7
19
20
15
2
13
10
14
4
11
8
22
45
20
19
15
2
13
10
14
4
11
8
22
46
20
19
14
15
2
13
10
7
4
11
8
22
47
RemoveMax()
20
19
14
15
2
13
10
8
4
11
7
22
48
Restructure
3
19
14
15
2
13
10
8
4
11
7
20
22
49
Restructure
19
3
14
15
2
13
10
8
4
11
7
20
22
50
Restructure
19
15
14
3
2
13
10
8
4
11
7
20
22
51
RemoveMax()
19
15
14
10
2
13
3
8
4
11
7
20
22
52
Restructure
7
15
14
10
2
13
3
8
4
11
19
20
22
53
Restructure
15
13
14
10
2
7
3
8
4
11
19
20
22
54
RemoveMax()
1
13
14
10
2
7
3
8
4
15
11
19
20
22
55
Restructure
14
13
11
10
2
7
3
8
4
15
1
19
20
22
56
RemoveMax()
4
13
11
10
2
7
3
8
14
15
1
19
20
22
57
Restructure
13
10
11
4
2
7
3
8
14
15
1
19
20
22
58
13
10
11
4
2
7
3
8
14
15
1
19
20
22
13
10
11
4
7
8
1
2
3
5
14
15
19
20
22
59
60
procedure heapify(A, n)
(start is assigned the index in A' of the last parent node)
(the last element in a 0-based array is at index n-1;
find the parent of that element)
start floor ((n - 2) / 2)
while start 0 do
(downHeap the node at index 'start' to the proper place)
downHeap(A, start, n - 1)
(go to the next parent node)
start start - 1
// after this loop array A is a heap
62
HeapSort in Place
63