Question: please write the main function for this program and the output should be like that only: please provide me a complete main function and runnable.
please write the main function for this program and the output should be like that only: please provide me a complete main function and runnable.
Inserting elements: 10, 20, 30, 40, 50
Minimum element: 10
Maximum element: 50
Removing minimum element: 10
Minimum element: 20
Maximum element: 50
Removing maximum element: 50
Minimum element: 20
Maximum element: 40
#include #include using namespace std; // Define the interval heap class class DEQueue { private: // Declare a vector to store the heap elements vector heap; // Helper function to get the left child index of a given node int leftChild(int i) { return 2 * i + 1; } // Helper function to get the right child index of a given node int rightChild(int i) { return 2 * i + 2; } // Helper function to get the parent index of a given node int parent(int i) { return (i - 1) / 2; } // Helper function to swap two elements in the heap void swap(int i, int j) { int temp = heap[i]; heap[i] = heap[j]; heap[j] = temp; } // Helper function to maintain the heap property while inserting an element void heapifyUp(int i) { // Compare the inserted element with its parent // and swap them if the inserted element is smaller while (i > 0 && heap[i] < heap[parent(i)]) { swap(i, parent(i)); i = parent(i); } } // Helper function to maintain the heap property while deleting an element void heapifyDown(int i) { int minIndex = i; int l = leftChild(i); int r = rightChild(i); // Compare the current element with its left and right children // and find the smallest element among them if (l < heap.size() && heap[l] < heap[minIndex]) minIndex = l; if (r < heap.size() && heap[r] < heap[minIndex]) minIndex = r; // If the current element is not the smallest, swap it with the smallest // and continue the process for the new element if (i != minIndex) { swap(i, minIndex); heapifyDown(minIndex); } } public: // Function to insert an element into the heap void insert(int value) { // Add the new element to the end of the heap heap.push_back(value); // Maintain the heap property heapifyUp(heap.size() - 1); } // Function to delete the minimum element from the heap int extractMin() { // If the heap is empty, return -1 if (heap.empty()) return -1; // Store the minimum element int min = heap[0]; // Replace the root element with the last element heap[0] = heap.back(); heap.pop_back(); // Maintain the heap property heapifyDown(0); // Return the minimum element return min; } // Function to delete the maximum element from the heap int extractMax() { // If the heap is empty, return -1 if (heap.empty()) return -1; // Store the maximum element int max = heap[0]; // Replace the root element with the last element heap[0] = heap.back(); heap.pop_back(); // Maintain the heap property heapifyDown(0); // Return the maximum element return max; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
