make_heap() is used to transform a sequence into a heap. A heap is a data structure which points to highest( or lowest) element and making its access in O(1) time. Order of all the other elements depends upon particular implementation, but remains consistent throughout. This function is defined in the header "algorithm". There are two implementations of make_heap() function. Both of them are explained through this article.
Syntax 1 : make_heap(iter_first, iter_last)
Template : void make_heap (RandomAccessIterator first, RandomAccessIterator last); Parameters : first : The pointer to the starting element of sequence that has to be transformed into heap. last : The pointer to the next address to last element of sequence that has to be transformed into heap.
Below is the demonstrating code :
C++
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
vector<int> v = { 4, 6, 7, 9, 11, 4 };
// Using make_heap() to transform vector into
// a max heap
make_heap(v.begin(),v.end());
// Checking if heap using front() function
cout << "The maximum element of heap is : ";
cout << v.front();
}
OutputThe maximum element of heap is : 11
Syntax 2 : make_heap(iter_first, iter_last, comp)
Template : void make_heap (RandomAccessIterator first, RandomAccessIterator last, comp); Parameters : first : The pointer to the starting element of sequence that has to be transformed into heap. last : The pointer to the next address to last element of sequence that has to be transformed into heap. comp : The comparator function that returns a boolean true/false of the each elements compared. This function accepts two arguments. This can be function pointer or function object and cannot change values.
Below is the demonstrating code:
C++
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
// comparator function to make min heap
struct greaters{
bool operator()(const long& a,const long& b) const{
return a>b;
}
};
int main() {
vector<int> v = { 15, 6, 7, 9, 11, 45 };
// using make_heap() to transform vector into
// a min heap
make_heap(v.begin(),v.end(), greaters());
// checking if heap using
// front() function
cout << "The minimum element of heap is : ";
cout << v.front();
}
OutputThe minimum element of heap is : 6
Possible application : This function can be used in scheduling. In scheduling, a new element is inserted dynamically in iterations. Sorting again and again to get maximum takes much complexity O(nlogn), instead of that we use "push_heap()" function to heapify the heap made in O(logn) time . The code below depicts its implementation.
Implementation:
C++
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
// initializing vector;
// initial job priorities
vector<int> vi = { 15, 6, 7, 9, 11, 19};
// No. of incoming jobs.
int k = 3;
// using make_heap() to transform vector into
// a min heap
make_heap(vi.begin(),vi.end());
// initializing job variable
int a = 10;
for ( int i=0; i<k; i++)
{
// push a job with priority level
vi.push_back(a);
// transform into heap ( using push_heap() )
push_heap(vi.begin(), vi.end());
//checking top priority job
// front() function
cout << "Job with maximum priority is : ";
cout << vi.front() << endl;
// increasing job priority level
a = a + 10;
}
return 0;
}
OutputJob with maximum priority is : 19
Job with maximum priority is : 20
Job with maximum priority is : 30