Lab04 Priority Queues and Heaps
Lab04 Priority Queues and Heaps
1. No, parent
is lesser 2. Not a complete binary 3. A complete binary
1 than child 3 tree & not a maxheap. 3 tree 3 a maxheap
2
/
g \
2 2
/
Ic
missing
-
/ \ & not a
/ \
a
/
3 4 3 4 3
-
\ / \ / \
1 2 1 2 1
2. (25 min) The following numbers are enqueued sequenHally into a priority queue. Draw the resulted
heap for every enqueue.
42586937
1) 2) 4
3) 4
4) 8
2 2 5 4
5
5) 8
0
6) 8 7) 8] 9
4 9 ⑧
5 4 8
6
2 62
[
2
6
⑧
8 -
I 6
2
9 I 3
4
↑
2
---
2 5 5
3
I 3
5 4 -
8
9
6 4
⑧
2 I
6 8 7 8
2
[
2
/I R 3
4 5
2 5 6
'3
2
1
Priority quere I heap
⑮
1518 3 854
I 56 I
O 4
8
x1c 2xH
=
2(2) 2 5
+ =
5
+2
=
2n
uRc
=
-largest
0 children
= Parents
40 heapity:
swap
with
=40
⑧ child
y =
greatest
83480506
index
formula take
so using
19 parent
is the
to find child
in LC
ofparent (2) put which is 8
index 5
1 5 so
2C2) +
=
1) Y 2
a
a +
5)
.
7) 8
Maxheap -
3. (25 min) In the previous quesHon, the heap is formed by enqueuing a number at a Hme. It works but
it takes O(n lg n) Hme for n input because each enqueue takes O(lg n) Hme. A faster way to make a
heap out of an iniHal array in Ѳ(n) Hme without enqueuing one at a Hme (see Lecture 4 topic
Forming an IniHal Heap). Draw the resulted heap based on this idea. Your answer in this quesHon
might be diXerent from the previous one.
;45z ;45z
idgididi jid jid
,
84 bs
,
int . .
maxheap
subneap
" b b
Initial array startfrom
de
last no
4. (25 min) From the Ynal heap you drew in the previous quesHon 2, dequeue all numbers unHl the
priority queue is empty. Draw the resulHng resulted heap for every dequeue.
17
9 2) 8- 7 3) 4)
8
L 7 - 6 -
5
- 6
(
Y 34
-
↳
4 6 4
7 4 4
I
-2
5
/ f
3
/ e
5
2
3 - ⑪ /
"
6
3
it
5)5c
·"En
8)
4
↑
-
2-
↑ -
2
3
Null
aut
NoHce that you actually dequeue 9 Yrst, then 8, 7, 6, 5, 4, 3, and 2 at last. The result is in a form of
sorHng order. This sorHng technique is known as Heapsort which will be discussed further in Lecture
6.
5. (5 min) What is the Hme complexity for enqueue an element, dequeue an element, and forming an
iniHal heap?
(0) n
3
element:
Enquere an
an
2
6. (25 min) An implementaHon of priority queue is provided below. Complete the heapify_enqueue
and heapify_dequeue funcHons, and then run the program.
// filename: PriorityQueueMaxHeap.cpp
#include <iostream>
#include <vector>
using namespace std;
// parent index
int parent_index = (index-1)/2
// swap if parent is smaller if (A[index]> A[parent_index])
swap (A[index], A[parent_index])
heapify_enqueue(parent_index)
// recursion of the function
public:
void enqueue (T element) {
A.push_back (element);
heapify_enqueue (A.size()-1); // start at last element.
}
T dequeue() {
T removed_element = A[0];
A[0] = A[A.size()-1]; // copy last element to root.
A.pop_back(); // remove last element.
heapify_dequeue (0); // start at root.
return removed_element;
}
int size() {
return A.size();
}
void print() {
for (int i = 0; i < A.size(); i++)
cout << A[i] << " ";
cout << endl;
}
};
int main () {
int A[] = {4, 2, 5, 8, 6, 9, 3, 7};
int n = sizeof(A)/sizeof(A[0]);
cout << "Array = ";
printArray (A, n);