Let us consider the following problem to understand Binary Indexed Tree.
We have an array arr[0 . . . n-1]. We would like to
1 Compute the sum of the first i-th elements.
2 Modify the value of a specified element of the array arr[i] = x where 0 <= i <= n-1.
A simple solution is to run a loop from 0 to i-1 and calculate the sum of the elements. To update a value, simply do arr[i] = x. The first operation takes O(n) time and the second operation takes O(1) time. Another simple solution is to create an extra array and store the sum of the first i-th elements at the i-th index in this new array. The sum of a given range can now be calculated in O(1) time, but the update operation takes O(n) time now. This works well if there are a large number of query operations but very few number of update operations.
Could we perform both the query and update operations in O(log n) time?
One efficient solution is to use Segment Tree that performs both operations in O(Logn) time.
An alternative solution is Binary Indexed Tree, which also achieves O(Logn) time complexity for both operations. Compared with Segment Tree, Binary Indexed Tree requires less space and is easier to implement..
Binary Indexed Tree is represented as an array. Let the array be BITree. Each node of the Binary Indexed Tree stores the sum of some elements of the input array. The size of the Binary Indexed Tree is equal to the size of the input array, denoted as n. In the code below, we use a size of n+1 for ease of implementation.
We initialize all the values in BITree as 0. Then we call update() for all the indexes, the update() operation is discussed below.
getSum(x): Returns the sum of the sub-array arr[0,...,x] // Returns the sum of the sub-array arr[0,...,x] using BITree[0..n], which is constructed from arr[0..n-1] 1) Initialize the output sum as 0, the current index as x+1. 2) Do following while the current index is greater than 0. ...a) Add BITree[index] to sum ...b) Go to the parent of BITree[index]. The parent can be obtained by removing the last set bit from the current index, i.e., index = index - (index & (-index)) 3) Return sum.
The diagram above provides an example of how getSum() is working. Here are some important observations.
BITree is a dummy node.
BITree[y] is the parent of BITree[x], if and only if y can be obtained by removing the last set bit from the binary representation of x, that is y = x – (x & (-x)).
The child node BITree[x] of the node BITree[y] stores the sum of the elements between y(inclusive) and x(exclusive): arr[y,…,x).
update(x, val): Updates the Binary Indexed Tree (BIT) by performing arr[index] += val // Note that the update(x, val) operation will not change arr. It only makes changes to BITree 1) Initialize the current index as x+1. 2) Do the following while the current index is smaller than or equal to n. ...a) Add the val to BITree[index] ...b) Go to parent of BITree[index]. The parent can be obtained by incrementing the last set bit of the current index, i.e., index = index + (index & (-index))
The update function needs to make sure that all the BITree nodes which contain arr[i] within their ranges being updated. We loop over such nodes in the BITree by repeatedly adding the decimal number corresponding to the last set bit of the current index.
How does Binary Indexed Tree work?
The idea is based on the fact that all positive integers can be represented as the sum of powers of 2. For example 19 can be represented as 16 + 2 + 1. Every node of the BITree stores the sum of n elements where n is a power of 2. For example, in the first diagram above (the diagram for getSum()), the sum of the first 12 elements can be obtained by the sum of the last 4 elements (from 9 to 12) plus the sum of 8 elements (from 1 to 8). The number of set bits in the binary representation of a number n is O(Logn). Therefore, we traverse at-most O(Logn) nodes in both getSum() and update() operations. Time complexity of the construction is O(nLogn) as it calls update() for all n elements.
Following are the implementations of Binary Indexed Tree.
Sum of elements in arr[0..5] is 12 Sum of elements in arr[0..5] after update is 18
Can we extend the Binary Indexed Tree to computing the sum of a range in O(Logn) time?
Yes. rangeSum(l, r) = getSum(r) – getSum(l-1).
The implementation of the arithmetic coding algorithm. The development of the Binary Indexed Tree was primarily motivated by its application in this case. See this for more details.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Two Dimensional Binary Indexed Tree or Fenwick Tree
- Maximum Sum Increasing Subsequence using Binary Indexed Tree
- Binary Indexed Tree : Range Updates and Point Queries
- Order statistic tree using fenwick tree (BIT)
- Binary Indexed Tree : Range Update and Range Queries
- Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
- Tournament Tree (Winner Tree) and Binary Heap
- Find LCA in Binary Tree using RMQ
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Euler tour of Binary Tree
- Flatten a binary tree into linked list | Set-2
- Treap (A Randomized Binary Search Tree)
- Queries to find distance between two nodes of a Binary tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- B-Tree | Set 1 (Introduction)