mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
merge: fixes: #{} (#853)
* fixes: #{} * fixes : ISSUE#795 * Fixed ISSUE:795 * Fixed : ISSUE:795 * Fixed ISSUE : 795 * Fixed ISSUE : 795
This commit is contained in:
36
Trees/FenwickTree.js
Normal file
36
Trees/FenwickTree.js
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Author: Mohit Kumar
|
||||
* Fedwick Tree Implementation in JavaScript
|
||||
* Fedwick Tree Implementation for finding prefix sum.
|
||||
*/
|
||||
|
||||
class FenwickTree {
|
||||
constructor (feneickArray, array, n) {
|
||||
for (let i = 1; i <= n; i++) {
|
||||
feneickArray[i] = 0
|
||||
}
|
||||
for (let i = 0; i < n; i++) {
|
||||
this.update(feneickArray, n, i, array[i])
|
||||
}
|
||||
}
|
||||
|
||||
update (feneickArray, n, index, value) {
|
||||
index = index + 1
|
||||
while (index <= n) {
|
||||
feneickArray[index] += value
|
||||
index += index & (-index)
|
||||
}
|
||||
}
|
||||
|
||||
getPrefixSum (feneickArray, index) {
|
||||
let currSum = 0
|
||||
index = index + 1
|
||||
while (index > 0) {
|
||||
currSum += feneickArray[index]
|
||||
index -= index & (-index)
|
||||
}
|
||||
|
||||
return currSum
|
||||
}
|
||||
}
|
||||
export { FenwickTree }
|
Reference in New Issue
Block a user