merge: fixes: #{} (#853)

* fixes: #{}

* fixes : ISSUE#795

* Fixed ISSUE:795

* Fixed : ISSUE:795

* Fixed ISSUE : 795

* Fixed ISSUE : 795
This commit is contained in:
Mohit Kumar
2021-11-30 18:10:20 +05:30
committed by GitHub
parent bf681d1a91
commit c3b2bacf08
3 changed files with 11521 additions and 560 deletions

36
Trees/FenwickTree.js Normal file
View 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 }

View File

@ -0,0 +1,22 @@
import { FenwickTree } from '../FenwickTree'
describe('Fenwick Tree Implementation', () => {
const fenwickArray = new Array(1000)
const array = [3, 2, 0, 6, 5, -1, 2]
const length = array.length
const fenwickTree = new FenwickTree(fenwickArray, array, length)
it('Fenwick Tree - Prefix sum of array', () => {
const prefixSum = fenwickTree.getPrefixSum(fenwickArray, 6)
expect(prefixSum).toBe(23)
})
array[2] += 6
fenwickTree.update(fenwickArray, length, 2, 6)
it('Fenwick Tree - Prefix sum of Updated array', () => {
const prefixSum = fenwickTree.getPrefixSum(fenwickArray, 6)
expect(prefixSum).toBe(23)
})
})

12023
package-lock.json generated

File diff suppressed because it is too large Load Diff