below is the header files and cpp files for my program, check what is wrong with the…
- User
below is the header files and cpp files for my program, check what is wrong with the programs and modify it #ifndef MINHEAP_H #define MINHEAP_H #include
template struct Heap { T data; int priority;
codeHeap(T d, int prio) : data(d), priority(prio) {};};
template class MinHeap { public: MinHeap(int d); /* Constructor that builds a d-ary Min Heap This should work for any d >= 2, but doesn't have to do anything for smaller d.*/ ~MinHeap();
codevoid add(const T& data, int priority); /* adds the item to the heap, with the given priority. */ const T & peek() const; /* returns the element with smallest priority. Break ties however you wish. Throws an exception if the heap is empty. */ void remove(); /* removes the element with smallest priority. Break ties however you wish. Throws an exception if the heap is empty. */ bool isEmpty(); /* returns true iff there are no elements on the heap. */ private: // whatever you need to naturally store things. // You may also add helper functions here. std::vector<Heap<T>> heap_; //store heaps int d_; //d-ary void bubbleUp (int i); void trickleDown (int i);};
#endif#include "MinHeap.h" using namespace std;
template MinHeap::MinHeap (int d) : d_(d_) { if (d <2){ throw ("d must be greater than 2"); } }
template MinHeap::~MinHeap() {}
template void MinHeap::add(const T& data, int priority) { Heap newHeap(data, priority); heap_.push_back(newHeap); bubbleUp(heap_.size()-1); }
template const T& MinHeap::peek() const { if (heap_.empty()) { throw ("Heap is empty"); } return heap_[0].data; }
template void MinHeap::remove() { if (heap_.empty()) { throw ("Heap is empty"); }
codeheap_[0] = heap_[heap_.size()-1]; heap_.pop_back(); trickleDown(0);}
template bool MinHeap::isEmpty() { return heap_.empty(); }
- Assistant