Collection Kit

SegmentTree

Tree

A SegmentTree is a tree data structure used for storing intervals or segments, allowing efficient querying of segment overlaps and range queries.

Import Statement
import { SegmentTree } from "collection-kit";

Key Features

  • Efficient Range Queries: Supports querying sums, minimums, or maximums over a range of elements.
  • Dynamic Size: Can be built and modified as needed.

Common Operations

Build
Construct the segment tree from an array.
Update
Modify an element in the array and update the tree.
Query
Retrieve information (e.g., sum, min, max) over a specified range.

Example Code

import { SegmentTree } from "collection-kit";

const arr = [1, 3, 5, 7, 9, 11];
const segTree = new SegmentTree(arr);

console.log("Sum [1-3]:", segTree.query(1, 3)); // 15
segTree.update(1, 10);
console.log("Sum [1-3]:", segTree.query(1, 3)); // 22