Struct BKTree

Source
pub struct BKTree<K, M = Levenshtein> { /* private fields */ }
Expand description

A representation of a BK-tree.

Implementations§

Source§

impl<K, M> BKTree<K, M>
where M: Metric<K>,

Source

pub fn new(metric: M) -> BKTree<K, M>

Constructs a new BKTree<K> using the provided metric.

Note that we make no assumptions about the metric function provided. Ideally it is actually a valid metric, but you may choose to use one that is not technically a valid metric. If you do not use a valid metric, however, you may find that the tree behaves confusingly for some values.

§Examples
use bk_tree::{BKTree, metrics};

let tree: BKTree<&str> = BKTree::new(metrics::Levenshtein);
Source

pub fn add(&mut self, key: K)

Adds a key to the tree.

If the tree is empty, this simply sets the root to Some(BKNode::new(key)). Otherwise, we iterate downwards through the tree until we see a node that does not have a child with the same distance. If we encounter a node that is exactly the same distance from the root node, then the new key is the same as that node’s key and so we do nothing. Note: This means that if your metric allows for unequal keys to return 0, you will see improper behavior!

§Examples
use bk_tree::{BKTree, metrics};

let mut tree: BKTree<&str> = BKTree::new(metrics::Levenshtein);

tree.add("foo");
tree.add("bar");
Source

pub fn find<'a, 'q, Q: ?Sized>( &'a self, key: &'q Q, tolerance: u32, ) -> Find<'a, 'q, K, Q, M>
where K: Borrow<Q>, M: Metric<Q>,

Searches for a key in the BK-tree given a certain tolerance.

This traverses the tree searching for all keys with distance within tolerance of of the key provided. The tolerance may be zero, in which case this searches for exact matches. The results are returned as an iterator of (distance, key) pairs.

Note: There is no guarantee on the order of elements yielded by the iterator. The elements returned may or may not be sorted in terms of distance from the provided key.

§Examples
use bk_tree::{BKTree, metrics};

let mut tree: BKTree<&str> = BKTree::new(metrics::Levenshtein);

tree.add("foo");
tree.add("fop");
tree.add("bar");

assert_eq!(tree.find("foo", 0).collect::<Vec<_>>(), vec![(0, &"foo")]);
assert_eq!(tree.find("foo", 1).collect::<Vec<_>>(), vec![(0, &"foo"), (1, &"fop")]);
assert!(tree.find("foz", 0).next().is_none());
Source

pub fn find_exact<Q: ?Sized>(&self, key: &Q) -> Option<&K>
where K: Borrow<Q>, M: Metric<Q>,

Searches for an exact match in the tree.

This is equivalent to calling find with a tolerance of 0, then picking out the first result.

§Examples
use bk_tree::{BKTree, metrics};

let mut tree: BKTree<&str> = BKTree::new(metrics::Levenshtein);

tree.add("foo");
tree.add("fop");
tree.add("bar");

assert_eq!(tree.find_exact("foz"), None);
assert_eq!(tree.find_exact("foo"), Some(&"foo"));

Trait Implementations§

Source§

impl<K: AsRef<str>> Default for BKTree<K>

Source§

fn default() -> BKTree<K>

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

impl<K, M: Metric<K>> Extend<K> for BKTree<K, M>

Source§

fn extend<I: IntoIterator<Item = K>>(&mut self, keys: I)

Adds multiple keys to the tree.

Given an iterator with items of type K, this method simply adds every item to the tree.

§Examples
use bk_tree::{BKTree, metrics};

let mut tree: BKTree<&str> = BKTree::new(metrics::Levenshtein);

tree.extend(vec!["foo", "bar"]);
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations§

§

impl<K, M> Freeze for BKTree<K, M>
where M: Freeze, K: Freeze,

§

impl<K, M> RefUnwindSafe for BKTree<K, M>

§

impl<K, M> Send for BKTree<K, M>
where M: Send, K: Send,

§

impl<K, M> Sync for BKTree<K, M>
where M: Sync, K: Sync,

§

impl<K, M> Unpin for BKTree<K, M>
where M: Unpin, K: Unpin,

§

impl<K, M> UnwindSafe for BKTree<K, M>
where M: UnwindSafe, K: 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.