Javatpoint Logo
Javatpoint Logo

Binary Indexed Tree Range Updates and Point Queries

A data structure called a Binary Indexed Tree (BIT), also known as a Fenwick Tree, efficiently supports both range updates and point searches while optimizing for cumulative operations on an array. It keeps track of an auxiliary array that displays cumulative values over various intervals.

The BIT is initialized during creation with zeros or starting array values. Bitwise arithmetic is used to add values to the BIT iteratively. A point query returns the total value up to a specified index by traversing the BIT in reverse order and accumulating data. This method guarantees effective querying with an O(log N) time complexity, where N is the size of the array.

When a range is updated, the BIT is changed at two indices: the index that comes before the range and the index that comes just after it. This smart method enables the BIT to save the results of a range addition, making it easier for subsequent point searches to consider the updates. BIT is appropriate for workloads demanding frequent updates and queries since range updates and point queries can be completed in O(log N) time.

Overall, the Binary Indexed Tree balances query performance and space efficiency. It is advantageous in situations requiring effective manipulation of arrays, such as competitive programming or tasks involving cumulative computations, because of its capacity to preserve cumulative information and swiftly process changes and queries.

Range Updates:

Effective updates across a variety of array items are possible with BIT. It would help if you changed the BIT at the range's beginning index and right after its end to accomplish a range update. The BIT preserves the array's cumulative information thanks to this modification, which considers the update in the impacted range.

Range updates involve effectively changing a continuous range of array members. Range updates in a Binary Indexed Tree are designed to be applied at two specified indices: the index at the start and right after the range's end. This method successfully catches the change brought about by the update inside the given range.

A step-by-step breakdown of how range updates are performed using a Binary Indexed Tree:

Suppose you want to update all elements in an array of size N from index start to index end by adding the value val to all the members in that range.

Increase the Initial Index:

Update the BIT at the index first, then start by adding the value val. This action serves as a reminder of the range's initial alteration.

Decreasing the Final Index by One

Subtracting the value val updates the BIT at the index end + 1. The alteration that was made after the range's conclusion is neutralized in this step.

Propagation and Effect:

The range update's cumulative effect can be seen when doing point queries on the BIT. The values along the route from the index being queried to the tree's root are accumulated during a point query by traversing the BIT structure. While the subtraction at the end plus one contributes adversely, the addition at the beginning positively contributes.

The outcome of a point query using the BIT accurately captures the range update's overall impact.

You may efficiently capture the effect of the range update within the BIT data structure by increasing the starting index and decreasing the ending index + 1. Bitwise operations are used in updates and queries to ensure quick movement through the BIT.

In conclusion, this detailed procedure shows how Binary Indexed Trees may effectively manage range updates by keeping cumulative information and swiftly absorbing changes brought about by the updates.

Example:

Assume we have the following 10-element array: [0, 2, 4, 1, 7, 3, 5, 6, 2].

By adding 3 to each element in that range, from index 3 to index 7, we want to update the range.

Create the BIT and run updates:

Fill a BIT with zeros at startup:

BIT = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Add 3 to the entries from index 3 to index 7 to update the range:

update(3, 3) # Add 3 at index 3

update(8, -3) # Subtract 3 at index 8 (end + 1)

Following the modifications, the BIT array is now:

BIT = [0, 0, 0, 3, 3, 3, 3, 0, -3, 0]

Perform point queries:

Let's imagine using a point query to determine the cumulative sum up to index 6.

query(6):

result = 0

while idx > 0:

result += BIT[idx]

idx = idx - (idx & -idx)

return result

We add up the values in the BIT array along the path starting with idx = 6:

result = 0 + BIT[6] + BIT[4] + BIT[0] = 3 + 3 + 0 = 6

The query accurately returns the cumulative total up to index 6.

This illustration shows the BIT's effective management of range updates and point queries. The range update was carried out by including three at the beginning index and taking three away from the finishing index plus one. The cumulative impact of the range update was appropriately reflected in the query result later during the point query.

Python Code:

Output:

Cumulative sum up to index 6: 6
Cumulative sum up to index 8: 15

The BinaryIndexedTree class is defined in this example to deal with the range update and point query actions. The update method applies a provided value to the BIT at a certain index. Range updates are carried out using the update method on the range's start and end points via the range_update method. The query method returns the cumulative total up to a particular index.

In the code, a Binary Indexed Tree is initialized, a range update is run, and point queries are used to find cumulative sums.

The sum of [0, 2, 4, 1, 7, 3] is the cumulative sum up to index 6, which equals 6. The array [0, 2, 4, 1, 7, 3, 5, 6, 2]'s total sum up to index 8 is 15, which equals the sum of the entire array.

In Java:

Output:

Cumulative sum up to index 6: 6
Cumulative sum up to index 8: 15

Point Queries:

BIT enables quick point queries that retrieve the total value up to a particular index. The point query procedure entails moving through the BIT structure and adding up the values kept along the way from the index being requested to the tree's base. This accumulation is a precise reflection of the range updates' overall impact.

The advantage of BIT is that it makes use of binary representation. It creates a tree-like structure using bitwise operations that optimizes range updates and point queries. For both types of operations, these operations result in a temporal complexity of O(log N), where N is the size of the array.

Point queries aim to find the total number of elements in the original array up to a particular index. A Binary Indexed Tree successfully handles these queries by utilizing its cumulative structure.

Point-Queries Steps:

Initialization: Fill in the initial values of the array or a BIT with zeros. The BIT will keep track of cumulative data.

Construction: Iterate through the initial array to build the BIT, updating it with the update operation at each index. The update procedure updates the BIT at that index by adding the value of the matching array element.

Point Query: Use the query operation to determine the total up to that index to make a pointed inquiry at index idx. The query action builds up the values kept in the BIT array along the line leading from the queried index to the tree's root.

Efficiency: The temporal complexity of point searches in a BIT is O(log N), where N is the array/BIT's size. The binary representation and bitwise operations are what lead to this performance.

Imagine you have a financial ledger where each entry is an expense or an income, and you want to query the overall balance as of a particular moment. You may quickly calculate this balance using point queries with a BIT.

BIT is extremely useful in cases requiring cumulative information tracking, such as prefix sums, prefix maximums, or balances over time. This is due to BIT's ability to execute quick point queries. It's very helpful when studying data cumulatively without constantly recalculating sums.

Python Code:

Output:

Cumulative expenses up to day 3: 45
Cumulative expenses up to day 5: 65

In this example, the code creates a Binary Indexed Tree from scratch and updates it based on the daily expenditures. Point queries are run once the BIT has been built to determine the total expenses up to a certain day.

The total expenses through day three are 10 + 15 + 20 = 45.

The total expenditures till day 5 are 65 (10 + 15 + 20 + 8 + 12).

This exemplifies the effective handling of point queries by a binary indexed tree while calculating cumulative data like expenses over time.

In java:

Output:

Cumulative expenses up to day 3: 45
Cumulative expenses up to day 5: 65

O(Q * log(N)) is the time complexity.

Range updates and range queries in a binary indexed tree are demonstrated in the program above. Both operations take O(logN) time. Therefore, the total time complexity is O(Q* log(N)), where Q and N are the number of queries and array elements, respectively.

O(N) Space Complexity

We have constructed two arrays of size (N+1) for the two necessary binary-indexed trees in the program above to demonstrate range updates and queries in binary-indexed trees. Therefore, the space's total complexity is O(N), where N is the number of elements in the given array.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA