18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
SegmentTreeUpdate.cs 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2024-01-08 14:18 . Switch to file-scoped namespaces (#434)
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));
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助