Heap Data Structure
Heap Data Structure
A Heap is a complete binary tree data structure that satisfies the heap property: for
every node, the value of its children is greater than or equal to its own value. Heaps are
usually used to implement priority queues, where the smallest (or largest) element is
always at the root of the tree.It either follows max heap or min heap property.
A Heap is a special Tree-based data structure in which the tree is a complete binary
tree. Since a heap is a complete binary tree, a heap with N nodes has log N height. It is
useful to remove the highest or lowest priority element. It is typically represented as
an array. There are two types of Heaps in the data structure.
Min-Heap
In a Min-Heap the key present at the root node must be less than or equal among the
keys present at all of its children. The same property must be recursively true for all
sub-trees in that Binary Tree. In a Min-Heap the minimum key element present at the
root. Below is the Binary Tree that satisfies all the properties of Min Heap.
A min-heap is a complete binary tree in which the value of each node is less than the
value of its left child and right child. This property is true for every node in the tree. In
this article, we will learn how we can implement the min heap data structure in C++.
To implement a min heap in C++, array data structure is used to represent the node
values of the min heap. So, for any node at index i in the array,
Here are some common operations that can be performed on a Heap Data Structure,
1. Insertion in Min-Heap Data Structure:
Elements can be inserted into the heap following a similar approach as discussed
above for deletion. The idea is to:
● The insertion operation in a min-heap involves the following steps:
● Add the new element to the end of the heap, in the next available position in
the last level of the tree.
● Compare the new element with its parent. If the parent is greater than the
new element, swap them.
● Repeat step 2 until the parent is smaller than or equal to the new element, or
until the new element reaches the root of the tree.
● The new element is now in its correct position in the min heap, and the heap
property is satisfied
Deletion: To remove the minimum element from the min heap, we first swap the root
node with the last element in the array, remove the last element, and then adjust the
heap property by repeatedly swapping the element with its smallest child until it is in the
correct position.
Heapify: A heapify operation can be used to create a min heap from an unsorted
Huffman Coding: Min heap is used to build the Huffman tree which is used for
the data compression.
Dijkstra's Shortest Path Algorithm: Min heaps are used in Dijkstra's algorithm
in graph theory to find the next vertex with minimum distance from the source
vertex.
Max-Heap
In a Max-Heap the key present at the root node must be greater than or equal among
the keys present at all of its children. The same property must be recursively true for
all sub-trees in that Binary Tree. In a Max-Heap the maximum key element present at
the root. Below is the Binary Tree that satisfies all the properties of Max Heap.
Difference between Min Heap and Max Heap
Applications of Heaps:
Heap Sort: Heap Sort is one of the best sorting algorithms that use Binary Heap
to sort an array in O(N*log N) time.
Graph Algorithms: The heaps are especially used in Graph Algorithms like
Dijkstra’s Shortest Path and Prim’s Minimum Spanning Tree.
The left and the right child of ith node are in indices 2*i+1 and 2*i+2.
2. We set the index of the current element, i, as the ‘MAXIMUM’.
3. If arr[2 * i + 1] > arr[i], i.e., the left child is larger than the current value, it is
set as ‘MAXIMUM’.
4. Similarly if arr[2 * i + 2] > arr[i], i.e., the right child is larger than the current
void MaxHeap::MaxHeapify(int i)
{
int l = lChild(i);
int r = rChild(i);
int largest = i;
if (l < heapSize && arr[l] > arr[i])
largest = l;
if (r < heapSize && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
MaxHeapify(largest);
}}