Struct ball_tree::BallTree

source ·
pub struct BallTree<P, V>(/* private fields */);
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§

source§

impl<P: Point, V> BallTree<P, V>

source

pub fn new(points: Vec<P>, values: Vec<V>) -> Self

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

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

source

pub fn query(&self) -> Query<'_, P, V>

Query this BallTree. The Query object provides a nearest-neighbor API and internally re-uses memory to avoid allocations on repeated queries.

Trait Implementations§

source§

impl<P: Clone, V: Clone> Clone for BallTree<P, V>

source§

fn clone(&self) -> BallTree<P, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<P: Debug, V: Debug> Debug for BallTree<P, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<P: Point, V> Default for BallTree<P, V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<P, V> Freeze for BallTree<P, V>
where P: Freeze,

§

impl<P, V> RefUnwindSafe for BallTree<P, V>

§

impl<P, V> Send for BallTree<P, V>
where P: Send, V: Send,

§

impl<P, V> Sync for BallTree<P, V>
where P: Sync, V: Sync,

§

impl<P, V> Unpin for BallTree<P, V>
where P: Unpin, V: Unpin,

§

impl<P, V> UnwindSafe for BallTree<P, V>
where P: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.