A heap is a special tree-based data structure that satisfies the heap property. It is commonly used to implement priority queues and is highly efficient for operations like finding the minimum or maximum element.
Complete Binary Tree:
Heap Order Property:
Max-Heap:
50
/ \
30 40
/ \ / \
10 20 15 25
Min-Heap:
10
/ \
20 15
/ \ / \
50 30 25 40
Heaps support the following main operations:
To insert an element into a heap:
C++ Implementation:
#include <iostream>
#include <vector>
using namespace std;
class MaxHeap {
vector<int> heap;
void heapifyUp(int index) {
while (index > 0 && heap[index] > heap[(index - 1) / 2]) {
swap(heap[index], heap[(index - 1) / 2]); // Swap with parent
index = (index - 1) / 2; // Move to the parent's index
}
}
public:
void insert(int value) {
heap.push_back(value); // Add at the end
heapifyUp(heap.size() - 1); // Restore heap property
}
void display() {
for (int val : heap) cout << val << " ";
cout << endl;
}
};
int main() {
MaxHeap h;
h.insert(50);
h.insert(30);
h.insert(40);
h.insert(10);
h.insert(20);
h.insert(15);
h.display(); // Output: 50 30 40 10 20 15
return 0;
}
To delete the root element:
C++ Implementation:
class MaxHeap {
vector<int> heap;
void heapifyDown(int index) {
int largest = index;
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
if (leftChild < heap.size() && heap[leftChild] > heap[largest])
largest = leftChild;
if (rightChild < heap.size() && heap[rightChild] > heap[largest])
largest = rightChild;
if (largest != index) {
swap(heap[index], heap[largest]);
heapifyDown(largest); // Recursively heapify the affected subtree
}
}
public:
void insert(int value) {
heap.push_back(value);
heapifyUp(heap.size() - 1);
}
void deleteRoot() {
if (heap.empty()) return;
heap[0] = heap.back(); // Replace root with last element
heap.pop_back(); // Remove last element
heapifyDown(0); // Restore heap property
}
void display() {
for (int val : heap) cout << val << " ";
cout << endl;
}
};
Heapify is the process of rearranging elements to maintain the heap property. It is often used when building a heap from an array.
heapifyDown for each node, moving upwards.Time Complexity:
Heap sort uses a max-heap to sort an array in ascending order:
C++ Implementation:
void heapify(vector<int>& arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) largest = left;
if (right < n && arr[right] > arr[largest]) largest = right;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(vector<int>& arr) {
int n = arr.size();
// Build a max-heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// Extract elements from the heap
for (int i = n - 1; i >= 0; i--) {
swap(arr[0], arr[i]); // Move current root to end
heapify(arr, i, 0); // Restore heap property
}
}
int main() {
vector<int> arr = {50, 30, 40, 10, 20, 15};
heapSort(arr);
for (int val : arr) cout << val << " ";
// Output: 10 15 20 30 40 50
return 0;
}
| Operation | Time Complexity |
|---|---|
| Insertion | |
| Deletion | |
| Build Heap | |
| Heap Sort |
Open this section to load past papers