k-d Tree JavaScript Library

1.0.3 · abandoned · verified Sun Apr 19

The `kd-tree-javascript` library provides a basic but high-performance JavaScript implementation of the k-dimensional tree data structure. It's designed for organizing points in k-dimensional space, facilitating efficient range searches and nearest neighbor queries. Currently at version 1.0.3, the library uses a UMD (Universal Module Definition) pattern, allowing it to be used in browser environments (exposing global variables `kdTree` and `BinaryHeap`) and with module loaders like RequireJS. Its primary differentiator was its reported speed and simplicity for specific spatial data operations, as highlighted by various demos. Due to its last update being in 2017, the library is considered abandoned, meaning no further feature development, bug fixes, or security patches are expected.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a k-d tree, insert points, define a distance function, and perform a nearest neighbor search.

var points = [
  {x: 1, y: 2},
  {x: 3, y: 4},
  {x: 5, y: 6},
  {x: 7, y: 8}
];

var distance = function(a, b){
  return Math.pow(a.x - b.x, 2) +  Math.pow(a.y - b.y, 2);
}

// Assuming kdTree is globally available or imported via CommonJS/AMD as 'ubilabs'
// For global: new kdTree(points, distance, ["x", "y"]);
// For CommonJS: new ubilabs.kdTree(points, distance, ["x", "y"]);
// For this example, we'll assume global/browser context as in README.
var tree = new kdTree(points, distance, ["x", "y"]);

var nearest = tree.nearest({ x: 5, y: 5 }, 2);

console.log(nearest);
// Expected output: 
// [ [ { x: 5, y: 6 }, 1 ], [ { x: 7, y: 8 }, 8 ] ]
// (point {x:5,y:6} at distance 1, point {x:7,y:8} at distance 8)

view raw JSON →