File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
Section 25 Collections Frameworks/Queue Interface/Priority Queue/src Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments