Fenwick Trees (Binary Indexed Trees)
Core Implementation
Basic Fenwick Tree
Implementation Variations
1. 2D Fenwick Tree
2. Range Update Point Query Fenwick Tree
3. Order Statistic Tree
Time Complexities
Operation | Basic | 2D | Range Update | Order Statistic |
---|---|---|---|---|
Point Update | O(log n) | O(log n * log m) | O(log n) | O(log n) |
Range Update | N/A | N/A | O(log n) | N/A |
Point Query | O(log n) | O(log n * log m) | O(log n) | O(log n) |
Range Query | O(log n) | O(log n * log m) | N/A | O(log n) |
Find Kth | N/A | N/A | N/A | O(log n) |
Space Complexities
Implementation | Space Complexity | Additional Notes |
---|---|---|
Basic | O(n) | Single array |
2D | O(n*m) | Matrix representation |
Range Update | O(n) | Two arrays |
Order Statistic | O(n) | Single array |
Common Data Structure Patterns
-
Range Query Operations
- Prefix sums
- Range sums
- Cumulative frequencies
-
Dynamic Updates
- Point updates
- Range updates
- Frequency counting
-
Order Statistics
- Kth smallest element
- Rank queries
- Frequency queries
-
Multi-dimensional Queries
- 2D range sums
- Rectangle queries
- Matrix updates
-
Optimization Problems
- Inversions counting
- Range minimum queries
- Dynamic rank queries
-
Frequency Tables
- Count occurrences
- Dynamic histograms
- Frequency tracking
Edge Cases to Consider
- Empty Tree
- Single Element
- Range Boundaries
Optimization Techniques
- Bit Operations
- Bulk Updates
Memory Management
- Sparse Representation
- Memory-Efficient Implementation
Performance Considerations
- Cache-Friendly Implementation
- Parallel Updates
Common Pitfalls
- Index Handling
- Range Updates
- Overflow Handling