The Heap Data Structure
The Heap Data Structure
Alexander Rother
Introduction to heaps
The heap data structure is organized in a tree data structure meaning, the heap has a root node, parent
nodes, and child nodes.[1] The most common implantation of a heap is a binary heap. Unlike a Binary Search
Tree (BST), heaps are not necessarily sorted structures.[5] However, in a binary heap the root node is always
the lowest or highest priority element. This type of heap has a root that is the minimum (min) or maximum
(max) value, and the root’s child nodes must be larger or smaller than the root node respectively.[2][3][4]
The height of a heap is the number of parent nodes of the farthest node from the root. A complete
binary heap is as small as physically possible. As all parent nodes have the maximum number of child nodes.
This is more efficient for searching than in a BST because the height of the tree can vary greatly.
Figure 1. Minimum Binary Heap, made with University of San Francisco heap visualization tool from: https://www.cs.usfca.edu/~galles/visualization/Heap.html
The above figure visualizes a minimum binary heap. The root node is the smallest binary element. For
this figure the root is 0001 which in base10 represents 20 or 1. The rule for a minimum binary heap is that
all the root’s child nodes are smaller than the root element.[2] If appending a value smaller than the root
3
value to a minimum binary heap the root value becomes the new minimum. The old value is appended as
In figure 1 the height of the above heap is 4. You can find the height of a heap in an algorithm by
counting the number of leftmost children starting from the root. When you reach the end of the tree you
A heap isn’t ordered in the same way as a binary search tree. The heap is neatly sorted so every parent
has at least two children, because of the nature of heaps it’s hard to say how many children a parent will
have. The most basic implementation of a heap has two child nodes for each parent, but a parent node
could have any number of child nodes. It's important to note that heap sort is not a stable sorting algorithm
which means that the output order is not necessarily the same order that it was inputted.
The neatly sorted heap is much faster to search through than a BST. Cousin nodes are nodes with the
same parent node, a BST could have a parent node with many children and grandchildren while it’s cousin
node could have no children at all. This makes it possible for a BST to be much more inefficient to search
Types of heaps
The most common implementation of a heap is the binary heap. There are other lesser-known
implementations of the heap data structure such as the Binomial, Fibonacci, or Leftist Heap. Like any data
structure there are many ways the underlying principles can be implemented in an algorithm.
Binary heaps can be sorted. In a minimum/maximum binary heap the smallest/largest element is the
root element. When adding a smaller/larger element to the min/max binary heap the new element
becomes the root value and the previous root value is appended to an available parent node.
4
Heap sort
Figure 2. Sorted maximum Binary Heap, made with VisuAlgo.net’s heap visualization tool from: https://visualgo.net/en/heap
An iterative heap sort builds a max heap, which is a heap with each parent node having the max amount
of child nodes.[7] Starting with the children of the root the child is compared to the parent node. In a
maximum heap if the child is greater than the parent node, they will swap places with the child node
becoming the parent node and vice versa. Moving through the heap every node is compared to their parent
When appending or removing values from a heap the element is compared to the parent node, in a
minimum heap if the child is smaller than the parent the elements will swap places. After swapping the
new parent compares itself to its parent and if it is smaller, they swap places. This process continues until
all elements in the heap are smaller than their parent node.
After using heap sort on a maximum value heap, the final heap is ordered from the maximum
value at the root node and the minimum values being at the bottom of the heap.
Applications of heaps
Heaps are very efficient to sort through compared to BST and are more versatile than arrays as
they do not have a fixed value. Unlike arrays, heaps don’t have to occupy consecutive sections of memory,
instead they have the advantage of taking small portions of memory in any location.
5
“The heap data structure can be used to efficiently find the kth smallest (or largest) element in an
array”[8] Heap data structures are also used for efficient priority queues as you can easily find the max value,
insert new values, and remove existing values “in O(logn) time”[8] which is some of the fastest time
Heap sort compared to merge or quick sort is marginally slower which is the reason why merge
and quick sort are more dominant in applications where the speed of the algorithm is crucial. Heap sort is
“[t]ypically 2-3 times slower than well-implemented QuickSort. The reason for slowness is a lack of
locality of reference.” A “well-implemented QuickSort” can be much faster than heap sort which is why
Time complexity
The time complexity of heap sort is O(nlogn). The best, worst and average case of heap sort is all
O(nlogn) meaning that the algorithm time will always be the same making the speed always consistent.
Compared to some other algorithms like in figure 3 heap sort isn’t as efficient as it cannot compete with a
speed of O(logn).
Conclusion
Despite the drawbacks of a speed of nlogn compared to other algorithms of logn heaps and heap
sort are both still used in real time embedded systems where less space is available as heap sort takes O(1)
Heaps are efficient and provide consistent best, worst, and average times as well as the lowest
possible space complexity of 1. This means that heaps will always reign supreme for small systems like
microcomputers.[9]
7
References
1^ CORMEN, THOMAS H. (2009). INTRODUCTION TO ALGORITHMS. United States of America: The MIT Press Cambridge, Massachusetts
2^ Black (ed.), Paul E. (2004-12-14). Entry for heap in Dictionary of Algorithms and Data Structures. Online version. U.S. National Institute of
3^ Williams, J. W. J. (1964), "Algorithm 232 - Heapsort", Communications of the ACM, 7 (6): 347–348, doi.org/10.1145/512274.512284
4^ Binary heap. GeeksforGeeks. (2022, November 2). Retrieved December 8, 2022, from https://www.geeksforgeeks.org/binary-heap/
5^ Difference between binary search tree and binary heap. GeeksforGeeks. (2021, June 22). Retrieved December 8, 2022, from
https://www.geeksforgeeks.org/difference-between-binary-search-tree-and-binary-heap/
6^ Heap sort. GeeksforGeeks. (2022, September 22). Retrieved December 8, 2022, from https://www.geeksforgeeks.org/heap-sort/
7^ Iterative heapsort. GeeksforGeeks. (2022, July 12). Retrieved December 8, 2022, from https://www.geeksforgeeks.org/iterative-heap-sort/
8^ Applications of heap data structure. GeeksforGeeks. (2022, October 21). Retrieved December 8, 2022, from
https://www.geeksforgeeks.org/applications-of-heap-data-structure/
9^ Woltmann, S. (2022, July 19). Heapsort – algorithm, source code, Time Complexity. HappyCoders.eu. Retrieved December 8, 2022, from
https://www.happycoders.eu/algorithms/heapsort/
10^ Where is heap sort used practically? GeeksforGeeks. (2021, June 28). Retrieved December 8, 2022, from
https://www.geeksforgeeks.org/where-is-heap-sort-used-practically/