MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / FenwickTree

Class FenwickTree

Trees/FenwickTree.js:7–35  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5 */
6
7class FenwickTree {
8 constructor(feneickArray, array, n) {
9 for (let i = 1; i <= n; i++) {
10 feneickArray[i] = 0
11 }
12 for (let i = 0; i < n; i++) {
13 this.update(feneickArray, n, i, array[i])
14 }
15 }
16
17 update(feneickArray, n, index, value) {
18 index = index + 1
19 while (index <= n) {
20 feneickArray[index] += value
21 index += index & -index
22 }
23 }
24
25 getPrefixSum(feneickArray, index) {
26 let currSum = 0
27 index = index + 1
28 while (index > 0) {
29 currSum += feneickArray[index]
30 index -= index & -index
31 }
32
33 return currSum
34 }
35}
36export { FenwickTree }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected