-
Notifications
You must be signed in to change notification settings - Fork 0
MinHeap
krishanthan4 edited this page Dec 16, 2025
·
1 revision
Implement a Min Heap data structure where the parent node is always smaller than or equal to its children, and the minimum element is always at the root.
A min heap is a complete binary tree where each parent node has a value less than or equal to its children. The minimum element is always at the root.
-
Time Complexity:
- Insert: O(log n)
- Extract Min: O(log n)
- Peek: O(1)
- Build Heap: O(n)
- Space Complexity: O(n)
- Heap Property: parent ≤ children
- Structure: Complete binary tree
For every node i:
-
heap[i] ≤ heap[2*i + 1](left child) -
heap[i] ≤ heap[2*i + 2](right child)
- insert(val): Add element maintaining heap property
- extractMin(): Remove and return minimum (root)
- peek(): View minimum without removing
- heapifyUp(): Restore heap property upward
- heapifyDown(): Restore heap property downward
Implement the following methods in MinHeap.java:
-
insert(int val)- Insert element -
extractMin()- Remove and return minimum -
peek()- Return minimum without removing -
heapifyUp(int index)- Fix heap property upward -
heapifyDown(int index)- Fix heap property downward
For a node at index i:
-
Parent:
(i - 1) / 2 -
Left child:
2 * i + 1 -
Right child:
2 * i + 2
Array: [1, 3, 5, 7, 9, 11]
Tree:
1
/ \
3 5
/ \ /
7 9 11
- Add element at the end (to maintain complete tree)
- Heapify up: compare with parent
- If smaller than parent, swap
- Repeat until heap property is satisfied
- Save root value (minimum)
- Move last element to root
- Heapify down: compare with children
- Swap with smaller child if necessary
- Repeat until heap property is satisfied
- Insert multiple elements
- Extract from heap with one element
- Extract all elements (should come out sorted)
- Peek without extracting
- Extract from empty heap (should throw exception)
- Insert and extract mixed operations
- Use ArrayList for dynamic sizing
- Always add new elements at the end
- For heapifyUp: compare with parent and swap if needed
- For heapifyDown: compare with both children, swap with smaller
- Handle edge cases: empty heap, single element
- Priority Queue implementation
- Dijkstra's shortest path algorithm
- Huffman coding
- Heap sort
- Finding k smallest elements
- Median maintenance
- Event-driven simulation
- Find k smallest elements in array
- Merge k sorted lists
- Find median in stream
- Top K frequent elements
- Task scheduler
- How would you convert a max heap to min heap?
- How would you find the kth smallest element using a heap?
- How would you implement heap sort using this heap?
- What's the difference between a heap and a binary search tree?
- Incorrect index calculations (parent/child formulas)
- Not handling empty heap errors
- Comparing with wrong child in heapifyDown
- Off-by-one errors
- Not maintaining complete tree property
| Property | Min Heap | Max Heap |
|---|---|---|
| Root | Minimum | Maximum |
| Parent-Child | parent ≤ child | parent ≥ child |
| Use Case | Find minimum | Find maximum |
Two approaches:
- Insert one by one: O(n log n)
- Heapify bottom-up: O(n) - more efficient!
- Empty heap operations
- Single element heap
- Heap with duplicate values
- Large dataset performance
- Max Heap
- Priority Queue
- Binary Search Tree
- Binomial Heap
- Fibonacci Heap