Question: In this activity you need to modify PQ implementation ( BinaryHeap . cpp ) to make it based on 3 - way ( 3 -
In this activity you need to modify PQ implementation BinaryHeapcpp to make it based on way children heap tree. To do so you need to update swimup and sinkdown functions.
way heap is also discussed in the this week's lecture m:s
After making required modification, demonstrate your new code with the following operations.
Add random items to PQ Each item integer will be randomly selected between and
Show the PQ array. Note that th item of the array is always skipped.
Repeatedly delete max of PQ times showing the PQ array.
Expected output of your program will be similar to
PQ created
Delete
Delete
Delete
Delete
Delete
Note that there is no user input required for this program
#include
#include
#include
#include
#include std::setw
#define N
using namespace std;
class BinaryHeap
public:
BinaryHeap; Construction
bool lessint i int j;
void exchint i int j;
void swimint k;
void sinkint k;
bool isEmpty;
int size;
void insertint v;
int delMax;
void ListArray;
void printTint x int id;
private:
int N ;
int pq;
int capacity ;
;
Initialize the class
BinaryHeap::BinaryHeap
pq new intcapacity;
cout "A new priority queue with capacity capacity was created...!" endl ;
void BinaryHeap::ListArray
for int i; i N; i Remember we have "size" items
cout pqi;
void BinaryHeap::swimint k
while k && lessk k
exchk k;
k k;
void BinaryHeap::isEmpty
return N ;
int BinaryHeap::size
return N;
void BinaryHeap::insertint v
pqN v;
swimN;
int BinaryHeap::delMax
int max pq;
exch N;
pqN NULL;
sink;
return max;
void BinaryHeap::sinkint k
while k N
int j k;
if j N && lessj j j;
if lessk j break;
exchk j;
k j;
bool BinaryHeap::lessint i int j
if pqi pqj
return true;
return false;
void BinaryHeap::exchint i int j
int t pqi; pqi pqj; pqj t;
void BinaryHeap::printTint x int id
if xN return;
printTxid;
cout setwid pqx endl;
printTxid;
The program lunches here
int main
BinaryHeap BH;
for int i; i ; i
BHinsert rand;
BHListArray;
cout
;
BHprintT;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
