A heap is a complete binary tree that follows the max heap or min heap property to organize data efficiently. Learn how heap insertion, deletion, heapify operations, and array representation work with step-by-step examples. Explore heap sort, time complexity, and real-world applications of heaps in priority queues, operating systems, and graph algorithms.
A Heap is a special type of binary tree that satisfies the following two properties:
a. Structural Property: It must be a Complete Binary Tree (CBT), in which all levels are completely filled except possibly the last level, which is filled from left to right.
b. Ordering Property: It must satisfy either the Max Heap property or the Min Heap property.
Before exploring heap operations, let us first understand the concept of a Complete Binary Tree, as it forms the structural foundation of a heap.
A Complete Binary Tree is a binary tree in which all levels are completely filled except possibly the last level. The last level is filled from left to right, without leaving any gaps between nodes.
For example:
A complete binary tree
Note: A complete binary tree alone is not necessarily a heap. It must satisfy both the structural property and the ordering property.
In a max heap, every parent node is greater than or equal to its children. Therefore, the largest element is always at the root.
Example:
Max heap
Here, 50 is the largest element and is placed at the root.
In a min heap, every parent node is less than or equal to its children. Therefore, the smallest element is always at the root.
Example:
Min heap
Here, 5 is the smallest element and is placed at the root.
A heap can be represented efficiently using an array for the parent-child relationships.
Example: A = {50, 45, 40, 20, 25, 35, 30, 10, 15}
Array representation:
For a heap stored using a zero-based array, these formulas help us navigate the heap:
(i - 1) / 22 * i + 12 * i + 2Integer division is used for the parent formula.
Example 1: Find the children of 45
Example 2: Find the parent of 35
Example 3: Find the children of 20
Important note: The parent formula applies only to non-root nodes. The root at index 0 has no parent.
When a new element is inserted, it is first placed at the next available position to preserve the complete binary tree structure.
Then, it is compared with its parent. If the new element is greater than its parent, the two elements are swapped. This process continues until the heap property is restored.
This process is called heapify up or sift up.
Example: Insert 60 into the following max heap.
Before inserting 60:
Step 1: Add 60 at the next available position.
Step 2: Compare 60 with its parent node i.e. 25. It is violating the property of max heap, swap them.
Step 3: Again compare 60 with its parent node i.e. 45, child is greater than parent, swap them.
Step 4: Again compare 60 with its parent node i.e. 50, child is greater than parent, swap them.
When an element is deleted from a max heap, the root element (the maximum element) is usually removed first.
The last element in the heap is moved to the root position to maintain the complete binary tree structure. Then, it is compared with its children. If the element is smaller than its larger child, the two elements are swapped. This process continues until the heap property is restored.
This process is called heapify down or sift down.
Note: In a max heap, the delete-max operation removes the root element, which contains the maximum value. In a min heap, the delete-min operation removes the root element, which contains the minimum value.
Steps to delete an element from max heap
Step 1: Remove the root element i.e. 50.
Step 2: Move the last element to the root, and reduce the size of heap by 1.
Step 3: Compare 15 with its children (45 and 40). Since 15 is smaller than its larger child i.e. 45, swap 15 and 45.
Step 4: Compare 15 with its children (20 and 25). The larger child is 25. Swap 15 and 25. Now 15 has no children, so the heap property is restored.
| Operation | Time Complexity |
|---|---|
| Access maximum in a max heap | O(1) |
| Insert an element | O(log n) |
| Delete the root | O(log n) |
| Build a heap | O(n) |
| Heap sort | O(n log n) |
| Search for an element | O(n) |
Heap Sort is a comparison-based sorting algorithm that uses a heap data structure to arrange elements in ascending or descending order.
To sort elements in ascending order, Max Heap is used, and to sort elements in descending order, Min Heap is used.
Heap trees are used in many computer science applications:
| Feature | Heap Tree | Binary Search Tree |
|---|---|---|
| Structure | Complete binary tree | Not necessarily complete |
| Ordering | Parent-child heap property | Left subtree values are smaller and right subtree values are larger, under the usual distinct-key rule |
| Root | Maximum in max heap; minimum in min heap | No guaranteed minimum or maximum at root |
| Searching | Usually O(n) | Average O(log n) if reasonably balanced; worst case O(n) |
| Main applications | Priority queues and heap sort | Searching, insertion, and deletion |
The Heap Data Structure is an important non-linear data structure that helps efficiently manage and retrieve the maximum or minimum element. By satisfying the structural property of a Complete Binary Tree and the ordering property of a Max Heap or Min Heap, it supports efficient operations such as insertion and deletion.
Understanding heap constructions, array representation, heapify operations, and Heap Sort provides a strong foundation for learning advanced data structures and algorithms. Heaps are widely used in priority queues, scheduling systems, and graph algorithms, making them an essential concept in computer science.
A Max Heap maintains the largest element at the root, where every parent is greater than or equal to its children. A Min Heap maintains the smallest element at the root, where every parent is less than or equal to its children.
Both insertion and deletion of the root element typically take (O(log n)) time because the heap may need to be adjusted across its height. Accessing the root element takes (O(1)) time.
Heaps are commonly used to implement priority queues, perform heap sort, support task scheduling in operating systems, and optimize graph algorithms such as Dijkstra’s shortest-path algorithm and Prim’s minimum spanning tree algorithm.
Indian Institute of Embedded Systems – IIES