StudyG Logo
Study G
Concept Breakdowns

Heap Operations Time Complexity

Heap operations — insertion, deletion, and heapify — have specific time complexities that appear frequently in algorithms courses, FAANG interview prep, and competitive programming. Understanding why these bounds hold and how min-heaps and max-heaps differ is essential for priority queue implementations and sorting algorithms like heapsort.

Interactive Deck

5 Cards
1
Front

Heap insert time complexity?

Click to reveal
1
Back

O(log n) — new element added at end, then sifted up (bubbled up) to restore heap property.

2
Front

Heap delete-min/max complexity?

Click to reveal
2
Back

O(log n) — root removed, last element placed at root, then sifted down to restore heap order.

3
Front

What is heapify (build heap) complexity?

Click to reveal
3
Back

O(n) — not O(n log n). Bottom-up heapify is more efficient; most nodes are near the bottom with less sifting needed.

4
Locked

Heap peek (find min/max) complexity?

5
Locked

Min-heap vs max-heap property?

Master this topic effortlessly.

Study G helps you master any topic effortlessly using proven learning algorithms and smart review timing

Download Study G

Frequently Asked Questions

Why is heap insertion O(log n) instead of O(n)?

A heap is a complete binary tree with height O(log n). Insertion adds the element at the bottom and sifts it up at most O(log n) levels, comparing with one parent per level.

Why is build heap O(n) and not O(n log n)?

Bottom-up heapify starts from the last non-leaf node and sifts down. Most nodes are near the leaves with short paths — the total work sums to O(n) by a geometric series argument.

What is the difference between a heap and a BST?

A heap only guarantees parent-child order (root is min or max), enabling O(1) access to the extreme. A BST maintains full sorted order, supporting O(log n) search but O(log n) min/max.

  • Heap: O(1) min/max, no search
  • BST: O(log n) search, O(log n) min/max