Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students explore these related Databases questions

Question

Define Administration?

Answered: 3 weeks ago

Question

Define Decision making

Answered: 3 weeks ago

Question

Describe several aggregation operators.

Answered: 3 weeks ago