Heap (data structure)
Tree-based data structure satisfying the heap property.
A heap is a tree-based data structure that satisfies the heap property: in a max heap, the parent node's key is greater than or equal to its child's key; in a min heap, the parent's key is less than or equal to its child's key. The node at the top is called the root. Heaps are a maximally efficient implementation of a priority queue, and priority queues are often referred to as 'heaps' regardless of implementation. Heaps are not sorted structures but are partially ordered, useful for repeatedly removing the highest or lowest priority element or interspersing insertions with root removals.
- field
- Computer science
- known_for
- Heap data structure, binary heap, heapsort algorithm
Lore & Background
The heap data structure, specifically the binary heap, was introduced by J. W. J. A common implementation is the binary heap, where the tree is a complete binary tree. Heaps are typically constructed in-place in the same array where elements are stored, with structure implicit in the access pattern of operations, requiring no additional memory beyond that used for storing keys. The heap has the smallest possible height when a complete binary tree: a heap with N nodes and a branches per node always has log_a N height.
Reader's Guide
Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm and Prim's minimal-spanning-tree algorithm, reducing run time by polynomial order. They are used in heapsort, one of the best sorting methods being in-place and with no quadratic worst-case scenarios. Heaps enable selection algorithms, allowing access to the smallest or largest element in constant time, and finding the k-smallest element in O(k) time. They are also useful for K-way merge operations, merging many already-sorted input streams into a single sorted output stream. The C++ Standard Library provides make_heap, push_heap, and pop_heap algorithms for heaps, usually implemented as binary heaps, operating on arbitrary random access iterators. The heap data structure has many applications including priority queues, external sorting, and streaming results from distributed data such as a log structured merge tree.
Did You Know?
- In a heap, the highest or lowest priority element is always stored at the root.
- Heaps are typically constructed in-place in the same array where elements are stored, requiring no additional memory beyond that used for storing keys.
- A heap with N nodes and a branches for each node always has log_a N height.
More in Algorithms And Data Structures 1-24
Elsewhere in the Algorithms And Data Structures universe
Spotted an error? Know more?
This is a living reference — every entry is fact-audited, and reader corrections feed straight into our audit queue. Suggest an edit · See this site's audit record
