Struct ball_tree::BallTree

source ·
pub struct BallTree<P, V>(_);
Expand description

A BallTree is a space-partitioning data-structure that allows for finding nearest neighbors in logarithmic time.

It does this by partitioning data into a series of nested bounding spheres (“balls” in the literature). Spheres are used because it is trivial to compute the distance between a point and a sphere (distance to the sphere’s center minus thte radius). The key observation is that a potential neighbor is necessarily closer than all neighbors that are located inside of a bounding sphere that is farther than the aforementioned neighbor.

Graphically:


   A -  
   |  ----         distance(A, B) = 4
   |      - B      distance(A, S) = 6
    |       
     |
     |    S
       --------
     /        G \ 
    /   C        \
   |           D |
   |       F     |
    \ E         /
     \_________/

In the diagram, A is closer to B than to S, and because S bounds C, D, E, F, and G, it can be determined that A it is necessarily closer to B than the other points without even computing exact distances to them.

Ball trees are most commonly used as a form of predictive model where the points are features and each point is associated with a value or label. Thus, This implementation allows the user to associate a value with each point. If this functionality is unneeded, () can be used as a value.

This implementation returns the nearest neighbors, their distances, and their associated values. Returning the distances allows the user to perform some sort of weighted interpolation of the neighbors for predictive purposes.

Implementations

Construct this BallTree. Construction is somewhat expensive, so BallTrees are best constructed once and then used repeatedly.

panic if points.len() != values.len()

Given a point and a number of neigbors to look for, find the k nearest neighbors (or fewer, if the ball tree contains fewer than k points).

The neighbor, its distance, and associated value is returned.

We allow the caller to pass in a result-consuming closure rather than return a Vec to allow the caller to control allocation.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.