heap.definition
heap.descriptionPart1
heap.descriptionPart2
heap.keyPointsTitle
- heap.keyPoint1
- heap.keyPoint2
- heap.keyPoint3
- heap.keyPoint4
heap.exampleTitle
heap.exampleCaption
heap.syntaxTitle
// Java
class MaxHeap {
int[] heap = new int[31];
int size = 0;
void insert(int value) {
heap[size] = value;
int i = size++;
// Bubble up
while (i > 0 && heap[i] > heap[(i-1)/2]) {
swap(i, (i-1)/2);
i = (i-1)/2;
}
}
}# Python
class MaxHeap:
def __init__(self):
self.heap = []
def insert(self, value):
self.heap.append(value)
i = len(self.heap) - 1
# Bubble up
while i > 0 and self.heap[i] > self.heap[(i-1)//2]:
parent = (i-1)//2
self.heap[i], self.heap[parent] = \
self.heap[parent], self.heap[i]
i = parent// C++
class MaxHeap {
vector<int> heap;
public:
void insert(int value) {
heap.push_back(value);
int i = heap.size() - 1;
// Bubble up
while (i > 0 && heap[i] > heap[(i-1)/2]) {
swap(heap[i], heap[(i-1)/2]);
i = (i-1)/2;
}
}
};// JavaScript
class MaxHeap {
constructor() { this.heap = []; }
insert(value) {
this.heap.push(value);
let i = this.heap.length - 1;
// Bubble up
while (i > 0 && this.heap[i] > this.heap[Math.floor((i-1)/2)]) {
const parent = Math.floor((i-1)/2);
[this.heap[i], this.heap[parent]] =
[this.heap[parent], this.heap[i]];
i = parent;
}
}
}Interactive Max Heap Visualization
Array Representation
Heap is empty. Insert values to begin.
Tree Representation
Heap is empty. Insert values to begin.
Note: In a max heap, each parent node has a value greater than or equal to its children. Parent index = floor((i-1)/2), Left child = 2i+1, Right child = 2i+2.
enhanced.realWorldExamples.title
OS schedulers use priority queues (heaps) for CPU allocation.
Why? O(log n) insertion and extraction of highest priority.
GPS apps use heaps in Dijkstra's algorithm for routing.
Why? Efficiently extracts next closest node.
Time Complexity
| Operation | Average Case | Worst Case | Description |
|---|---|---|---|
| Insert | O(log n) | O(log n) | Add to end, then bubble up to restore heap property |
| Extract Max | O(log n) | O(log n) | Remove root, replace with last element, bubble down |
| Find Max | O(1) | O(1) | Return root element (always at index 0) |
| Heapify (Build) | O(n) | O(n) | Build heap from array using bottom-up approach |
| Increase Key | O(log n) | O(log n) | Update value and bubble up if needed |
Heap property: In a max heap, every parent node is greater than or equal to its children.
Array representation: Parent index = floor((i-1)/2), Left child = 2i+1, Right child = 2i+2.
Space complexity: O(n) for storing n elements. Operations use O(1) auxiliary space.
enhanced.complexityAnalysis.title
enhanced.complexityAnalysis.mathematicalTitle
Heap operations involve traversing tree height. A complete binary tree of n nodes has height โlogโ(n)โ, so operations are O(log n).
๐ขBest Case
O(1) for peek/find-min. O(1) for insert when new element is largest (no bubble-up needed).
๐กAverage Case
O(log n) for insert and extract-min/max operations.
๐ดWorst Case
O(log n) for insert (bubble up entire height) and extract (bubble down entire height).
๐พSpace Complexity
O(n) using array representation. No pointer overhead needed.
โกCache Performance
Good cache locality with array-based implementation due to level-order storage.