ds23 8
ds23 8
ds23 8
COMP2015 HKBU/CS/JF/2023 1
Priority Queues - Motivation
v Have you ever been jammed by a huge job while you are
waiting for just one-page printout ?
COMP2015 HKBU/CS/JF/2023 3
Priority Queues - Implementation
COMP2015 HKBU/CS/JF/2023 4
Priority Queues - Implementation
COMP2015 HKBU/CS/JF/2023 5
Completeness
Incomplete Complete
A A
C B C
B
D E F G D E F G
H I J H I J
COMP2015 HKBU/CS/JF/2023 6
Heap Structure Property
n Structure Property
COMP2015 HKBU/CS/JF/2023 7
Heap Order Property
n Heap Order Property
q The value at any node should be smaller than (or equal
to) all of its descendants
q Guarantee that the node with the minimum value is at
the root). (Min-heap)
COMP2015 HKBU/CS/JF/2023 8
Are these heaps?
INVALID
Binary Heap:
1. Binary Tree 1
2. Heap
3. Complete
3 VALID
INVALID 4
4 7
5 6
2 5 6
9 8 7
3 7 8
9 11 10
COMP2015 HKBU/CS/JF/2023 9
Heap: Array Implementation
n Array: “tree-like” arrangement (i) level by level, and (ii) left to
right
00
Note: The parent of any
This has a node i is given by i/2
special purpose
(integer division)
01
02 03
Note: The left Note: The right
child of any node child of any node
i is given by 2i 04 05 06 07 i is given by 2i
+1
08 09 10 11 12 13 14 15
COMP2015 HKBU/CS/JF/2023 10
Heap: Array Implementation
For any element at array position i:
q left child is at (2i)
1
A
q right child is at (2i +1)
3
2
C q parent is at (int) (i/2)
B
4 5 6 7
D E F G
8 9 10
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13
COMP2015 HKBU/CS/JF/2023 11
Heap Operations
n Insertion
COMP2015 HKBU/CS/JF/2023 12
Insertion
Insert 4
COMP2015 HKBU/CS/JF/2023 13
Insertion
A heap!
COMP2015 HKBU/CS/JF/2023 14
Heap Operations
Ø DeleteMin: delete the minimum item from the heap
COMP2015 HKBU/CS/JF/2023 15
DeleteMin
COMP2015 HKBU/CS/JF/2023 16
DeleteMin
COMP2015 HKBU/CS/JF/2023 17
Tutorial
v DeleteMin:
5
10 9
17 14 11 13
20 19 16 15 24 22 18
COMP2015 HKBU/CS/JF/2023 18
Tutorial
v DeleteMin:
5
18
9
10 9
18
11
17 14 11
18 13
20 19 16 15 24 22
COMP2015 HKBU/CS/JF/2023 19
Tutorial
v Insert 3:
4 7
5 8 10 9
11 13
COMP2015 HKBU/CS/JF/2023 20
Tutorial
v Insert 3:
Algorithm:
- Insert a node to ensure no gaps 2
- Fix heap
- Percolate UP
4
3 7
5 8
4
3 10 9
11 13 3
8
COMP2015 HKBU/CS/JF/2023 21
Tutorial
v Construct a min Heap by inserting the following values in
this order:
5, 10, 15, 20, 7, 2
5
2
10
7 15
25
Percolate Up
20 7
10 2
15
Percolate Up Percolate Up
COMP2015 HKBU/CS/JF/2023 22
Tutorial
v Do better?
- Add all nodes, fix heap all at once à BuildHeap
algorithm
COMP2015 HKBU/CS/JF/2023 23
BuildHeap
n Given an array of numbers, how do we convert it into a heap?
7
Start at element
int(size/2) = 7
COMP2015 HKBU/CS/JF/2023 24
BuildHeap
COMP2015 HKBU/CS/JF/2023 25
BuildHeap
COMP2015 HKBU/CS/JF/2023 26
Tutorial
v Construct a min Heap with the following values by
BuildHeap algorithm:
12, 5, 11, 3, 10, 2, 9, 4, 8, 15, 7, 6
COMP2015 HKBU/CS/JF/2023 27
Tutorial
v [Heap Implementation]
How do we find the minimum node?
COMP2015 HKBU/CS/JF/2023 28
d-Heaps
3-heaps
COMP2015 HKBU/CS/JF/2023 29
Heapsort
n Heapsort relies on the properties of a heap data structure
to sort a data set.
COMP2015 HKBU/CS/JF/2023 30
Heapsort: no extra storage
COMP2015 HKBU/CS/JF/2023 31
Heapsort - Example
COMP2015 HKBU/CS/JF/2023 32
Heapsort - Example
COMP2015 HKBU/CS/JF/2023 33
Heapsort - Example
COMP2015 HKBU/CS/JF/2023 34
Heapsort – Example
As you delete
the root of the heap,
place the deleted
items at the end of
the array that stores
the heap.
COMP2015 HKBU/CS/JF/2023 35