diff --git a/src/data-structures/tree/fenwick-tree/FenwickTree.js b/src/data-structures/tree/fenwick-tree/FenwickTree.js index da4f8416..7bfb8426 100644 --- a/src/data-structures/tree/fenwick-tree/FenwickTree.js +++ b/src/data-structures/tree/fenwick-tree/FenwickTree.js @@ -13,13 +13,13 @@ export default class FenwickTree { } /** - * Adds value to position. + * Adds value to existing value at position. * * @param {number} position * @param {number} value * @return {FenwickTree} */ - update(position, value) { + increase(position, value) { if (position < 1 || position > this.arraySize) { throw new Error('Position is out of allowed range'); } diff --git a/src/data-structures/tree/fenwick-tree/__test__/FenwickTree.test.js b/src/data-structures/tree/fenwick-tree/__test__/FenwickTree.test.js index ece00ec8..0fd71657 100644 --- a/src/data-structures/tree/fenwick-tree/__test__/FenwickTree.test.js +++ b/src/data-structures/tree/fenwick-tree/__test__/FenwickTree.test.js @@ -20,7 +20,7 @@ describe('FenwickTree', () => { expect(tree.treeArray.length).toBe(inputArray.length + 1); inputArray.forEach((value, index) => { - tree.update(index + 1, value); + tree.increase(index + 1, value); }); expect(tree.treeArray).toEqual([0, 3, 5, -1, 10, 5, 9, -3, 19, 7, 9, 3]); @@ -41,23 +41,42 @@ describe('FenwickTree', () => { expect(tree.queryRange(1, 2)).toBe(5); expect(tree.queryRange(2, 4)).toBe(7); expect(tree.queryRange(6, 9)).toBe(11); + + tree.increase(3, 1); + + expect(tree.query(1)).toBe(3); + expect(tree.query(2)).toBe(5); + expect(tree.query(3)).toBe(5); + expect(tree.query(4)).toBe(11); + expect(tree.query(5)).toBe(16); + expect(tree.query(6)).toBe(20); + expect(tree.query(7)).toBe(17); + expect(tree.query(8)).toBe(20); + expect(tree.query(9)).toBe(27); + expect(tree.query(10)).toBe(29); + expect(tree.query(11)).toBe(32); + + expect(tree.queryRange(1, 1)).toBe(3); + expect(tree.queryRange(1, 2)).toBe(5); + expect(tree.queryRange(2, 4)).toBe(8); + expect(tree.queryRange(6, 9)).toBe(11); }); it('should correctly execute queries', () => { const tree = new FenwickTree(5); - tree.update(1, 4); - tree.update(3, 7); + tree.increase(1, 4); + tree.increase(3, 7); expect(tree.query(1)).toBe(4); expect(tree.query(3)).toBe(11); expect(tree.query(5)).toBe(11); expect(tree.queryRange(2, 3)).toBe(7); - tree.update(2, 5); + tree.increase(2, 5); expect(tree.query(5)).toBe(16); - tree.update(1, 3); + tree.increase(1, 3); expect(tree.queryRange(1, 1)).toBe(7); expect(tree.query(5)).toBe(19); expect(tree.queryRange(1, 5)).toBe(19); @@ -66,12 +85,12 @@ describe('FenwickTree', () => { it('should throw exceptions', () => { const tree = new FenwickTree(5); - const updateAtInvalidLowIndex = () => { - tree.update(0, 1); + const increaseAtInvalidLowIndex = () => { + tree.increase(0, 1); }; - const updateAtInvalidHighIndex = () => { - tree.update(10, 1); + const increaseAtInvalidHighIndex = () => { + tree.increase(10, 1); }; const queryInvalidLowIndex = () => { @@ -86,8 +105,8 @@ describe('FenwickTree', () => { tree.queryRange(3, 2); }; - expect(updateAtInvalidLowIndex).toThrowError(); - expect(updateAtInvalidHighIndex).toThrowError(); + expect(increaseAtInvalidLowIndex).toThrowError(); + expect(increaseAtInvalidHighIndex).toThrowError(); expect(queryInvalidLowIndex).toThrowError(); expect(queryInvalidHighIndex).toThrowError(); expect(rangeQueryInvalidIndex).toThrowError();