Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
Ruby k-d tree
=============
# Ruby k-d tree

Ruby implementation of "K-Dimensional Tree" and "N-Nearest Neighbors" based on [http://en.wikipedia.org/wiki/Kd-tree](http://en.wikipedia.org/wiki/Kd-tree).

##API
#####Initialize
## API

### Initialize

Build a new tree on a set of (2 or higher dimensional) coordinates:

```ruby
points = [[2,3], [5,4], [9,6], [4,7], [8,1], [7,2]]
kdtree = KDTree.new points
```

#####Flatten
### Flatten

Get an inorder representation as an array

```ruby
kdtree.to_a
=> [[2, 3], [5, 4], [4, 7], [7, 2], [8, 1], [9, 6]]
```
#####Print

### Print

```ruby
kdtree.print
=>
Expand All @@ -29,16 +34,23 @@ kdtree.print
[5, 4]
[2, 3]
```
#####Nearest Neighbors

### Nearest Neighbors

Current implementation uses squared euclidean as distance metric.
That is, for any 2 points a and b in D-dimensional space:<br>
That is, for any 2 points a and b in D-dimensional space:

![equation](http://latex.codecogs.com/gif.latex?%5Csum_%7Bd%3D1%7D%5E%7BD%7D%28a_d-b_d%29%5E2)
#####1-NN

### 1-NN

```ruby
kdtree.nnearest([1,1])
=> [[2, 3]]
```
#####k-NN

### k-NN

```ruby
kdtree.nnearest([1,1],3)
=> [[7, 2], [5, 4], [2, 3]]
Expand Down