Javatpoint Logo
Javatpoint Logo

Binary indexed tree

A data structure called a Binary Indexed Tree (BIT), also called a Fenwick Tree, is made to perform cumulative frequency operations on an array of elements effectively. Since it offers quick updates and prefix sum queries, it is especially helpful for dynamic cumulative calculations issues.

The main concept of the BIT is to efficiently store cumulative sums by using a binary representation of the array indices. The design of the tree assures that these cumulative sums may be updated and retrieved with logarithmic time complexity. Each member of the BIT denotes the sum of a certain range in the original array.

The two main operations supported by the BIT are query and update. In O(log n) time, a query operation computes the cumulative sum of entries from the array's start-up to a specified index. With the same O(log n) time complexity, an update operation modifies an element in the array and propagates the changes throughout the BIT.

The BIT finds use in various algorithms and programming tasks, including computing prefix sums, resolving dynamic range sum queries, and more because of its small memory footprint and effective time complexity for both queries and updates. It is a crucial data structure in competitive programming and algorithmic problem-solving, acting as a potent tool for optimizing cumulative calculations.

Representation of Binary indexed tree:

An array of numbers is frequently used to represent the Binary Indexed Tree (BIT), commonly called the Fenwick Tree. This array is one size larger than the original array, which we want to conduct cumulative operations. Give this array the name BIT.

The BIT "BIT" will contain n+1 entries for an array of length n. The cumulative total of each element in the BIT array corresponds to a certain range in the initial array "arr." The indices of the BIT array map to various binary representations of the original array's indices.

The BIT array is created and used as follows:

Construction: The BIT started with a value of zero.

Updating Elements: Elements are updated by updating related elements in the BIT array in the manner described below:

  • We start at index i+1 in the BIT and increment all elements whose binary representations contain the bit position that i+1 indicates to update element arr[i].

Using prefix sum queries: The procedures below are used to determine the cumulative total in the original array from index 0 to i (inclusive):

  • We begin at index i+1 in the BIT and add entries as we update the index by deleting the lowest set bit from its binary representation to query the prefix sum.

Example: Original Array (arr):

[4, 2, 5, 8, 3, 1, 6]

BIT Array (BIT):

[ 0, 4, 6, 5, 19, 3, 4, 16]

Binary indexed tree




BIT array: [0, 4, 6, 5, 19, 3, 4, 16]
Prefix Sum [0, 2]: 11
Prefix Sum [0, 5]: 23
Prefix Sum [2, 4]: 17

Utilizing a Binary Indexed Tree (BIT) to effectively handle cumulative sum operations on arrays is demonstrated in the accompanying Python code. The arr array is first populated in the code as [4, 2, 5, 8, 3, 1, 6], after which a BIT array is created and its elements are all set to zero.

The central operations of the BIT are carried out by two fundamental functions, update and query. Using bitwise operations to traverse the tree efficiently, the update function advances the pertinent nodes in the BIT according to the index and value supplied. The query function uses bitwise operations to efficiently navigate the tree, generating cumulative sums from index 0 to a specific index in the BIT array.

The construct_BIT method creates a completely built BIT by initializing the BIT array and updating each element of the original arr array using the update function.

After building the BIT, the query function runs three prefix sum queries, yielding cumulative sums for various ranges in the original array. The result shows how quickly the BIT can compute prefix sums and how effectively it handles these queries.

In conclusion, the code illustrates how the BIT may be represented using arrays to effectively perform cumulative sum operations even if it is not a standard tree data structure. The BIT is an effective tool for various algorithmic tasks and competitive programming challenges that need cumulative computations on arrays because of its small memory footprint and optimized update and query operations.

In java:


BIT array: 0 4 6 5 19 3 1 27 
Prefix Sum [0, 2]: 11
Prefix Sum [0, 5]: 23
Prefix Sum [2, 4]: 17

The Binary Indexed Tree (BIT), also referred to as a Fenwick Tree, is implemented in the Java code. This data structure is made for quick computation of prefix sums and range sum queries in an array. The code first specifies the update, query, and constructBIT methods before showing how to use them in the main method.

To update the BIT array with the provided value at the given index, the update function requires the BIT array, an index, and a value. It navigates over the BIT array using a bitwise operation (index & -index) and modifies the values to keep the cumulative sum attribute. The query function iteratively adds pertinent BIT values to determine the prefix sum up to the specified index. The constructBIT function creates the BIT array by updating each input array element sequentially.

An example array named arr is defined in the main method as [4, 2, 5, 8, 3, 1, 6]. The BIT array is built using the constructBIT function. The following three prefix sum queries are run:

"query(BIT, 2)": computes the total of items in the array from index 0 to 2 (4 + 2 + 5), giving a prefix sum of 11.

"query(BIT, 5)": This query computes the prefix sum of all the members in the array up to index 5 (4 + 2 + 5 + 8 + 3 + 1), which is 23.

"query(BIT, 4) - query(BIT, 1)": computes the sum of the elements from indexes 2 to 4 (5 + 8 + 3) minus the total of the elements from indexes 0 to 1, giving a range sum of 17 as a result.

The generated BIT array, which comprises cumulative sums, is first shown in the output, followed by the answers to the three prefix sum inquiries. The program and its results show how the Binary Indexed Tree effectively manages cumulative sums and facilitates range sum searches for a given array.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA