A heap is a type of tree data structure where each node is either greater than or equal to or less than or equal to the values of its children. Based on this property, heaps are classified into two types:
- Min Heap: Each node is less than or equal to the values of its children.
- Max Heap: Each node is greater than or equal to the values of its children.
Additionally, a heap is a complete binary tree, meaning all levels are fully filled except possibly the last level, which is filled from left to right.
In this article, we will learn how to implement the heap data structure in C++ using classes. We will focus on a binary heap. For a k-ary heap, refer to this article – K-ary Heap Data Structure.
Implementation of Heap in C++
In C++, heaps are often stored in arrays for faster access and modification. Due to the complete binary tree property, the index of the child and parent nodes can be easily computed:
- Left child: 2 * i + 1
- Right child: 2 * i + 2
- Parent: (i - 1) / 2
All indices assume 0-based indexing.
Heap Representation in C++
In C++, we can create a class with an vector and a size variable to represent the heap. We can implement the required functions as the member functions of this class
class Heap {
private:
std::vector<int> array;
int size;
void heapify(int i);
public:
Heap(int capacity);
void insert(int key);
int extractMax();
int getMax() const;
void deleteKey(int i);
void increaseKey(int i, int newValue);
void printHeap() const;
};
Heap Operations in C++
There are several essential operations used with heaps:
- Insert: Adds a new element to the heap while maintaining the heap property (either min or max heap ordering).
- Extract Min/Max: Removes and returns the minimum/maximum element from the heap.
- Peek: Returns the minimum/maximum element without removing it.
- Heapify: Reorganizes a subtree for a given node to ensure the heap property holds.
- Delete: Removes a specific element from the heap.
- Increase/Decrease Key: Changes the value of an existing element in the heap.
- Build Heap: Converts an array into a proper min or max heap.
Heapify Implementation in C++
The heapify function is implemented with the function signature: void Heap::heapify(int i) where i is the index on which heapify is called.
- Initialize the largest node as the current node i.
- Calculate the left child index as 2 * i + 1.
- Calculate the right child index as 2 * i + 2.
- Check if the left child exists (is within the array bounds) and is greater than the current largest node.
- If true, update the largest node to the left child.
- Check if the right child exists (is within the array bounds) and is greater than the current largest node.
- If true, update the largest node to the right child.
- If the largest node is not the current node i, swap the current node with the largest node using the std::swap function.
- Recursively apply the heapify operation to the subtree rooted at the largest node.
Insert Key Implementation in C++
The insert function is implemented with the function signature: void Heap::insert(int key).
- Append the new key to the end of the array.
- Initialize the index i as the index of the newly inserted key (last element of the array).
- While the key at index i is greater than its parent and i is not the root:
- Swap the key with its parent.
- Update i to the parent index.
- Continue this process until the heap property is restored.
Extract Max Implementation in C++
The extractMax function is implemented with the function signature: int Heap::extractMax().
- Check if the heap is empty. If true, throw an underflow_error.
- If the heap has only one element, remove and return that element.
- Store the maximum value (root of the heap) in a temporary variable.
- Replace the root of the heap with the last element in the array.
- Remove the last element from the array.
- Apply the heapify operation on the root to restore the heap property.
- Return the stored maximum value.
Get Max Implementation in C++
The getMax function is implemented with the function signature: int Heap::getMax() const.
- Check if the heap is empty. If true, throw an underflow_error.
- Return the root of the heap, which is the maximum value.
Delete Key Implementation in C++
The deleteKey function is implemented with the function signature: void Heap::deleteKey(int i).
- Check if the index i is valid (within the array bounds). If not, throw an out_of_range exception.
- Replace the key at index i with the last element in the array.
- Remove the last element from the array.
- Apply the heapify operation on the subtree rooted at index i to restore the heap property.
Increase Key Implementation in C++
The increaseKey function is implemented with the function signature: void Heap::increaseKey(int i, int newValue).
- Check if the index i is valid and the new value is greater than the current value. If not, throw an invalid_argument exception.
- Update the value at index i to the new value.
- While the key at index i is greater than its parent and i is not the root:
- Swap the key with its parent.
- Update i to the parent index.
- Continue this process until the heap property is restored.
Print Heap Implementation in C++
The printHeap function is implemented with the function signature: void Heap::printHeap() const.
- Iterate through each element in the array.
- Print each element followed by a space.
- Print a newline at the end.
C++ Program to Implement
C++
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;
// Class representing a Max-Heap
class Heap {
private:
// Vector to store heap elements
vector<int> array;
// Helper function to maintain the heap property
void heapify(int i);
public:
// Default constructor
Heap() = default;
// Function to insert a new key into the heap
void insert(int key);
// Function to extract the maximum element from the heap
int extractMax();
// Function to get the maximum element from the heap
int getMax() const;
// Function to delete a key at a given index
void deleteKey(int i);
// Function to increase the value of a key at a given
// index
void increaseKey(int i, int newValue);
// Function to print the heap elements
void printHeap() const;
};
// Helper function to maintain the heap property
void Heap::heapify(int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
int size = array.size();
if (left < size && array[left] > array[largest])
largest = left;
if (right < size && array[right] > array[largest])
largest = right;
if (largest != i) {
swap(array[i], array[largest]);
heapify(largest);
}
}
// Function to insert a new key into the heap
void Heap::insert(int key)
{
array.push_back(key);
int i = array.size() - 1;
while (i != 0 && array[(i - 1) / 2] < array[i]) {
swap(array[i], array[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
// Function to extract the maximum element from the heap
int Heap::extractMax()
{
if (array.size() <= 0) {
throw underflow_error("Heap underflow");
}
if (array.size() == 1) {
int root = array[0];
array.pop_back();
return root;
}
int root = array[0];
array[0] = array.back();
array.pop_back();
heapify(0);
return root;
}
// Function to get the maximum element from the heap
int Heap::getMax() const
{
if (array.size() <= 0) {
throw underflow_error("Heap is empty");
}
return array[0];
}
// Function to delete a key at a given index
void Heap::deleteKey(int i)
{
if (i >= array.size()) {
throw out_of_range("Invalid index");
}
array[i] = array.back();
array.pop_back();
heapify(i);
}
// Function to increase the value of a key at a given index
void Heap::increaseKey(int i, int newValue)
{
if (i >= array.size() || array[i] >= newValue) {
throw invalid_argument(
"Invalid index or new value is not greater");
}
array[i] = newValue;
while (i != 0 && array[(i - 1) / 2] < array[i]) {
swap(array[i], array[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
// Function to print the heap elements
void Heap::printHeap() const
{
for (int val : array)
cout << val << " ";
cout << endl;
}
int main()
{
Heap heap;
heap.insert(3);
heap.insert(2);
heap.insert(15);
heap.insert(5);
heap.insert(4);
heap.insert(45);
cout << "Max Heap array: ";
heap.printHeap();
cout << "Extracted max value: " << heap.extractMax()
<< endl;
cout << "Max Heap array after extraction: ";
heap.printHeap();
return 0;
}
OutputMax Heap array: 45 5 15 2 4 3
Extracted max value: 45
Max Heap array after extraction: 15 5 3 2 4
The min heap can be easily implemented using the same algorithms by changing the comparison operator to less than (<
).
Similar Reads
Stack implementation in C++
Stack is the fundamental data structures used in the computer science to the store collections of the objects. It can operates on the Last In, First Out (LIFO) principle where the most recently added the object is the first one to be removed. It can makes the stacks highly useful in the situations w
4 min read
Heap in C++ STL
The heap data structure can be implemented in a range using STL which provides faster max or min item retrieval, and faster insertion and deletion on sorted data and also works as a sub-routine for heapsort. STL Functions for Heap Operationsmake_heap(): Converts given range to a heap.push_heap(): Ar
6 min read
B-Tree Implementation in C++
In C++, B-trees are balanced tree data structures that maintain sorted data and allow searches, sequential access, insertions, and deletions in logarithmic time. B-trees are generalizations of binary search trees (BST) in that a node can have more than two children. B-trees are optimized for systems
13 min read
C++ STL Cheat Sheet
The C++ STL Cheat Sheet provides short and concise notes on Standard Template Library (STL) in C++. Designed for programmers that want to quickly go through key STL concepts, the STL cheatsheet covers the concepts such as vectors and other containers, iterators, functors, etc., with their syntax and
15+ min read
C++ Programming Examples
Writing C++ programs yourself is the best way to learn the C++ language. C++ programs are also asked in the interviews. This article covers the top practice problems for basic C++ programs on topics like control flow, patterns, and functions to complex ones like pointers, arrays, and strings. C++ Tu
9 min read
C++ Standards and Implementations
C++ programming language is widely used and known for its power, versatility, and performance. C++ is an extension of the C programming language created by Danish computer scientist Bjarne Stroustrup. With time several C++ standards have been introduced with new features and enhancements. In this ar
6 min read
How to implement our own Vector Class in C++?
The given task is to implement a class in C++ which behaves just like the Vector class.Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements a
5 min read
C++ Cheatsheet
This is a C++ programming cheat sheet. It is useful for beginners and intermediates looking to learn or revise the concepts of C++ programming. While learning a new language, it feels annoying to switch pages and find different websites for different concepts that are easily understandable. You can
15+ min read
std::string class in C++
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character. String vs Character ArrayString Ch
8 min read
Segment Tree in C++
A Segment Tree is a data structure that is used for various range query problems, particularly in competitive programming. It allows for efficient query operations on arrays, such as finding the minimum, maximum, or sum of elements in a given range. In this article, we will learn how to implement a
7 min read