-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
SegmentTreeUpdate.cs
47 lines (43 loc) · 1.49 KB
/
SegmentTreeUpdate.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
namespace DataStructures.SegmentTrees;
/// <summary>
/// This is an extension of a segment tree, which allows the update of a single element.
/// </summary>
public class SegmentTreeUpdate : SegmentTree
{
/// <summary>
/// Initializes a new instance of the <see cref="SegmentTreeUpdate" /> class.
/// Runtime complexity: O(n) where n equals the array-length.
/// </summary>
/// <param name="arr">Array on which the queries should be made.</param>
public SegmentTreeUpdate(int[] arr)
: base(arr)
{
}
/// <summary>
/// Updates a single element of the input array.
/// Changes the leaf first and updates its parents afterwards.
/// Runtime complexity: O(logN) where N equals the initial array-length.
/// </summary>
/// <param name="node">Index of the node that should be updated.</param>
/// <param name="value">New Value of the element.</param>
public void Update(int node, int value)
{
Tree[node + Tree.Length / 2] = value;
Propagate(Parent(node + Tree.Length / 2));
}
/// <summary>
/// Recalculates the value of node by its children.
/// Calls its parent to do the same.
/// </summary>
/// <param name="node">Index of current node.</param>
private void Propagate(int node)
{
if (node == 0)
{
// passed root
return;
}
Tree[node] = Tree[Left(node)] + Tree[Right(node)];
Propagate(Parent(node));
}
}