Searching in Binary Indexed Tree using Binary Lifting in O(LogN)
Last Updated :
02 Oct, 2023
Binary Indexed Tree (BIT) is a data structure that allows efficient queries of a range of elements in an array and updates on individual elements in O(log n) time complexity, where n is the number of elements in the array.
Binary Lifting:
- One of the efficient techniques used to perform search operations in BIT is called Binary lifting.
- Binary Lifting is a technique that helps to navigate through the BIT in O(log n) time complexity. The basic idea of binary lifting is to perform jumps to the next ancestor that covers the range of elements you want to query.
To perform search operations in BIT using binary lifting, we need to store additional information in the BIT, which is the binary representation of the index. For example, if we have an index i, we can write it in binary form as follows:
, where b[k], b[k-1], …, b[0] are binary digits (0 or 1). We store this binary representation in the BIT for each index i.
To perform a range query [l, r] in the BIT:
- Find the highest ancestor of r which is also an ancestor of l. We can do this by finding the largest power of 2 that is less than or equal to the distance between l and r.
- We call this power of 2k.
- Then add 2^k to l to move up to the next ancestor.
- We continue this process until we reach r. At each step, we check if the binary digit at the k-th position of r is 1. If it is 1, we include the value of the node at r in the query result.
- Finally, we return the sum of the query result.
Examples:
Input: arr[] = {1, 3, 2, 5, 4}, k = 3
Explanation: The binary representation of the numbers in the range [0, k] are:
To find the sum of elements in the range [0, k], we can represent the range [0, k] using the binary representation of k. Starting from the most significant bit, if the bit is 1, we include the corresponding prefix sum in the total sum.
Illustration:
- The Binary Indexed Tree (BIT) is a data structure that allows efficient updates and queries on prefix sums of an array. It uses an array of size n to store cumulative sums. Each element of the array represents the sum of certain elements in the original array.
- In binary lifting, we use the binary representation of the index to perform efficient searches in the BIT. By using the binary representation of the index, we can determine which prefix sums to include in the query based on the bits of the index.
Here’s a brief explanation of how the standard BIT implementation works:
- Initialization: The BIT is initialized with all values set to zero.
- Update operation: The update() function is used to modify the value at a specific index i. It increases the value at index i by v and updates the necessary indices to maintain the cumulative frequency property of the BIT. It uses the binary representation of i to determine the indices to update.
- Query operation: The query() function is used to calculate the cumulative sum from index 0 to index i. It traverses the BIT, starting from the index i, following the binary representation of i to find the necessary indices to include in the sum. It accumulates the values of the corresponding indices.
- Range query operation: The range_query() function calculates the sum of values within a given range from index l to index r. It iteratively uses the binary representation of r to determine the necessary indices to include in the sum. It subtracts the cumulative sum from index l-1 to obtain the sum of values within the range.
Here’s an implementation of binary lifting search operations in BIT:
C++
#include <bits/stdc++.h>
using namespace std;
class BinaryIndexedTree {
private :
vector< int > tree;
public :
BinaryIndexedTree( int n) { tree.resize(n + 1, 0); }
void update( int i, int v)
{
i += 1;
while (i < tree.size()) {
tree[i] += v;
i += i & (-i);
}
}
int query( int i)
{
i += 1;
int res = 0;
while (i > 0) {
res += tree[i];
i -= i & (-i);
}
return res;
}
int range_query( int l, int r)
{
int res = 0;
while (r >= l) {
int k = r & (-r);
res += (r & k) ? tree[r] : 0;
r -= k;
}
return res;
}
};
int main()
{
int n = 10;
BinaryIndexedTree bit(n);
bit.update(0, 1);
bit.update(1, 2);
bit.update(2, 3);
bit.update(3, 4);
cout << bit.query(3) << endl;
cout << bit.range_query(1, 3) << endl;
return 0;
}
|
Java
import java.util.*;
class BinaryIndexedTree {
private List<Integer> tree;
public BinaryIndexedTree( int n) {
tree = new ArrayList<>(n + 1 );
for ( int i = 0 ; i <= n; i++) {
tree.add( 0 );
}
}
public void update( int i, int v) {
i += 1 ;
while (i < tree.size()) {
tree.set(i, tree.get(i) + v);
i += i & (-i);
}
}
public int query( int i) {
i += 1 ;
int res = 0 ;
while (i > 0 ) {
res += tree.get(i);
i -= i & (-i);
}
return res;
}
public int rangeQuery( int l, int r) {
int res = 0 ;
while (r >= l) {
int k = r & (-r);
res += (r & k) != 0 ? tree.get(r) : 0 ;
r -= k;
}
return res;
}
public static void main(String[] args) {
int n = 10 ;
BinaryIndexedTree bit = new BinaryIndexedTree(n);
bit.update( 0 , 1 );
bit.update( 1 , 2 );
bit.update( 2 , 3 );
bit.update( 3 , 4 );
System.out.println(bit.query( 3 ));
System.out.println(bit.rangeQuery( 1 , 3 ));
}
}
|
Python
class BinaryIndexedTree:
def __init__( self , n):
self .tree = [ 0 ] * (n + 1 )
def update( self , i, v):
i + = 1
while i < len ( self .tree):
self .tree[i] + = v
i + = i & ( - i)
def query( self , i):
i + = 1
res = 0
while i > 0 :
res + = self .tree[i]
i - = i & ( - i)
return res
def range_query( self , l, r):
res = 0
while r > = l:
k = r & ( - r)
res + = self .tree[r] if r & k else 0
r - = k
return res
def main():
n = 10
bit = BinaryIndexedTree(n)
bit.update( 0 , 1 )
bit.update( 1 , 2 )
bit.update( 2 , 3 )
bit.update( 3 , 4 )
print (bit.query( 3 ))
print (bit.range_query( 1 , 3 ))
if __name__ = = "__main__" :
main()
|
C#
using System;
using System.Collections.Generic;
class BinaryIndexedTree
{
private List< int > tree;
public BinaryIndexedTree( int n)
{
tree = new List< int >(n + 1);
for ( int i = 0; i <= n; i++)
{
tree.Add(0);
}
}
public void Update( int i, int v)
{
i += 1;
while (i < tree.Count)
{
tree[i] += v;
i += i & (-i);
}
}
public int Query( int i)
{
i += 1;
int res = 0;
while (i > 0)
{
res += tree[i];
i -= i & (-i);
}
return res;
}
public int RangeQuery( int l, int r)
{
int res = 0;
while (r >= l)
{
int k = r & (-r);
res += (r & k) != 0 ? tree[r] : 0;
r -= k;
}
return res;
}
}
class Program
{
static void Main()
{
int n = 10;
BinaryIndexedTree bit = new BinaryIndexedTree(n);
bit.Update(0, 1);
bit.Update(1, 2);
bit.Update(2, 3);
bit.Update(3, 4);
Console.WriteLine(bit.Query(3));
Console.WriteLine(bit.RangeQuery(1, 3));
}
}
|
Javascript
class BinaryIndexedTree {
constructor(n) {
this .tree = new Array(n + 1).fill(0);
}
update(i, v) {
i += 1;
while (i < this .tree.length) {
this .tree[i] += v;
i += i & -i;
}
}
query(i) {
i += 1;
let res = 0;
while (i > 0) {
res += this .tree[i];
i -= i & -i;
}
return res;
}
range_query(l, r) {
let res = 0;
while (r >= l) {
const k = r & -r;
res += (r & k) ? this .tree[r] : 0;
r -= k;
}
return res;
}
}
const n = 10;
const bit = new BinaryIndexedTree(n);
bit.update(0, 1);
bit.update(1, 2);
bit.update(2, 3);
bit.update(3, 4);
console.log(bit.query(3));
console.log(bit.range_query(1, 3));
|
Time Complexity: O(N * logN).
Auxiliary Space: O(N).
Share your thoughts in the comments
Please Login to comment...