Skip to content

Commit 013751f

Browse files
committed
KDTree docs
1 parent 856a421 commit 013751f

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

README.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Done so far:
3131
* Splay Trees Containers::SplayTreeMap, Containers::CSplayTreeMap (C extension), Containers::RubySplayTreeMap
3232
* Tries Containers::Trie
3333
* Suffix Array Containers::SuffixArray
34+
* kd Tree Containers::KDTree
3435

3536
* Search algorithms
3637
- Binary Search Algorithms::Search.binary_search

lib/algorithms.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Splay Trees - Containers::SplayTreeMap
3030
* Tries - Containers::Trie
3131
* Suffix Array - Containers::SuffixArray
32+
* kd Tree - Containers::KDTree
3233
3334
* Search algorithms
3435
- Binary Search - Algorithms::Search.binary_search

lib/containers/kd_tree.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
=begin rdoc
22
3-
A kd-tree allows searching of points in multi-dimensional space, increasing
4-
efficiency for nearest-neighbor searching in particular.
3+
A kd-tree is a binary tree that allows one to store points (of any space dimension: 2D, 3D, etc).
4+
The structure of the resulting tree makes it so that large portions of the tree are pruned
5+
during queries.
6+
7+
One very good use of the tree is to allow nearest neighbor searching. Let's say you have a number
8+
of points in 2D space, and you want to find the nearest 2 points from a specific point:
9+
10+
First, put the points into the tree:
11+
12+
kd = Containers::KDTree.new([ [4, 3], [3, 4], [-1, 2], [6, 4], [3, -5], [-2, -5] ])
13+
14+
Then, query on the tree:
15+
16+
puts kd.find_nearest([0, 0], 2) => [[0, 6], [0, 3]]
17+
18+
Note that the point queried on does not have to exist in the tree. However, if it does exist,
19+
it will be returned.
520
621
=end
722

@@ -48,6 +63,7 @@ def check_nearest(nearest, node, target, k_nearest)
4863
end
4964
nearest
5065
end
66+
private :check_nearest
5167

5268
# Find k closest points to given coordinates
5369
def find_nearest(target, k_nearest)

0 commit comments

Comments
 (0)