All Data Structures
Heap
Core Implementation
Binary Heap (Min Heap)
Implementation Variations
1. Max Heap
2. Priority Queue with Custom Objects
3. D-ary Heap
Time Complexities
Operation | Binary Heap | D-ary Heap | Priority Queue |
---|---|---|---|
Build Heap | O(n) | O(n) | O(n) |
Insert | O(log n) | O(logd n) | O(log n) |
Extract Min/Max | O(log n) | O(d logd n) | O(log n) |
Decrease Key | O(log n) | O(logd n) | N/A |
Get Min/Max | O(1) | O(1) | O(1) |
Delete | O(log n) | O(d logd n) | O(log n) |
Merge | O(n) | O(n) | O(n) |
Space Complexities
Implementation | Space Complexity | Additional Notes |
---|---|---|
Binary Heap | O(n) | Complete binary tree structure |
D-ary Heap | O(n) | D children per node |
Priority Queue | O(n) | Additional space for entry objects |
Common Data Structure Patterns
-
Priority Queue Applications
- Task Scheduling
- Event Processing
- Dijkstra’s Algorithm
-
Heap Sort Patterns
- K-way Merge
- Partial Sorting
- Stream Processing
-
Median Finding
- Running Median
- Sliding Window Median
- Two-Heap Pattern
-
K-th Element Problems
- K Largest Elements
- K Closest Points
- Top K Frequent
-
Merge Patterns
- Merge K Sorted Lists
- Merge K Sorted Arrays
- Stream Merging
-
Optimization Problems
- Load Balancing
- Resource Allocation
- Job Scheduling
Edge Cases to Consider
- Empty Heap
- Single Element
- Duplicate Elements
Optimization Techniques
- Array-based Implementation
- Bottom-up Heap Construction
- Index Mapping for Quick Access
Memory Management
- Fixed-size Heap
- Memory-Efficient Implementation
Performance Considerations
- Batch Operations
- Cache-Friendly Implementation
Common Pitfalls
- Incorrect Heap Property Maintenance
- Improper Memory Management
- Index Out of Bounds