Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Only Update PQ implementation (BinaryHeap.cpp) to make it based on 3-way (3-children) heap tree. To do so, you will need to update swim-up and

C++ Only

  • Update PQ implementation (BinaryHeap.cpp) to make it based on 3-way (3-children) heap tree. To do so, you will need to update swim-up and sink-down functions.
  • 3-way heap is also discussed in the video (21m:30s).

After making required modification, demonstrate your new code with following operations.

  1. - Add 20 random items to PQ. Each item will be randomly selected between 10 and 99.
  2. - Show the PQ array.
  3. - Delete 5 items, i.e, call delMax() 5 times.
  4. - Show the PQ array.

Please help me modify code provided below to fit format.

include #include #include #include #include // std::setw // #define N 10

using namespace std;

class BinaryHeap { public: BinaryHeap(); // Construction bool less(int i, int j); void exch(int i, int j); void swim(int k); void sink(int k); bool isEmpty(); int size(); void insert(int v); int delMax(); void ListArray(); void printT(int x, int id);

private: int N = 0; int *pq; int capacity = 100;

};,

// Initialize the class BinaryHeap::BinaryHeap() {

pq = new int[capacity]; cout }

void BinaryHeap::ListArray() { for (int i=1; i { cout } }

void BinaryHeap::swim(int k) { while (k > 1 && less(k/2, k)) { exch(k/2, k); k = k/2; } }

bool BinaryHeap::isEmpty() { return N == 0; }

int BinaryHeap::size() { return N; }

void BinaryHeap::insert(int v) { pq[++N] = v; swim(N); }

int BinaryHeap::delMax() { int max = pq[1]; exch(1, N--); pq[N+1] = NULL; sink(1); return max; }

void BinaryHeap::sink(int k) { while (2*k { int j = 2*k; if (j if (!less(k, j)) break; exch(k, j); k = j; } }

bool BinaryHeap::less(int i, int j) { if (pq[i] return true; return false; }

void BinaryHeap::exch(int i, int j) { int t = pq[i]; pq[i] = pq[j]; pq[j] = t; } //1-> 2, 3 //2-> 4, 5 //3-> 6, 7 void BinaryHeap::printT(int x, int id) { if (x>N) return;

printT(2*x+1,id+10);

cout

printT(2*x,id+10); } // The program lunches here int main( ) { BinaryHeap BH; for (int i=0; i BH.insert( rand()%50 +1);

BH.ListArray(); cout BH.printT(1,10);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions