CSI2110 Data Structures and Algorithms
CSI2110 Data Structures and Algorithms
Data Structures
and Algorithms
1
Heaps
• Heaps
• Properties
• Deletion, Insertion, Construction
• Implementation of the Heap
• Implementation of Priority Queue
using a Heap
• An application: HeapSort
2
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:
key(parent) £ key(child)
REMEMBER:
complete
binary tree
all levels are full,
except the
last one, which is
left-filled
3
Max-heap
key(parent) ³ key(child)
40
35 26
15 19 17 20
1 13 14 12 11 8
4
We store the keys in the internal nodes only
15 9
16
depth keys
0 1
1 2
h-2 2h-2
h-1 at least 1
h
6
Notice that ….
(2, Sue)
7
Removal From a Heap
10
Downheap Continues
11
End of Downheap
13
Heap Insertion
14
Upheap
• Swap parent-child keys out of order
15
Upheap Continues
16
End of Upheap
18
Bottom-up Heap
Construction
19
Construction of a Heap
Idea: Recursively re-arrange each sub-tree
in the heap starting with the leaves
6th 5th
begin here
4th 3rd 2nd 1st
begin here
HEAP
HEAP HEAP
20
Example 1 (Max-Heap)
2
7 3 10 8 6
1 9 6 3
2 2 2
P P
4 5 4 5 4 10
P P
7 6 10 8 9 P 6 10 8 9 P 6 5 8
1 9 3 1 7 3 1 7 3
7«9 5 « 10
21
Example 1
2
P
4 10
P
9 P 6 5 8
1 7 3
9
9
4«9
4
2
P P
9 10
7 6 5 8
22
1 4 3
Example 1
2
P P
9 10
10
7 6 5 8
2
Finally: 2 « 10
1 4 3
5 8
10
2«8
2
5 8
10
9 8
7 6 5 2
1 4 3 23
--- keys given one at a time ---
Example 2 (min-heap)
[20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]
16 15 4 12 6 7 23 20
25 5 11 27
16 15 4 12 6 7 23 20
24
Example 2
20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]
25 5 11 27
16 15 4 12 6 9 23 20
15 4 6 23
16 25 5 12 11 9 27 20
25
Example 2
20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]
7 8
15 4 6 23
16 25 5 12 11 9 27 20
4 6
15 5 8 23
16 25 7 12 11 9 27 20
26
Example 2
20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]
10
4 6
15 5 8 23
16 25 7 12 11 9 27 20
5 6
15 7 8 23
16 25 10 12 11 9 27 20
27
Analysis of Heap Construction
Number of swaps
h=4
3 swaps 2 level 0
2 swaps 4 5
level 1
1 swap 7 3 10 8 level 2
0 swaps 1 9 6
level 3
Number of swaps
level 0
1
L
At level i the number of swaps is
≤ L–i for each node
At level i there are ≤ 2i nodes
L
Total: ≤ S(L – i)·2i
i=0
29
Calculating O(S(L – i)·2 )
i
Consider Sj·2-j:
S j·2-j = 1/2 + 2 1/4 + 3 1/8 + 4 1/16 + …
= 1/2 + 1/4 + 1/8 + 1/16 + … <= 1
+ 1/4 + 1/8 + 1/16 + … <= 1/2
+ + 1/8 + 1/16 + … <= 1/4
Sj·2-j <= 2
So 2L Sj 2-j <= 2.2L =2n where L is O(log 30
n)
L
2L Sj/2
j=1
j
≤ 2L+1 = 2n
Where L is O(log n)
O(n)
31
Review
2
3
D I
4 5 6 7
B E L O
8 9 10 11 12 13
A C F G H N
2i 2i+1
1 2 3 4 5 6 7 8 9 10 11 12 13
H D I B E L O A C F G H N
34
Reminder …..
1 n = 11
2 3
I
4 5 6 7
8 9 10 11
35
Implementation of a Priority
Queue with a Heap
36
(upheap)
O(log n)
O(1)
O(log n)
37
Application: Sorting
Heap Sort
n re-arrange O(log n)
! "
!
38
When there are i nodes left in the PQ: ëlog iû
n
àTOT = S ëlog iû
i=1
40
HeapSort in Place
41
RemoveMax()
22
19 20
15 13 14 11
2 10 5 4 1 8 3 7
7
19 20
15 13 14 11
2 10 5 4 1 8 3 22
15 13 14 11
2 10 5 4 1 8 3 22
43
20
19 7
15 13 14 11
2 10 5 4 1 8 3 22
44
20
19 14
15 13 7 11
2 10 5 4 1 8 3 22
45
RemoveMax()
20
19 14
15 13 8 11
2 10 5 4 1 7 3 22
46
Restructure
3
19 14
15 13 8 11
2 10 5 4 1 7 20 22
47
Restructure
19
3 14
15 13 8 11
2 10 5 4 1 7 20 22
48
Restructure
19
15 14
3 13 8 11
2 10 5 4 1 7 20 22
49
RemoveMax()
19
15 14
10 13 8 11
2 3 5 4 1 7 20 22
50
Restructure
7
15 14
10 13 8 11
2 3 5 4 1 19 20 22
51
Restructure
15
13 14
10 7 8 11
2 3 5 4 1 19 20 22
52
RemoveMax()
1
13 14
10 7 8 11
2 3 5 4 15 19 20 22
53
Restructure
14
13 11
10 7 8 1
2 3 5 4 15 19 20 22
54
RemoveMax()
4
13 11
10 7 8 1
2 3 5 14 15 19 20 22
55
Restructure
13
10 11
4 7 8 1
2 3 5 14 15 19 20 22
56
13
10 11
4 7 8 1
2 3 5 14 15 19 20 22
13 10 11 4 7 8 1 2 3 5 14 15 19 20 22
57
Pseudocode for in-place HEAPSORT
(based on wikipedia pseudocode)
procedure heapsort(A,n) {
input: an unordered array A of length n
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
60