namastey-segment-tree
v1.0.0
Published
A JavaScript package that provides an implementation of the Segment Tree data structure for efficient range queries and updates.
Downloads
9
Maintainers
Readme
namastey-segment-tree
Brief Description: A JavaScript package implementing the Segment Tree data structure with essential methods for range queries and updates.
Features:
build(arr, node, start, end)
: Builds the segment tree from the given arrayarr
. Initializes the segment tree nodes.update(index, value, node, start, end)
: Updates the value at a specific index in the segment tree.query(left, right, node, start, end)
: Queries the sum of elements within the range[left, right]
in the segment tree.getTree()
: Returns the current state of the segment tree as an array.
Installation: To install the package globally, use the following command:
npm install -g namastey-segment-tree
Examples
const SegmentTree = require('namastey-segment-tree');
const arr = [1, 3, 5, 7, 9, 11];
const segmentTree = new SegmentTree(arr);
console.log('Segment Tree:', segmentTree.getTree());
// Query the sum of range [1, 3]
console.log('Sum of range [1, 3]:', segmentTree.query(1, 3));
// Update value at index 2 to 6
segmentTree.update(2, 6);
console.log('Updated Segment Tree:', segmentTree.getTree());
// Query the sum of range [1, 3] after update
console.log('Sum of range [1, 3] after update:', segmentTree.query(1, 3));