forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryIndexedTree.cs
64 lines (56 loc) · 1.84 KB
/
BinaryIndexedTree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace DataStructures.Fenwick;
/// <summary>
/// Represent classical realization of Fenwiсk tree or Binary Indexed tree.
///
/// BITree[0..n] --> Array that represents Binary Indexed Tree.
/// arr[0..n-1] --> Input array for which prefix sum is evaluated.
/// </summary>
public class BinaryIndexedTree
{
private readonly int[] fenwickTree;
/// <summary>
/// Initializes a new instance of the <see cref="BinaryIndexedTree"/> class.
/// Create Binary indexed tree from the given array.
/// </summary>
/// <param name="array">Initial array.</param>
public BinaryIndexedTree(int[] array)
{
fenwickTree = new int[array.Length + 1];
for (var i = 0; i < array.Length; i++)
{
UpdateTree(i, array[i]);
}
}
/// <summary>
/// This method assumes that the array is preprocessed and
/// partial sums of array elements are stored in BITree[].
/// </summary>
/// <param name="index">The position to sum from.</param>
/// <returns>Returns sum of arr[0..index].</returns>
public int GetSum(int index)
{
var sum = 0;
var startFrom = index + 1;
while (startFrom > 0)
{
sum += fenwickTree[startFrom];
startFrom -= startFrom & (-startFrom);
}
return sum;
}
/// <summary>
/// Updates a node in Binary Index Tree at given index.
/// The given value 'val' is added to BITree[i] and all of its ancestors in tree.
/// </summary>
/// <param name="index">Given index.</param>
/// <param name="val">Value to be update on.</param>
public void UpdateTree(int index, int val)
{
var startFrom = index + 1;
while (startFrom <= fenwickTree.Length)
{
fenwickTree[startFrom] += val;
startFrom += startFrom & (-startFrom);
}
}
}