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

A heap is a complete binary tree data structure where parent nodes are either greater than (max heap) or less than (min heap) their children. C++ STL provides several heap operations through the <algorithm> header that work with containers like vectors.

Basic Heap Operations

make_heap() and front()

The make_heap() function converts a range in a container to a heap structure. Use front() to access the largest element in a max heap ?

#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;
    return 0;
}
Top element is : 53

push_heap() and pop_heap()

Use push_heap() to maintain heap property after adding an element, and pop_heap() to reorganize after removing the largest element ?

#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;
    return 0;
}
Top element is : 53
Top element after insert : 60
Top element after deletion : 53

Heap Sorting

sort_heap()

The sort_heap() function sorts heap elements in ascending order using the heapsort algorithm ?

#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 << "\nAfter Sort : ";
    for (const auto &i : heap) {
        cout << i << ' ';
    }
    return 0;
}
Before Sort : 53 43 33 38 28
After Sort : 28 33 38 43 53

Heap Validation Functions

is_heap() and is_heap_until()

Use is_heap() to check if a container maintains heap property, and is_heap_until() to find the position until which heap property is maintained ?

#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 << "This is a heap" : cout << "This is not a heap";
    cout << endl;
    
    cout << "Heapify" << endl;
    make_heap(heap.begin(), heap.end());
    
    is_heap(heap.begin(), heap.end()) ? cout << "This is a heap" : cout << "This 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 << " ";
    }
    return 0;
}
This is not a heap
Heapify
This is a heap
The heap elements are : 53 43 33 38 28

Summary of Heap Functions

Function Purpose Time Complexity
make_heap() Convert range to heap O(n)
push_heap() Insert element into heap O(log n)
pop_heap() Remove largest element O(log n)
sort_heap() Sort heap in ascending order O(n log n)
is_heap() Check if range is heap O(n)

Conclusion

C++ STL heap functions provide efficient operations for maintaining and manipulating heap data structures. Use make_heap() to create heaps, push_heap() and pop_heap() for insertion and deletion, and sort_heap() for sorting.

Updated on: 2026-03-25T10:06:26+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements