Javatpoint Logo
Javatpoint Logo

Binary Indexed Tree Range Updates and Range Queries

A data structure called the Binary Indexed Tree (BIT), or the Fenwick Tree, makes it possible to query and update the prefix efficiently sums in an array. It is especially helpful for resolving issues requiring cumulative frequency or range questions. The BIT effectively handles range updates and range queries.

An array of values is represented as a tree structure in a BIT, with each node holding the cumulative sum of a certain range of values from the original array. This uses the binary representation of indexes to enable quick updates and queries.

Range updates and range inquiries are sophisticated procedures that involve changing and examining numerous elements in an array or data structure within a particular range. These operations are frequently utilized in various techniques and situations to handle updates and searches on data segments effectively. Let's explore range updates and range queries in more detail:

Range Updates:

Range updates involve changing a specific subset of an array or data structure's elements. This operation is helpful when you wish to make a specific change to all the items inside a given range. Range updates involve changing all elements in a range to a specific value or adding a value to all the elements in a range.

Range updates are carried out in the context of a Binary Indexed Tree (BIT) or Fenwick Tree by carrying out two update operations, one at the starting index of the range and the other at the index right after the ending index. While the update at the ending index reduces the cumulative sum for indices after that point, the update at the starting index raises the cumulative sum for that index and all succeeding indices. With this method, the change within the specified range is efficiently captured.

Range updates entail adjusting a specific subset of an array or data structure's elements. This operation is helpful when you wish to effectively apply a modification to numerous elements throughout a set period. Algorithms and issues that call for simultaneously updating many values frequently use range updates.

Example: Consider a set of numbers in an array. You want to add a specific amount to each value within a defined range.

Suppose you have the following array:

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

Additionally, you want to add 4 more values to the [2, 5] range. The array would change after the range modification to:

[3, 12, 6, 10, 9, 1, 4]

BIT (Fenwick Tree) approach:

The initial index of the range and the index just after the ending index must both receive changes to accomplish a range update using a Binary Indexed Tree (BIT). While the update at the ending index reduces the cumulative sum for indices after that point, the update at the starting index raises the cumulative sum for that index and all succeeding indices. This effectively captures the change within the intended range.

Here is how a BIT would be used to implement a range update:

Set the BIT array's initial value to zeros.

Add the increment value to the starting index of the range and all succeeding indices up to the array's end to update that index.

Subtract the increment value from the index that follows the ending index and from each index after that, all the way to the end of the array, to perform an update at that point.

Range update pseudocode using BIT:

In this pseudocode, l stands for the range's initial index, r for its ending index, and delta for the increment that needs to be applied.

You can change the cumulative sums in the range [l, r] by applying these two updates, then change the subsequent cumulative sums accordingly.

Range updates benefit from the BIT's ability to update cumulative sums and generate prefix sums fast, making range updates utilizing the BIT data structure efficient. They become an invaluable tool for tackling numerous algorithmic issues involving changing ranges of values in an array.

Python Code:

Output:

Updated array: [3, 12, 6, 10, 9, 1, 4]

The update function applies a certain value to the BIT at a particular index in this illustration. The range_update method makes two calls to the update function to create the range update effect. Using the BIT, the prefix_sum function determines the prefix sum up to a specified index.

Be aware that this code will index the array and the BIT using a 1-based system to quickly navigate the BIT structure, the index & -index operation extracts the rightmost set bit. Whether your problem uses 0-based- or 1-based indexing, adjust the indices accordingly.

Range Queries:

In an array or data structure, range inquiries entail getting details about a certain range of elements. The computation of an aggregate value or property of the elements inside the range is done using this operation. Examples of range queries are calculating the total, minimum, maximum, or other statistics inside a range.

The prefix sum technique can effectively respond to range queries in a Binary Indexed Tree or Fenwick Tree context. The sum of the items within the range can be calculated by subtracting the prefix sum at the index just before the starting index from the prefix sum at the range's ending index. This method uses the BIT's ability to accumulate sums to calculate the required outcome swiftly.

In an array or data structure, range inquiries entail getting details about a certain range of elements. Using this method, you can efficiently calculate some aggregate value or property of the components within the range. Range queries are frequently employed in various algorithmic issues that call for the analysis of data intervals or segments. A more thorough description of the range of inquiries is provided below:

Example: You wish to quickly get the sum of elements inside a given range given an array of values.

Suppose you have the following array:

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

The sum of numbers in the [2, 5] range is what you're looking for. Values 8 + 2 + 6 + 5 add up to 21.

BIT (Fenwick Tree) approach:

Use the prefix sum approach for a range query on a Binary Indexed Tree (BIT). The sum of the items within the range can be calculated by subtracting the prefix sum at the index just before the starting index from the prefix sum at the range's ending index.

Here is how a BIT would be used to implement a range query:

  1. Utilize the prefix_sum function to determine the prefix sum up to the range's terminating index.
  2. Using the same prefix_sum function, calculate the prefix sum to the range's starting index.
  3. You can determine the element sum that falls inside the necessary range by comparing the prefix sums obtained in steps 1 and 2 to each other.

Range query pseudocode using BIT:

RangeQuery(l, r):

return PrefixSum(r) - PrefixSum(l - 1)

In this pseudocode, "1" designates the range's beginning index and "r" its ending index.

Without explicitly iterating through the array, you can quickly extract data about the range of values by calculating the difference between prefix sums.

Because range searches use the BIT data structure's effective prefix sum calculation capabilities, range inquiries run quickly. The ability to analyze or query data ranges in an array makes them useful for solving various algorithmic challenges.

Python Code:

Output:

Sum of values in range [2, 5]: 21

In this example, the initial BIT is constructed using cumulative sums and the update function. Using the BIT, the prefix_sum function determines the prefix sum up to a specified index. Using the distinction between prefix sums, the range_query function determines the total values falling within a specified range.

After initializing the BIT with cumulative sums, the code shows how to use a range query to retrieve the sum of values inside a certain range.

Whether your problem uses 0-based- or 1-based indexing, adjust the indices accordingly.







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