Skip to content

Commit 6efed29

Browse files
committed
feat: A min heap is a complete binary tree where every parent node is smaller than its children.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 17c26fc commit 6efed29

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
654 KB
Loading
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Time & Space complexities for Min Heap:
2+
3+
- Peek (get minimum): O(1) — The smallest element is always at the root of the heap.
4+
- Insert: O(log n) — The new element is added at the end and then bubbled up to maintain the heap property.
5+
6+
- Remove smallest (extract-min): O(log n) — The root (minimum element) is removed, the last element
7+
is moved to the root, and then bubbled down to restore order.
8+
9+
- BuildHeap (Floyd’s algorithm): O(n) — Converts an unsorted array into a valid min heap efficiently using bottom-up
10+
heapify.
11+
12+
- Clear heap: O(1) — Resetting or reinitializing the heap reference (or O(n) if you explicitly clear memory).
13+
- Decrease-key / increase-key (if supported): O(log n) — Adjusts the position of a modified key to maintain heap order.
14+
- Space complexity: O(n) — The heap is typically stored in an array with one slot per element.
15+
16+
17+
Quick intuition:
18+
A min heap is a complete binary tree where every parent node is smaller than its children.
19+
The height of the heap is Θ(log n), so any operation that travels up or down the tree costs O(log n).
20+
Building the heap from scratch is linear because nodes near the bottom require less work to heapify.

0 commit comments

Comments
 (0)