Heap in C++ STL - make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()

PythonServer Side ProgrammingProgramming

In this section we will see the heap data structure present in C++ STL. This permits faster input into heap and retrieval of a number always results in the largest number i.e. largest number of the remaining numbers is popped out each time. Other elements of the heap are arranged which depends on the implementation. The heap operations are as follows −

  • make_heap() − This converts a range in a container to a heap.

  • front() − This returns first element of heap which is the largest number.

Example

Let us see the following implementation to get better understanding −

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int main() {
   vector<int> heap = {33, 43, 53, 38, 28};
   make_heap(heap.begin(), heap.end());
   cout <<"Top element is : " << heap.front() << endl;
}

Output

Top element is : 53
  • push_heap() − This helps to re-heapify the heap after inserting an element into heap. The size of the heap is incremented by 1. In the heap, new element is placed appropriately.

  • pop_heap() − This helps to re-heapify the heap after deleting the largest element of the heap. The size of heap is decremented by 1. After deleting an element, the heap elements are reorganized accordingly.

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int main() {
   vector<int> heap = {33, 43, 53, 38, 28};
   make_heap(heap.begin(), heap.end());
   cout <<"Top element is : " << heap.front() << endl;
   heap.push_back(60);
   push_heap(heap.begin(), heap.end());
   cout <<"Top element after insert : " << heap.front() << endl;
   pop_heap(heap.begin(), heap.end());
   heap.pop_back();
   cout <<"Top element after deletion : " << heap.front() << endl;
}

Output

Top element is : 53
Top element after insert : 60
Top element after deletion : 53
  • sort_heap() : This sorts the heap element in ascending order by the heapsorting technique.

Example (C++)

Let us see the following implementation to get better understanding −

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int main() {
   vector<int> heap = {33, 43, 53, 38, 28};
   make_heap(heap.begin(), heap.end());
   cout <<"Before Sort : ";
   for (const auto &i : heap) {
      cout << i << ' ';
   }
   sort_heap(heap.begin(), heap.end());
   cout <<"
After Sort : ";    for (const auto &i : heap) {       cout << i << ' ';    } }

Output

Before Sort : 53 43 33 38 28
After Sort : 28 33 38 43 53
  • is_heap() − This is used to check whether the container is a heap or not. In most implementations, the reverse sorted container is treated as heap. This function returns true heap when this is heap, otherwise false.

  • is_heap_until() − This is used to find the iterator to the position until the container is the heap.

Example

Let us see the following implementation to get better understanding −

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int main() {
   vector<int> heap = {33, 43, 53, 38, 28};
   vector<int>::iterator iter;
   is_heap(heap.begin(), heap.end()) ? cout <<"The is a heap ": cout <<"The is not a heap";
   cout << endl;
   cout < "Heapify" << endl;
   make_heap(heap.begin(), heap.end());
   is_heap(heap.begin(), heap.end()) ? cout <<"The is a heap ": cout <<"The is not a heap";
   cout << endl;
   vector<int>::iterator iter2 = is_heap_until(heap.begin(), heap.end());
   cout <<"The heap elements are : ";
   for (iter=heap.begin(); iter!=iter2; iter++)
      cout << *iter <<" ";
}

Output

The is not a heap
Heapify
The is a heap
The heap elements are : 53 43 33 38 28
raja
Updated on 27-Aug-2020 13:49:11

Advertisements