Imagine if you have been tasked to sort a list of people based on some criteria or priority; how would you go about it? Doing so manually without a proper approach can take you a lot of time and might not even result in accurate sorting. Now take that list of people to be a massive dataset of numbers and values. Instinctively, you will not manually do the dirty work but seek a visual data structure where you can study the criteria/priority relationship between data points. Binary heaps, which are tree-like data structures, are the most appropriate data structures to accomplish this. Wondering what it would look like? Read to know more about this interesting data structure, including what is a binary heap and how it represents relationships between a node and its (at most) two children.
A binary heap is a data structure used to store and visualize data as a binary tree. The word “binary” here implies that each node (other than the root/starting node) of this tree has at most two children. Secondly, the key (or value) associated with each node ‘x’ should be greater than or equal to the keys (or values) of the children.
Here’s an example to help you understand better. Imagine a hospital’s emergency room and a queue of patients. The priority queue can be represented as a binary heap in the following manner.
Binary heaps are represented using arrays. Arrays are containers that store elements in a specific, pre-determined order that satisfies the property. For instance, an ascending array will store values in an ascending (increasing) order.
As heap elements are stored in an array, you’ll need an index. The root or the starting value is stored at the 1st position. For any other element ‘i’ in the array,
Here, let’s say that you want to compute the position of G. As per the array, G is at the 5th position. To know its child’s position, use 2*i. That’ll give you J as the 10th element.
A binary heap in data structure has certain properties.
Binary heaps should be “complete” to satisfy the shape property. Being complete implies that all levels (nodes) of the tree are filled, leaving the last one aside, which is filled from left to right.
The heap order property states that binary heaps could be of two primary types: max and min. It ensures that the highest (or lowest) priority element is always at the root of the heap, making it efficient for priority queue operations.
There are two types of binary heaps: a min-heap and a max-heap.
In a minimum binary heap structure, for every node ‘x’ (besides the root), the key (value) stored in children is less than or equal to the key (values) of x. In other words, the minimum element is always at the root, and the value of each parent node is less than or equal to the values of its children.
As you can see in the image above, the key (value) of the root is less than the keys of its children.
Contrary to a min-heap, for every node ‘x’ (besides the root), the key (value) stored in it should be less than or equal to the keys (values) stored in its children. Simply put, the maximum element is always at the root, and the value of each parent node is greater than or equal to the values of its children.
As you can see here, the key (value) of the root is greater than the keys of its children.
Crtieria | Binary Min Heap | Binary Max Heap |
Definition | For any node ‘x,’ the key (value) in ‘x’ is less than or equal to the keys (values) in its children. | For any node ‘x,’ the key (value) in ‘x’ is greater than or equal to the keys (values) in its children. |
Root Key (or Value) | Minimum | Maximum |
Sorting | Ascending | Descending |
Insertion | A new element to be inserted is placed at the appropriate position to maintain the min-heap property. | The new element is placed at the appropriate position without disturbing the max-heap property. |
Deletion | The minimum value (root value) is removed and the last element in the heap replaces it. The entire heap is then accommodated to satisfy the min-heap property. | The maximum value (root value) is deleted. The last element of the heap replaces it, and the heap is finally adjusted to satisfy the max-heap property. |
Application | Used where the minimum is to be assessed. | Used where the maximum is to be assessed. |
Binary heaps are widely used in sorting-based applications, assessing the minimum/maximum and prioritization. As a result, there is a whole set of operations that you can do with binary heap arrays. The most commonly used heap operations are mentioned below.
The getMin() operation returns the minimum value (root element) of a binary min heap. Contrarily, getMax() returns the maximum value (root) of a binary max heap.
You can insert new elements using the insert() operation. The new element is generally inserted at the bottom right of the heap to maintain the complete tree property. The order property is then restored by appropriate swapping after comparing the newly inserted value with its parent.
This operation deletes the root (min in min-heap and max in max-heap). Then the last element of the heap is moved to the root position, and the entire heap is adjusted to satisfy the required order property. The adjustment is done by comparing the new root value with its children’s value.
The extractMin() command is used to remove the minimum element (root) from a binary min heap. Contrarily, the extractMax() command is used to remove the maximum (root) from a binary max heap.
Here are some of the common ways to implement Binary Heap:
The most efficient way to represent binary heaps is via arrays. For a binary heap represented as an array, the following properties hold:
If you have an array and wish to convert it into a valid binary heap, you have to use heapify algorithms. It is an essential step in building a heap or restoring the heap property after an operation like insertion or deletion. There are two primary variations of heapify algorithms (also called heap sort): sift-down and sift-up.
Insertion
Illustration
Max Heap: [9,8,6,7,5,4,2,3,1] Step 1: Add a new element, say 14. [9, 8, 6, 7, 5, 4, 2, 3, 1, 14] Step 2: Compare 14 with the parents (1), and swap if necessary. [9, 8, 6, 7, 5, 4, 2, 3, 14, 1] Step 3: Keep repeating till the property is satisfied. The output here will be [14, 9, 8, 6, 7, 5, 4, 2, 3, 15, 1] |
Deletion
Illustration
Original Max Heap: [9, 8, 6, 7, 5, 4, 2, 3, 1] Step 1: Replace the root (9) with the last element (2) [2, 8, 6, 7, 5, 4, 2, 3, 1] Step 2: Remove the last element (2) [2, 8, 6, 7, 5, 4, 2, 3] Step 3: Compare the new root (2) with its larger child (8) and swap if necessary [8, 2, 6, 7, 5, 4, 2, 3] Repeat till the heap property is restored and you get: [8, 6, 7, 5, 4, 2, 3, 1] |
In this section; we’ll explore the time complexity of binary heap operations. Different operations take different durations of time to be performed.
On average, the time complexity is O(log n), where n is the number of elements in the heap. This is because we’ll keep checking till the parent-child values satisfy the condition, and this could take as many as log n checks and shifts.
However, in case an element is to be sifted up the entire binary heap, then the duration will be proportional to (log n).
Generally, it would be O(log n), where n is the number of elements in the heap. The complexity starts from the best case at 1 and then increases at 1,2,3,4… till the max complexity of log n is attained.
However, in cases where the elements are sifted down the entire length of the heap, the time complexity would be proportional to log n.
Unlink insertion and deletion, heapify operations sift down every node except the leaves. Simply put, the operation does not necessarily start from the root. Let’s calculate the time complexity of the sift-down heapify operation to have a better understanding.
This can be simplified to:
Binary heaps hold significant importance as they make the implementation of sorting and prioritizing algorithms more efficient. Here are some standard applications.
One of the most widely used applications of binary heaps is in priority queues— queues where elements have associated priorities and need to be processed based on their priority. These queues are used to schedule tasks, event simulations, graph algorithms like Dijkstra and Prim’s, etc.
Heap sort is an efficient sorting algorithm that utilizes binary heaps. This algorithm constructs a max-heap (ascending), and min-heap (descending) using the input array and then extracts the max (min), respectively. The final output is a sorted array. Heap is often used in scenarios where in-place sorting with a guaranteed worst-case performance (where elements are to be sifted through the entire length of the heap) is required.
By now, you must have learned what a binary heap is. Let’s talk about a binary tree—a tree data structure with at most 2 children at each node. A binary heap and a binary tree might sound near-similar, but there are some differences.
Criteria | Binary Heap | Binary Search Tree |
Structure | A binary tree with heap and ordering properties. | A tree data structure with at most 2 children at each node. |
Order | May not be ordered/partially ordered. | Completely ordered. |
Duplication | Allows duplicates | Does not allow duplicates. |
Insertion or Deletion | O(log n) | O(n) |
Operations | Efficient for insertion, deletion, and retrieval of the minimum (or maximum) element | Efficient for searching, insertion, deletion, and traversal |
Common Applications | Priority queues, heap sort, event-driven simulations | Efficient searching, ordered data storage, dictionary, symbol tables |
Binary heaps are used in ample ways in the real world. Here are some examples.
D-ary heaps are an advanced variation of binary heaps where each internal node can have up to ‘D’ children instead of only (or at most) two. They offer better cache performance and reduced tree height compared to binary heaps, especially for large D values.
Fibonacci heaps are a kind of heap data structure that is more efficient and offers more advanced operations like decreasing a key or merging keys. They have significantly reduced the constant time complexity for these operations, making them suitable for certain algorithms like Dijkstra’s algorithm.
We’re sure that you must have learned quite a bit about binary heaps and their vitality in sorting algorithms, as well as in the real world. These robust data structures are highly efficient in managing, sorting, and prioritizing data. Whether you’re using a priority queue or arranging elements in an ascending/descending order— heap vs binary tree provide a scalable solution. To explore more intricacies of these data structures and learn more about other similar ones, Analytics Vidhya is an excellent choice. Analytics Vidhya is a comprehensive online platform that offers verified educational content, tutorials, and articles, and courses on various topics related to data science, algorithms, and programming. Not only this, with courses like the AI and ML Blackbelt Program, AV teaches you how these modern-day technologies are aiding standard data operations. So without further ado, head over to the website.
A. A binary heap is a hierarchical, tree data structure, whereas a stack is a linear data structure that provides static memory allocation for temporary variables.
Binary Tree vs. Binary Heap: Binary trees have arbitrary node arrangements, while binary heaps follow specific rules (max/min heap). Binary heaps are used for priority queues/sorting; trees have diverse applications.
Heap and Types: A heap is a tree-based structure where each node’s value satisfies a specific order. Two types are max heap (highest value at root) and min heap (lowest value at root).
Binary Heap vs. D-heap: Binary heap: complete binary tree, 2 children per node. D-heap: generalized, allows up to d children per node (d > 2). Differences affect operation complexity and application suitability.