kd_tree

An implementation of "K-Dimensional Tree" and "N-Nearest Neighbors" k-dimensional data-structures tree-structure priority-queue
0.1.0 released

Kd::Tree

Build Status GitHub release License

Crystal implementation of "K-Dimensional Tree" and "N-Nearest Neighbors" based on http://en.wikipedia.org/wiki/Kd-tree.

Installation

Add this to your application's shard.yml:

dependencies:
  kdtree:
    github: mamantoha/kd_tree

Usage

require "kd_tree"

Construct a new tree. Each point should be of the form [x, y], where x and y are floats:

kd = Kd::Tree.new(points)

Find the nearest point to [x, y]. Returns an array with one point:

kd.nearest([x, y])

Find the nearest k points to [x, y]. Returns an array of points:

kd.nearest([x, y], k)

Example

require "kd_tree"

points = [
  [2.0, 3.0],
  [5.0, 4.0],
  [4.0, 7.0],
  [7.0, 2.0],
  [8.0, 1.0],
  [9.0, 6.0],
]

kd = Kd::Tree.new(points)

kd.nearest([1.0, 1.0])
# => [[2.0, 3.0]])

kd_tree.nearest([1.0, 1.0], 2)
# => [[2.0, 3.0], [5.0, 4.0]])

Contributing

  1. Fork it (https://github.com/mamantoha/kd_tree/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • mamantoha Anton Maminov - creator, maintainer
kd_tree:
  github: mamantoha/kd_tree
  version: ~> 0.1.0
License MIT
Crystal 0.25.1

Authors

Dependencies 0

Development Dependencies 0

Dependents 0

Last synced .
search fire star recently