A Heap is a specialized tree-based data structure that satisfies two key properties:
- It is a complete binary tree, meaning all levels are completely filled except possibly the last, which is filled from left to right.
- It follows the heap property — in a max heap, every parent node is greater than or equal to all its descendants, while in a min heap, every parent node is less than or equal to all its descendants.
Max-Heap
The value of the root node must be the greatest among all its descendant nodes and the same thing must be done for its left and right sub-tree also.
Min-Heap
The value of the root node must be the smallest among all its descendant nodes and the same thing must be done for its left and right sub-tree also.
Properties of Heap:
- In a heap, the minimum or maximum element is always at the root, allowing constant-time access. For a node at index i (0-based indexing), its left child is at index 2i + 1 and its right child is at index 2i + 2.
- Since a heap is a complete binary tree, all levels are fully filled except possibly the last, which is filled from left to right.
- When inserting an element, it is added at the last available position, and then the nodes are rearranged to maintain the heap property.
- When removing an element, the root is swapped with the last node to remove either the maximum or minimum value. The remaining nodes are then rearranged to ensure the heap property is preserved.
Operations Supported by Heap:
Operations supported by min - heap and max - heap are same. The difference is just that min-heap contains minimum element at root of the tree and max - heap contains maximum element at the root of the tree.
Heapify:
Heapify is the process of rearranging elements to maintain the heap property. It is performed in two main scenarios:
- When the root is removed: The root is replaced with the last node, and then heapify is called to restore the heap property.
- When building a heap: Heapify is applied from the last internal node up to the root to ensure the entire tree satisfies the heap property.
This operation has a time complexity of O(log n).
- In a max-heap, heapify ensures the maximum element is at the root, and all subtrees maintain the same property.
- In a min-heap, it ensures the minimum element is at the root, with all subtrees following the min-heap property.
Insertion:
When a new element is inserted into a heap, it may violate the heap property. To restore the heap structure, a heapify operation is performed, which ensures the heap properties are maintained.
Time complexity - O(log n).
Deletion:
Deleting an element from a heap means removing the root node, replacing it with the last node in the heap, and then performing heapify to restore the heap’s order.
Time complexity - O(log n).
getMax (For max-heap) or getMin (For min-heap):
It finds the maximum element or minimum element for max-heap and min-heap respectively and as we know minimum and maximum elements will always be the root node itself for min-heap and max-heap respectively.
Time Complexity - O(1).
Please refer the following articles for more details about Heap Data Structure
Library Support for Max-Heap and Min-Heap:
Advantage of Heap:
Applications of Heap :
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem