This is the project for "Data structures and algorithms". I have used Python for this project whereas no libraries have been used.
To understand 2D Fenwick tree better, you should first understand Fenwick tree.
A Binary Indexed Tree or Fenwick Tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers. We know that to answer range sum queries on a 1-D array efficiently, binary indexed tree (or Fenwick Tree) is the best choice (even better than segment tree due to less memory requirements and a little faster than segment tree).
2D Fenwick tree is one such implementation used to answer sub-matrix queries, i.e. queries in 2 dimensions. Fenwick tree is considered a prerequisite to understand 2D Fenwick tree. Like 1D, 2D Fenwick tree also requires the operation to be invertible.
Since Fenwick tree stores prefix sums, 1D Fenwick tree works by processing query(m, n) as query(1, n) - query(1, m - 1). 2D Fenwick tree operates on a matrix, so query is processed differently, but the requirement is still same, i.e. operation must be invertible.
- Update : O(log2(MN))
- Query : O(log2(MN)) Where the matrix is of size M x N
- Is much more space efficient than 2D Segment tree or Quad tree.
- The queries can be processed in O(log2mn) time.
- Used for finding sub-matrix sum/product etc.
- Cannot be used for sub-matrix min/max.