0% found this document useful (0 votes)
60 views

Lab04 Priority Queues and Heaps

1. The document discusses priority queues and heaps. It provides examples of binary trees that are and are not max heaps based on whether the value of each node is greater than or equal to its children. 2. It asks to draw the resulting heap for a sequence of numbers enqueued sequentially into a priority queue. The sequence is 42586937 and it shows the heap after each number is enqueued. 3. Key points are that a max heap has each node greater than or equal to its children, examples of binary trees that are and are not max heaps are given, and the resulting heap is drawn after each number is enqueued from the given sequence.

Uploaded by

husnazk.work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Lab04 Priority Queues and Heaps

1. The document discusses priority queues and heaps. It provides examples of binary trees that are and are not max heaps based on whether the value of each node is greater than or equal to its children. 2. It asks to draw the resulting heap for a sequence of numbers enqueued sequentially into a priority queue. The sequence is 42586937 and it shows the heap after each number is enqueued. 3. Key points are that a max heap has each node greater than or equal to its children, examples of binary trees that are and are not max heaps are given, and the resulting heap is drawn after each number is enqueued from the given sequence.

Uploaded by

husnazk.work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Or largest

TCP2101 Algorithm Design and Analysis


*/i*
Lab 4 Priority Queues and Heaps

Learning Outcomes heap sort:only parent any children


 Understand how heap and priority queue work.
with
greater
heapify,swap child
1. (15 min) A maxheap is a complete binary tree in which the value of each node is greater than or
equal to the values of its children (parent >= children). Which of the following binary trees is not a
maxheap? Explain your answer.

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

4. complete tree 5. full


A binary tree 6.
Not a
Not a complete tree
5 5 xRc 5 maxheap
maxheap 3 maxheap 8 not a

-
/ \ & 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) +
=

the last level with


tree only have

PS: A complete & not all the levels with missing


leftchild missing
left child
58 9 s EEnqueve priority queue
4 i2
4 to a
6
·& neapify

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

worst time complexity


element (0) n
Dequeve
=

an

Form an initial heap= (0) n

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;

template <typename T>


void printArray (T A[], int n) {
for (int i = 0; i < n; i++)
cout << A[i] << " ";
cout << endl;
}

template <typename T>


class PriorityQueue {
vector<T> A;

void heapify_enqueue (int index) { // used in heapify enqueue.


if (index == 0) // if already at root.
return;

// 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

void heapify_dequeue (int index) { // used in heapify dequeue.


int max; // max index
// left child index
int left = 2* index+1;
// right child index
int right = 2 * index +2;
// compare and find the greatest child
if (left < A.size() && A [left] > A[index])
max = left;
else
max = index;

if (right < A.size() && A [right] > A[index])


max = right;
if (max != index) {
3
swap (A[index], A[max]);
heapify_dequeue (max); // recursion
}
}

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);

cout << "\nEnqueue\t: PriorityQueue\n";


PriorityQueue<int> pq;
for (int i = 0; i < n; i++) {
cout << A[i] << "\t: ";
pq.enqueue (A[i]);
pq.print();
}

cout << "\nDequeue\t: PriorityQueue\n";


for (int i = 0; i < n; i++) {
cout << pq.dequeue() << "\t: ";
pq.print();
}
}

You might also like