Computer ScienceData StructuresMedium

Heap (data structure)

Also known as:Binary heapPriority heap

A heap is a specialized tree-based data structure that satisfies the heap property: in a max-heap, each parent node is greater than or equal to its children; in a min-heap, each parent is less than or equal to its children. Heaps are typically implemented as complete binary trees stored in arrays, enabling O(1) access to the minimum or maximum element. They are the underlying structure of priority queues and are used in heap sort and Dijkstra's shortest path algorithm.

Heap Operation Complexities

OperationMin-HeapMax-HeapNotes
Find Min/MaxO(1)O(1)Always at root
InsertO(log n)O(log n)Sift up after insert
Delete Min/MaxO(log n)O(log n)Sift down after removal
Heapify (build)O(n)O(n)Bottom-up construction
Merge Two HeapsO(n)O(n)Fibonacci heap: O(1)

Interactive Tools

Visualgo — Heap Visualization

Open Tool

CS USF — Heap Visualization

Open Tool

Brilliant.org — Heaps

Open Tool
A max-heap binary tree where each parent node is greater than its children

Wikimedia Commons, CC BY-SA

Related Terms

The term "heap" in this context was coined by J. W. J. Williams in 1964 when he introduced heap sort. The word "heap" reflects the informal notion of a pile where the largest (or smallest) item is always on top and accessible.

heapprioritytreesortingdata-structurealgorithms