1673290020

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

Add this to your application's `shard.yml`

:

```
dependencies:
kd_tree:
github: geocrystal/kd_tree
```

```
require "kd_tree"
```

Construct a new tree. Each point should be of the form `[x, y]`

, where `x`

and `y`

are numbers(`Int32`

, `Float64`

, etc):

```
kd = Kd::Tree(Int32).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)
```

```
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(Float64).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]])
```

Using a tree with 1 million points `[x, y] of Float64`

on my i7-8550U CPU @ 1.80GHz:

```
build(init) ~10 seconds
nearest point 00.000278579
nearest point 5 00.000693038
nearest point 50 00.007207470
nearest point 255 00.134533902
nearest point 999 08.510465131
```

- Fork it (https://github.com/geocrystal/kd_tree/fork)
- Create your feature branch (
`git checkout -b my-new-feature`

) - Commit your changes (
`git commit -am 'Add some feature'`

) - Push to the branch (
`git push origin my-new-feature`

) - Create a new Pull Request

- mamantoha Anton Maminov - creator, maintainer

Author: Geocrystal

Source Code: https://github.com/geocrystal/kd_tree

License: MIT license

#crystal #datastructures #tree #structure

1673290020

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

Add this to your application's `shard.yml`

:

```
dependencies:
kd_tree:
github: geocrystal/kd_tree
```

```
require "kd_tree"
```

Construct a new tree. Each point should be of the form `[x, y]`

, where `x`

and `y`

are numbers(`Int32`

, `Float64`

, etc):

```
kd = Kd::Tree(Int32).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)
```

```
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(Float64).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]])
```

Using a tree with 1 million points `[x, y] of Float64`

on my i7-8550U CPU @ 1.80GHz:

```
build(init) ~10 seconds
nearest point 00.000278579
nearest point 5 00.000693038
nearest point 50 00.007207470
nearest point 255 00.134533902
nearest point 999 08.510465131
```

- Fork it (https://github.com/geocrystal/kd_tree/fork)
- Create your feature branch (
`git checkout -b my-new-feature`

) - Commit your changes (
`git commit -am 'Add some feature'`

) - Push to the branch (
`git push origin my-new-feature`

) - Create a new Pull Request

- mamantoha Anton Maminov - creator, maintainer

Author: Geocrystal

Source Code: https://github.com/geocrystal/kd_tree

License: MIT license