A heap is a type of tree data structure where each node is either greater than or equal to or is less than or equal to the values of its children.
- It is a complete binary tree, meaning all levels are completely filled except possibly the last, which is filled from left to right.
- It follows the heap property — in a max heap, every parent node is greater than or equal to its children, while in a min heap, every parent node is less than or equal to its children.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a Heap (Max Heap)
typedef struct Heap
{
int *array;
int size;
int capacity;
} Heap;
// Function to create a heap
Heap *createHeap(int capacity)
{
Heap *heap = (Heap *)malloc(sizeof(Heap));
heap->size = 0;
heap->capacity = capacity;
heap->array = (int *)malloc(capacity * sizeof(int));
return heap;
}
// Function to swap two integers
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Function to heapify the node at index i
void heapify(Heap *heap, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < heap->size && heap->array[left] > heap->array[largest])
largest = left;
if (right < heap->size && heap->array[right] > heap->array[largest])
largest = right;
if (largest != i)
{
swap(&heap->array[i], &heap->array[largest]);
heapify(heap, largest);
}
}
// Function to build a max heap from an existing array
void buildHeap(Heap *heap)
{
int n = heap->size;
for (int i = (n - 1) / 2; i >= 0; i--)
heapify(heap, i);
}
// Function to increase the value at a given index
void increaseKey(Heap *heap, int index, int newValue)
{
if (index >= heap->size || heap->array[index] >= newValue)
{
printf("Invalid index or new value is not greater\n");
return;
}
heap->array[index] = newValue;
while (index != 0 && heap->array[(index - 1) / 2] < heap->array[index])
{
swap(&heap->array[index], &heap->array[(index - 1) / 2]);
index = (index - 1) / 2;
}
}
// Function to insert a new value into the heap
void insertHeap(Heap *heap, int value)
{
if (heap->size == heap->capacity)
{
printf("Heap overflow\n");
return;
}
heap->size++;
int i = heap->size - 1;
heap->array[i] = value;
// Fix the heap property if it is violated
while (i != 0 && heap->array[(i - 1) / 2] < heap->array[i])
{
swap(&heap->array[i], &heap->array[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
// Function to extract the root (maximum element)
int extractMax(Heap *heap)
{
if (heap->size <= 0)
return INT_MIN;
if (heap->size == 1)
{
heap->size--;
return heap->array[0];
}
int root = heap->array[0];
heap->array[0] = heap->array[heap->size - 1];
heap->size--;
heapify(heap, 0);
return root;
}
// Function to print the heap
void printHeap(Heap *heap)
{
for (int i = 0; i < heap->size; ++i)
printf("%d ", heap->array[i]);
printf("\n");
}
// Function to delete an element at a given index
void deleteKey(Heap *heap, int index)
{
if (index >= heap->size)
{
printf("Invalid index\n");
return;
}
if (index == heap->size - 1)
{
heap->size--;
return;
}
heap->array[index] = heap->array[heap->size - 1];
heap->size--;
heapify(heap, index);
}
int main()
{
Heap *heap = createHeap(10);
insertHeap(heap, 3);
insertHeap(heap, 2);
insertHeap(heap, 15);
insertHeap(heap, 5);
insertHeap(heap, 4);
insertHeap(heap, 45);
printf("Max Heap array: ");
printHeap(heap);
printf("Extracted max value: %d\n", extractMax(heap));
printf("Max Heap array after extraction: ");
printHeap(heap);
free(heap->array);
free(heap);
return 0;
}
Output
Max Heap array: 45 5 15 2 4 3 Extracted max value: 45 Max Heap array after extraction: 15 5 3 2 4
Implementation of Heap in C
In C, heaps are typically stored in arrays. This makes them faster to access and modify elements. And due to its completeness, we can easily find the index of the child and parents nodes. Here is how to find child and parent nodes of an element at index i in the array:
- Left child: 2 * i + 1
- Right child: 2 * i + 2
- Parent: (i - 1) / 2
All the values are assuming the index starts from 0.
Two types of Heap :
Max-Heap
The value of the root node must be the greatest among all its descendant nodes and the same thing must be done for its left and right sub-tree also.
Min-Heap
The value of the root node must be the smallest among all its descendant nodes and the same thing must be done for its left and right sub-tree also.
Heap Operations Implementation in C
Insertion - O(log n) Time
- Add the new element at the end of the heap (next available position).
- Perform heapify-up (bubble up) to restore the heap property:
- Min-Heap: Swap with parent if smaller.
- Max-Heap: Swap with parent if larger.
Deletion - O(log n) Time
- Replace the root with the last element in the heap.
- Remove the last element.
- Perform heapify-down (bubble down) to restore heap property:
- Min-Heap: Swap with smaller child.
- Max-Heap: Swap with larger child.
Heapify
- Adjusts a subtree to maintain heap property.
- Used during insertion, deletion, and heap construction.
Applications of Heap :

Advantage of Heap:
