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.

1020154030

Complexity Analysis

OperationTime Complexity
Find Min/MaxO(1)
InsertO(log n)
Delete Min/MaxO(log n)
Build HeapO(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.