Binary Indexed Tree or Fenwick Tree
- Difficulty Level : Medium
- Last Updated : 16 Jul, 2022
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 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 a 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..
Representation
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.
Construction
We initialize all the values in BITree[] as 0. Then we call update() for all the indexes, the update() operation is discussed below.
Operations
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[0] 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 next element of BITree[index]. The next element 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. The time complexity of the construction is O(nLogn) as it calls update() for all n elements.
Implementation:
Following are the implementations of Binary Indexed Tree.
C++
// C++ code to demonstrate operations of Binary Index Tree#include <iostream>usingnamespacestd;/* n --> No. of elements present in input array.BITree[0..n] --> Array that represents Binary Indexed Tree.arr[0..n-1] --> Input array for which prefix sum is evaluated. */// Returns sum of arr[0..index]. This function assumes// that the array is preprocessed and partial sums of// array elements are stored in BITree[].intgetSum(intBITree[],intindex){intsum = 0;// Initialize result// index in BITree[] is 1 more than the index in arr[]index = index + 1;// Traverse ancestors of BITree[index]while(index>0){// Add current element of BITree to sumsum += BITree[index];// Move index to parent node in getSum Viewindex -= index & (-index);}returnsum;}// Updates a node in Binary Index Tree (BITree) at given index// in BITree. The given value 'val' is added to BITree[i] and// all of its ancestors in tree.voidupdateBIT(intBITree[],intn,intindex,intval){// index in BITree[] is 1 more than the index in arr[]index = index + 1;// Traverse all ancestors and add 'val'while(index <= n){// Add 'val' to current node of BI TreeBITree[index] += val;// Update index to that of parent in update Viewindex += index & (-index);}}// Constructs and returns a Binary Indexed Tree for given// array of size n.int*constructBITree(intarr[],intn){// Create and initialize BITree[] as 0int*BITree =newint[n+1];for(inti=1; i<=n; i++)BITree[i] = 0;// Store the actual values in BITree[] using update()for(inti=0; i<n; i++)updateBIT(BITree, n, i, arr[i]);// Uncomment below lines to see contents of BITree[]//for (int i=1; i<=n; i++)// cout << BITree[i] << " ";returnBITree;}// Driver program to test above functionsintmain(){intfreq[] = {2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9};intn =sizeof(freq)/sizeof(freq[0]);int*BITree = constructBITree(freq, n);cout <<"Sum of elements in arr[0..5] is "<< getSum(BITree, 5);// Let use test the update operationfreq[3] += 6;updateBIT(BITree, n, 3, 6);//Update BIT for above change in arr[]cout <<"\nSum of elements in arr[0..5] after update is "<< getSum(BITree, 5);return0;}Java
// Java program to demonstrate lazy// propagation in segment treeimportjava.util.*;importjava.lang.*;importjava.io.*;classBinaryIndexedTree{// Max tree sizefinalstaticintMAX =1000;staticintBITree[] =newint[MAX];/* n --> No. of elements present in input array.BITree[0..n] --> Array that represents BinaryIndexed Tree.arr[0..n-1] --> Input array for which prefix sumis evaluated. */// Returns sum of arr[0..index]. This function// assumes that the array is preprocessed and// partial sums of array elements are stored// in BITree[].intgetSum(intindex){intsum =0;// Initialize result// index in BITree[] is 1 more than// the index in arr[]index = index +1;// Traverse ancestors of BITree[index]while(index>0){// Add current element of BITree// to sumsum += BITree[index];// Move index to parent node in// getSum Viewindex -= index & (-index);}returnsum;}// Updates a node in Binary Index Tree (BITree)// at given index in BITree. The given value// 'val' is added to BITree[i] and all of// its ancestors in tree.publicstaticvoidupdateBIT(intn,intindex,intval){// index in BITree[] is 1 more than// the index in arr[]index = index +1;// Traverse all ancestors and add 'val'while(index <= n){// Add 'val' to current node of BIT TreeBITree[index] += val;// Update index to that of parent// in update Viewindex += index & (-index);}}/* Function to construct fenwick treefrom given array.*/voidconstructBITree(intarr[],intn){// Initialize BITree[] as 0for(inti=1; i<=n; i++)BITree[i] =0;// Store the actual values in BITree[]// using update()for(inti =0; i < n; i++)updateBIT(n, i, arr[i]);}// Main functionpublicstaticvoidmain(String args[]){intfreq[] = {2,1,1,3,2,3,4,5,6,7,8,9};intn = freq.length;BinaryIndexedTree tree =newBinaryIndexedTree();// Build fenwick tree from given arraytree.constructBITree(freq, n);System.out.println("Sum of elements in arr[0..5]"+" is "+ tree.getSum(5));// Let use test the update operationfreq[3] +=6;// Update BIT for above change in arr[]updateBIT(n,3,6);// Find sum after the value is updatedSystem.out.println("Sum of elements in arr[0..5]"+" after update is "+ tree.getSum(5));}}// This code is contributed by Ranjan BinwaniPython3
# Python implementation of Binary Indexed Tree# Returns sum of arr[0..index]. This function assumes# that the array is preprocessed and partial sums of# array elements are stored in BITree[].defgetsum(BITTree,i):s=0#initialize result# index in BITree[] is 1 more than the index in arr[]i=i+1# Traverse ancestors of BITree[index]whilei >0:# Add current element of BITree to sums+=BITTree[i]# Move index to parent node in getSum Viewi-=i & (-i)returns# Updates a node in Binary Index Tree (BITree) at given index# in BITree. The given value 'val' is added to BITree[i] and# all of its ancestors in tree.defupdatebit(BITTree , n , i ,v):# index in BITree[] is 1 more than the index in arr[]i+=1# Traverse all ancestors and add 'val'whilei <=n:# Add 'val' to current node of BI TreeBITTree[i]+=v# Update index to that of parent in update Viewi+=i & (-i)# Constructs and returns a Binary Indexed Tree for given# array of size n.defconstruct(arr, n):# Create and initialize BITree[] as 0BITTree=[0]*(n+1)# Store the actual values in BITree[] using update()foriinrange(n):updatebit(BITTree, n, i, arr[i])# Uncomment below lines to see contents of BITree[]#for i in range(1,n+1):# print BITTree[i],returnBITTree# Driver code to test above methodsfreq=[2,1,1,3,2,3,4,5,6,7,8,9]BITTree=construct(freq,len(freq))("Sum of elements in arr[0..5] is "+str(getsum(BITTree,5)))freq[3]+=6updatebit(BITTree,len(freq),3,6)("Sum of elements in arr[0..5]"+" after update is "+str(getsum(BITTree,5)))# This code is contributed by Raju VarshneyC#
// C# program to demonstrate lazy// propagation in segment treeusingSystem;publicclassBinaryIndexedTree{// Max tree sizereadonlystaticintMAX = 1000;staticint[]BITree =newint[MAX];/* n --> No. of elements present in input array.BITree[0..n] --> Array that represents BinaryIndexed Tree.arr[0..n-1] --> Input array for which prefix sumis evaluated. */// Returns sum of arr[0..index]. This function// assumes that the array is preprocessed and// partial sums of array elements are stored// in BITree[].intgetSum(intindex){intsum = 0;// Initialize result// index in BITree[] is 1 more than// the index in arr[]index = index + 1;// Traverse ancestors of BITree[index]while(index>0){// Add current element of BITree// to sumsum += BITree[index];// Move index to parent node in// getSum Viewindex -= index & (-index);}returnsum;}// Updates a node in Binary Index Tree (BITree)// at given index in BITree. The given value// 'val' is added to BITree[i] and all of// its ancestors in tree.publicstaticvoidupdateBIT(intn,intindex,intval){// index in BITree[] is 1 more than// the index in arr[]index = index + 1;// Traverse all ancestors and add 'val'while(index <= n){// Add 'val' to current node of BIT TreeBITree[index] += val;// Update index to that of parent// in update Viewindex += index & (-index);}}/* Function to construct fenwick treefrom given array.*/voidconstructBITree(int[]arr,intn){// Initialize BITree[] as 0for(inti = 1; i <= n; i++)BITree[i] = 0;// Store the actual values in BITree[]// using update()for(inti = 0; i < n; i++)updateBIT(n, i, arr[i]);}// Driver codepublicstaticvoidMain(String []args){int[]freq = {2, 1, 1, 3, 2, 3,4, 5, 6, 7, 8, 9};intn = freq.Length;BinaryIndexedTree tree =newBinaryIndexedTree();// Build fenwick tree from given arraytree.constructBITree(freq, n);Console.WriteLine("Sum of elements in arr[0..5]"+" is "+ tree.getSum(5));// Let use test the update operationfreq[3] += 6;// Update BIT for above change in arr[]updateBIT(n, 3, 6);// Find sum after the value is updatedConsole.WriteLine("Sum of elements in arr[0..5]"+" after update is "+ tree.getSum(5));}}// This code is contributed by PrinciRaj1992Javascript
<script>// Javascript program to demonstrate lazy// propagation in segment tree// Max tree sizelet MAX = 1000;let BITree=newArray(MAX);/* n --> No. of elements present in input array.BITree[0..n] --> Array that represents BinaryIndexed Tree.arr[0..n-1] --> Input array for which prefix sumis evaluated. */// Returns sum of arr[0..index]. This function// assumes that the array is preprocessed and// partial sums of array elements are stored// in BITree[].functiongetSum( index){let sum = 0;// Initialize result// index in BITree[] is 1 more than// the index in arr[]index = index + 1;// Traverse ancestors of BITree[index]while(index>0){// Add current element of BITree// to sumsum += BITree[index];// Move index to parent node in// getSum Viewindex -= index & (-index);}returnsum;}// Updates a node in Binary Index Tree (BITree)// at given index in BITree. The given value// 'val' is added to BITree[i] and all of// its ancestors in tree.functionupdateBIT(n,index,val){// index in BITree[] is 1 more than// the index in arr[]index = index + 1;// Traverse all ancestors and add 'val'while(index <= n){// Add 'val' to current node of BIT TreeBITree[index] += val;// Update index to that of parent// in update Viewindex += index & (-index);}}/* Function to construct fenwick treefrom given array.*/functionconstructBITree(arr,n){// Initialize BITree[] as 0for(let i=1; i<=n; i++)BITree[i] = 0;// Store the actual values in BITree[]// using update()for(let i = 0; i < n; i++)updateBIT(n, i, arr[i]);}// Main functionlet freq=[2, 1, 1, 3, 2, 3,4, 5, 6, 7, 8, 9];let n = freq.length;// Build fenwick tree from given arrayconstructBITree(freq, n);document.write("Sum of elements in arr[0..5]"+" is "+ getSum(5)+"<br>");// Let use test the update operationfreq[3] += 6;// Update BIT for above change in arr[]updateBIT(n, 3, 6);// Find sum after the value is updateddocument.write("Sum of elements in arr[0..5]"+" after update is "+ getSum(5));// This code is contributed by patel2127</script>Output:
Sum of elements in arr[0..5] is 12 Sum of elements in arr[0..5] after update is 18Can 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).
Applications:
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.
Example Problems:
Count inversions in an array | Set 3 (Using BIT)
Two Dimensional Binary Indexed Tree or Fenwick Tree
Counting Triangles in a Rectangular space using BIT
References:
http://en.wikipedia.org/wiki/Fenwick_tree
http://community.topcoder.com/tc?module=Static&d1;=tutorials&d2;=binaryIndexedTrees
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
My Personal Notes arrow_drop_upRecommended ArticlesPage :Article Contributed By :Improved By :Practice Tags :





.png)