+ 2

Heaps(STL)

---Is there anything that I should change/add from this code for the required assignment? thanks A heap data structure can be efficiently implemented in a range using the C++ Standard Template Library (STL). In this exercise you are asked to utilize STL in order to build and manipulate a heap efficiently. In particular, you are requested to: 1. Make a heap consisting of 10 different integers of your own choosing. Include a statement to show the maximum element of the heap. 2. Add a new value that is the mean of the random values you creates in the previous step. Floor the value if needed (truncate decimal part). 3. Delete the maximum element of the heap and 4. Sort the heap. Write down the code to achieve all the operations mentioned above and clarify which section of the code does what. #include<iostream> #include <vector> #include<algorithm> #include <stdlib.h> using namespace std; int main() { vector<int> v1; int i; // Initialized a vector for(i=0;i<10;i++){ v1.push_back(rand()); } // Converting vector into a heap using make_heap() make_heap(v1.begin(), v1.end()); // Displaying heap elements cout << "The heap elements Before Sorting are : "; for (int &val : v1) cout << val << " "; cout << endl; // sorting heap using sort_heap() sort_heap(v1.begin(), v1.end()); cout << "The maximum element of heap that are deleted : "; cout << v1.front() << endl; //using pop_heap() to delete maximum element pop_heap(v1.begin(), v1.end()); v1.pop_back(); // Displaying heap elements after sorting cout << "The heap elements after sorting are : "; for (int &val : v1) cout << val << " "; return 0; }

13th Dec 2019, 1:02 AM
lakas
1 ответ
0
Hi @Charmaine Labastida, You did forget step 2 and inverted steps 3 and 4. You should delete before sorting the heap. An improved version of the code is: #include<iostream> #include <vector> #include<algorithm> #include <stdlib.h> using namespace std; int main() { vector<int> v1; int i; // Initialized a vector for(i=0;i<10;i++){ v1.push_back(rand()%100); } cout << "Generated elements : " << endl; for (int &val : v1) cout << val << " "; cout << endl; // Converting vector into a heap using make_heap() make_heap(v1.begin(), v1.end()); // Displaying heap elements cout << "The heap elements are : " << endl; for (int &val : v1) cout << val << " "; cout << endl; cout << "Max heap element : " << v1.front() << endl; int sum = 0; for (int &val : v1) sum += val; int avg = sum / v1.size(); cout << "Elements Avg: " << avg << endl; v1.push_back(avg); push_heap(v1.begin(),v1.end()); cout << "Now the heap elements are : " << endl; for (int &val : v1) cout << val << " "; cout << endl; cout << "The maximum element of heap that are deleted : "; cout << v1.front() << endl; //using pop_heap() to delete maximum element pop_heap(v1.begin(), v1.end()); v1.pop_back(); // sorting heap using sort_heap() sort_heap(v1.begin(), v1.end()); // Displaying heap elements after sorting cout << "The heap elements after sorting are : " << endl; for (int &val : v1) cout << val << " "; cout << endl; return 0; }
14th Dec 2019, 3:35 PM
Mark