Collection Kit

IntervalTree

Tree

An IntervalTree is a tree structure that stores intervals and allows for efficient querying of overlapping intervals. It is useful for applications that require range queries.

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

Key Features

  • Efficient Range Queries: Supports querying for all intervals that overlap with a given interval.
  • Dynamic Size: Can grow and shrink as intervals are added or removed.

Common Operations

Insert
Add a new interval to the tree.
Remove
Delete an interval from the tree.
Query
Retrieve all intervals that overlap with a given interval.

Example Code

import { IntervalTree } from "collection-kit";

const tree = new IntervalTree();
tree.insert([1, 5]);
tree.insert([3, 7]);
tree.insert([6, 10]);

const overlapping = tree.query([4, 8]);
console.log("Overlapping intervals:", overlapping);