Collection Kit

KDTree

Spatial

A KDTree (k-dimensional tree) is a binary tree used for organizing points in a k-dimensional space, allowing for efficient range searches and nearest neighbor searches.

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

Key Features

  • Multi-Dimensional: Supports points in multiple dimensions.
  • Efficient Searches: Allows for quick range and nearest neighbor queries.

Common Operations

Insert
Add a new point to the KDTree.
Search
Retrieve points within a specified range.
NearestNeighbor
Find the closest point to a given point.

Example Code

import { KDTree } from "collection-kit";

const tree = new KDTree(2); // 2D space
tree.insert([2, 3]);
tree.insert([5, 4]);
tree.insert([9, 6]);

const nearest = tree.nearestNeighbor([5, 5]);
console.log("Nearest point:", nearest);