Heap / Priority Queue
A specialized tree-based data structure that satisfies the heap property, commonly used to implement priority queues.
Heap Visualizer
Select an operation to begin.
Array Representation
Heap is empty
Tree Representation
Types / Variants
Min-Heap
Max-Heap
Binary Heap
Complete Binary Tree
Priority Queue (Heap-based)
Min-Heap
The value of each node is less than or equal to the value of its children. The smallest element is always at the root.
Complexity Analysis
| Operation | Time Complexity |
|---|---|
| Find Min/Max | O(1) |
| Insert | O(log n) |
| Delete Min/Max | O(log n) |
| Build Heap | O(n) |
When to Use Heaps
- Implementing priority queues for tasks, events, or pathfinding (e.g., Dijkstra's algorithm).
- Finding the k-th smallest or largest element in a collection.
- Heap Sort algorithm, which provides O(n log n) time complexity.
- Any scenario where you need quick access to the minimum or maximum element.