Skip to main content

Detailed Time Complexity of Heap Operations

Heap Operations and Their Time Complexities​

A binary heap is a complete binary tree often used to implement priority queues. It can be a min-heap (parent nodes are smaller than their children) or a max-heap (parent nodes are larger). Binary heaps support several key operations, such as insertion, deletion, and more. Below is a breakdown of these operations, their time complexities, and detailed explanations.

1. Insertion (Insert a New Element)​

  • Time Complexity: O(log n)

  • Explanation:
    Inserting an element into a heap involves two steps:

    1. The new element is placed at the end of the heap.
    2. The element is then "bubbled up" to its correct position to restore the heap property.

    Since the heap is a complete binary tree, its height is proportional to log n. Thus, bubbling up the element takes at most log n comparisons and swaps.

  • Example:
    Consider inserting the number 2 into a min-heap:

          3
    / \
    5 7
    / \
    8 9

    After insertion:

          3
    / \
    5 7
    / \ \
    8 9 2

    The element 2 is smaller than its parent 7, so it bubbles up:

          3
    / \
    5 2
    / \ \
    8 9 7

2. Deletion (Extracting the Root)​

  • Time Complexity: O(log n)

  • Explanation:
    Deleting the root (the smallest or largest element in a min-heap or max-heap) is one of the key operations. It involves:

    1. Replacing the root with the last element in the heap.
    2. "Bubbling down" the new root to maintain the heap property.

    Bubbling down requires comparing and swapping the new root with its children to ensure the heap remains valid. This process can take up to log n steps due to the height of the heap.

  • Example:
    Consider the following max-heap before and after extracting the root (value 9):

          9
    / \
    7 8
    / \
    6 4

    After extracting the root and replacing it with the last element 4:

          4
    / \
    7 8
    /
    6

    The element 4 bubbles down to its correct position:

          8
    / \
    7 4
    /
    6

3. Heapify (Building a Heap from an Array)​

  • Time Complexity: O(n)

  • Explanation:
    Heapifying an unsorted array to form a valid heap is done in linear time, although it may seem like O(n log n). This is because, during heap construction, the number of operations decreases significantly for elements at lower levels of the tree. Elements closer to the root require more comparisons, but there are fewer such elements.

  • Example:
    For an array [4, 10, 3, 5, 1], the heapify process builds the following min-heap:

          1
    / \
    5 3
    / \
    10 4

4. Peek (Getting the Root)​

  • Time Complexity: O(1)

  • Explanation:
    The root of the heap (either the minimum or maximum element) can be accessed in constant time because it is always at the top of the heap.

  • Example:
    In the min-heap:

          1
    / \
    5 3
    / \
    10 4

    Accessing the root (1) takes constant time.

5. Decrease Key (For Priority Queues)​

  • Time Complexity: O(log n)

  • Explanation:
    In priority queues, we may need to decrease the key value of a particular element. This involves:

    1. Reducing the key value.
    2. "Bubbling up" the element to restore the heap property.

    Since bubbling up takes O(log n) time, decreasing a key takes logarithmic time as well.

  • Example:
    If we decrease the key value of the node 10 in a min-heap to 2, it may need to bubble up to maintain the heap property:

          1
    / \
    5 3
    / \
    2 4

6. Delete a Key (Removing an Arbitrary Element)​

  • Time Complexity: O(log n)

  • Explanation:
    To delete a specific element from the heap:

    1. Replace the element with the last node in the heap.
    2. Restore the heap property by either bubbling up or bubbling down.

    Both of these operations take O(log n) time because they depend on the height of the heap.

  • Example:
    Consider deleting the element 5 from the min-heap:

          1
    / \
    5 3
    / \
    10 4

    After replacing 5 with the last element 4:

          1
    / \
    4 3
    /
    10

    The heap is already valid, so no further adjustments are needed.


Summary Table of Heap Operation Time Complexities​

OperationTime Complexity
InsertionO(log n)
Deletion (Extract Root)O(log n)
Heapify (Build a Heap)O(n)
Peek (Get Root)O(1)
Decrease KeyO(log n)
Delete a KeyO(log n)

Explanation of Time Complexities​

Why is Heapify O(n)?​

Heapify has a time complexity of O(n) because elements at the lowest levels of the tree require fewer comparisons. If you think about the number of operations needed for nodes at different levels of the tree:

  • For nodes at the bottom level, no swaps are needed.
  • For nodes at the next level up, only one comparison and swap are needed.
  • For nodes higher up, more swaps are required, but there are fewer of these nodes.

The sum of these operations across the entire tree results in a linear time complexity of O(n).

Why is Insertion and Deletion O(log n)?​

Both insertion and deletion involve restructuring the heap by bubbling elements up or down. Since the heap is a complete binary tree, its height is log n, meaning that in the worst case, you only need to perform log n swaps.

This logarithmic behavior makes heaps extremely efficient for use cases like priority queues, where fast insertions and deletions are crucial.


Understanding the time complexities of heap operations is fundamental when implementing or optimizing algorithms involving heaps, such as scheduling algorithms, Dijkstra's shortest path, or priority queues. The balance between constant-time root access and logarithmic-time inserts/deletes makes heaps ideal for many real-world applications.