Question: Given is a cpp file heap.cpp. You have to write a coden in c++ to delete the minimum element. In the main function, some numbers

Given is a cpp file "heap.cpp". You have to write a coden in c++ to delete the minimum element. In the main function, some numbers are being inserted into the heap and delete function is called twice. Then the heap is printed.

Expected output if compiled and run is "Heap --> 3 5 6 24".

Along with the completed cpp file, submit a document containing description of the the deletion process (10 lines Maximum).

// "heap.cpp"

#include  #include  #include  #include  using namespace std; /* * Class Declaration */ class BinaryHeap { private: vector  heap; int left(int parent); int right(int parent); int parent(int child); void heapifyup(int index); void heapifydown(int index); public: BinaryHeap() {} void Insert(int element); void DeleteMin(); int ExtractMin(); void DisplayHeap(); int Size(); }; /* * Return Heap Size */ int BinaryHeap::Size() { return heap.size(); } /* * Insert Element into a Heap */ void BinaryHeap::Insert(int element) { heap.push_back(element); heapifyup(heap.size() -1); } /* * Delete Minimum Element */ void BinaryHeap::DeleteMin() { //Write Your Code Here } /* * Extract Minimum Element */ int BinaryHeap::ExtractMin() { if (heap.size() == 0) { return -1; } else return heap.front(); } /* * Display Heap */ void BinaryHeap::DisplayHeap() { vector ::iterator pos = heap.begin(); cout<<"Heap --> "; while (pos != heap.end()) { cout<<*pos<<" "; pos++; } cout<= 0 && parent(in) >= 0 && heap[parent(in)] > heap[in]) { int temp = heap[in]; heap[in] = heap[parent(in)]; heap[parent(in)] = temp; heapifyup(parent(in)); } } /* * Heapify- Maintain Heap Structure top down */ void BinaryHeap::heapifydown(int in) { int child = left(in); int child1 = right(in); if (child >= 0 && child1 >= 0 && heap[child] > heap[child1]) { child = child1; } if (child > 0 && heap[in] > heap[child]) { int temp = heap[in]; heap[in] = heap[child]; heap[child] = temp; heapifydown(child); } } /* * Main Contains Menu */ int main() { BinaryHeap h; h.Insert(3); h.Insert(24); h.Insert(6); h.Insert(5); h.Insert(2); h.Insert(1); h.DeleteMin(); h.DeleteMin(); h.DisplayHeap(); return 0; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!