class Containers::KDTree

A kd-tree is a binary tree that allows one to store points (of any space dimension: 2D, 3D, etc). The structure of the resulting tree makes it so that large portions of the tree are pruned during queries.

One very good use of the tree is to allow nearest neighbor searching. Let's say you have a number of points in 2D space, and you want to find the nearest 2 points from a specific point:

First, put the points into the tree:

kdtree = Containers::KDTree.new( {0 => [4, 3], 1 => [3, 4], 2 => [-1, 2], 3 => [6, 4],
                                 4 => [3, -5], 5 => [-2, -5] })

Then, query on the tree:

puts kd.find_nearest([0, 0], 2) => [[5, 2], [9, 1]]

The result is an array of [distance, id] pairs. There seems to be a bug in this version.

Note that the point queried on does not have to exist in the tree. However, if it does exist, it will be returned.

Constants

Node

Public Class Methods

new(points) click to toggle source

Points is a hash of id => [coord, coord] pairs.

# File lib/containers/kd_tree.rb, line 30
def initialize(points)
  raise "must pass in a hash" unless points.kind_of?(Hash)
  @dimensions = points[ points.keys.first ].size
  @root = build_tree(points.to_a)
  @nearest = []
end

Public Instance Methods

find_nearest(target, k_nearest) click to toggle source

Find k closest points to given coordinates

# File lib/containers/kd_tree.rb, line 38
def find_nearest(target, k_nearest)
  @nearest = []
  nearest(@root, target, k_nearest, 0)
end

Private Instance Methods

build_tree(points, depth=0) click to toggle source

points is an array

# File lib/containers/kd_tree.rb, line 44
def build_tree(points, depth=0)
  return if points.empty?
  
  axis = depth % @dimensions
  
  points.sort! { |a, b| a.last[axis] <=> b.last[axis] }
  median = points.size / 2
  
  node = Node.new(points[median].first, points[median].last, nil, nil)
  node.left = build_tree(points[0...median], depth+1)
  node.right = build_tree(points[median+1..-1], depth+1)
  node
end
check_nearest(nearest, node, target, k_nearest) click to toggle source

Update array of nearest elements if necessary

# File lib/containers/kd_tree.rb, line 69
def check_nearest(nearest, node, target, k_nearest)
  d = distance2(node, target) 
  if nearest.size < k_nearest || d < nearest.last[0]
    nearest.pop if nearest.size >= k_nearest
    nearest << [d, node.id]
    nearest.sort! { |a, b| a[0] <=> b[0] }
  end
  nearest
end
distance2(node, target) click to toggle source

Euclidian distanced, squared, between a node and target coords

# File lib/containers/kd_tree.rb, line 60
def distance2(node, target)
  return nil if node.nil? or target.nil?
  c = (node.coords[0] - target[0])
  d = (node.coords[1] - target[1])
  c * c + d * d
end
nearest(node, target, k_nearest, depth) click to toggle source

Recursively find nearest coordinates, going down the appropriate branch as needed

# File lib/containers/kd_tree.rb, line 81
def nearest(node, target, k_nearest, depth)
  axis = depth % @dimensions

  if node.left.nil? && node.right.nil? # Leaf node
    @nearest = check_nearest(@nearest, node, target, k_nearest)
    return
  end

  # Go down the nearest split
  if node.right.nil? || (node.left && target[axis] <= node.coords[axis])
    nearer = node.left
    further = node.right
  else
    nearer = node.right
    further = node.left
  end
  nearest(nearer, target, k_nearest, depth+1)

  # See if we have to check other side
  if further
    if @nearest.size < k_nearest || (target[axis] - node.coords[axis])**2 < @nearest.last[0]
      nearest(further, target, k_nearest, depth+1)
    end
  end

  @nearest = check_nearest(@nearest, node, target, k_nearest)
end