https://github.com/tomusdrw/kdtree
K-d tree implementation for nearest neighbours searching.
https://github.com/tomusdrw/kdtree
Last synced: 11 months ago
JSON representation
K-d tree implementation for nearest neighbours searching.
- Host: GitHub
- URL: https://github.com/tomusdrw/kdtree
- Owner: tomusdrw
- Created: 2012-06-30T14:46:59.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2016-01-29T08:17:49.000Z (over 10 years ago)
- Last Synced: 2025-05-31T00:46:45.337Z (about 1 year ago)
- Language: JavaScript
- Size: 178 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
JavaScript [k-d tree](http://en.wikipedia.org/wiki/K-d_tree) implementation.
================================================
Supports:
* building balanced tree from predefined data
* performing search for any given number of nearest neighbours
See [examples of usage](http://examples.blacksoft.eu/blacksoft.kdtree/examples):
* [Nearest points on plane](http://examples.blacksoft.eu/blacksoft.kdtree/examples/NearestPoints.html)
* [Classification (knn method)](http://examples.blacksoft.eu/blacksoft.kdtree/examples/Classification.html)
See [test suite](http://examples.blacksoft.eu/blacksoft.kdtree/tests/kdtree.html).
You can tune distance metric (default is euclid) and provide bucket size. Example:
```javascript
var data = [
[1, 2, 3, {additional: "data"}],
[4, 5, 6, {additional: "data2"}]
];
var k = data[0].length - 1;
var options = {
/**
* Defines bucket (terminal node) maximal size (default: 10)
*/
bucketSize : 10,
/**
* Data dimension (default: data[0].length)
*/
k : k,
/**
* Dissimilarity function (default: sqrt)
*/
dissim : function(sum) {
return Math.sqrt(sum);
},
/**
* Coordinate distance (default: (a-b)^2)
*/
coordinateDistance : function(a, b) {
var d = a - b;
return d * d;
}
};
var tree = kdtree.buildTree(data, options);
var noOfNeighboursToFind = 2;
var neighbours = tree.search([1, 2, 3], noOfNeighboursToFind);
neighbours[0][k].additional === "data"; //true
neighbours[1][k].additional === "data2"; //true
```
Implementation is based on paper:
JH Friedman, JL Bentley, "An algorithm for finding best matches in logarithmic expected time.", 1977