Struct bk_tree::BKTree [] [src]

pub struct BKTree<K> where K: Clone {
    pub root: Option<BKNode<K>>,
    // some fields omitted
}

A representation of a BK-tree.

Fields

root: Option<BKNode<K>>

The root node. May be empty if nothing has been put in the tree yet.

Methods

impl<K> BKTree<K> where K: Clone
[src]

fn new<M: 'static>(metric: M) -> BKTree<K> where M: Fn(K, K) -> u64

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);

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");

fn find(&self, key: K, tolerance: u64) -> Vec<K>

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 in a Vec<K>.

Note: There is no guarantee on the order of the vector provided. 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), vec!["foo"]);
assert_eq!(tree.find("foo", 1), vec!["foo", "fop"]);
assert!(tree.find("foz", 0).is_empty());

fn find_exact(&self, key: K) -> Option<K>

Searches for an exact match in the tree.

This is pretty much the same as calling find with a tolerance of 0, with the addition of pulling the value out of the vector if there was a match.

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

impl<K: Clone> Extend<K> for BKTree<K>
[src]

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"]);

impl<K: 'static + Clone + ToString> Default for BKTree<K>
[src]

fn default() -> BKTree<K>

Returns the "default value" for a type. Read more