Speed:
Min Heap
Insert: O(log n)Extract Min: O(log n)Peek: O(1)
🔻How Min Heap Works
A Min Heap is a complete binary tree where every parent node is less than or equal to its children. The minimum element is always at the root, making it perfect for priority queues and efficiently finding the smallest element.
Key properties of Min Heap:
- Heap Property: Parent ≤ children for all nodes
- Complete Tree: All levels filled except possibly the last
- Array Representation: Parent at index i, children at 2i+1 and 2i+2
- Root Minimum: Smallest element always at index 0
- Efficient Operations: Insert and extract in O(log n) time
💻Implementation
📊Operations Analysis
Time Complexity
- • Insert: O(log n) - heapify up
- • Extract Min: O(log n) - heapify down
- • Peek: O(1) - just return root
- • Build Heap: O(n) from array
Space Complexity
- • Storage: O(n) for n elements
- • Array-based: No extra pointer overhead
- • In-place: Operations use O(1) extra space
- • Cache-friendly: Good memory locality
🎯 Applications
- • Priority Queues: Process scheduling, event simulation
- • Dijkstra's Algorithm: Shortest path finding
- • Huffman Coding: Data compression algorithms
- • A* Search: Pathfinding in games and robotics
- • Median Finding: Using two heaps (min + max)
âš¡ Advantages
- • Efficient min access: O(1) to find minimum
- • Logarithmic operations: Insert/delete in O(log n)
- • Memory efficient: Array-based implementation
- • Simple structure: Easy to implement and understand
- • Cache performance: Good spatial locality