pub struct BinaryTree<K, V> { /* private fields */ }Expand description
A binary tree containing key-value pairs where the keys can be ordered.
It should be noted that for most applications, a std::collections::HashMap will offer
superior performance to that of a binary tree, since each node in the tree requires a heap
allocation (apart from the root). Hash maps also provided amortized-constant lookup times where
a binary tree gives O(log(n)).
For efficiency, the tree maintains a count of the number of elements inserted so that the
len and is_empty methods are constant-time complexity.
§Examples
This example shows how the binary tree functions much like a std::collections::HashMap, but
gives O(log(n)) lookup time for keys that are of an ordinal type.
use collect_me::tree::binary_tree::BinaryTree;
let mut tree = BinaryTree::new();
tree.insert(0, "John");
tree.insert(42, "Neo");
tree.insert(2, "Alice");
assert_eq!(tree.get(&0), Some(&"John"));
assert_eq!(tree.get(&42), Some(&"Neo"));
assert_eq!(tree.get(&2), Some(&"Alice"));Implementations§
Source§impl<K, V> BinaryTree<K, V>
impl<K, V> BinaryTree<K, V>
Source§impl<K, V> BinaryTree<K, V>where
K: PartialOrd + Eq,
impl<K, V> BinaryTree<K, V>where
K: PartialOrd + Eq,
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the BinaryTree.
Returns None if the key did not exist, otherwise updates
the value and returns Some with the old value.
§Note
Like with std::collections::HashMap the key does not get updated.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
Trait Implementations§
Source§impl<K: Clone, V: Clone> Clone for BinaryTree<K, V>
impl<K: Clone, V: Clone> Clone for BinaryTree<K, V>
Source§fn clone(&self) -> BinaryTree<K, V>
fn clone(&self) -> BinaryTree<K, V>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<K: Default, V: Default> Default for BinaryTree<K, V>
impl<K: Default, V: Default> Default for BinaryTree<K, V>
Source§fn default() -> BinaryTree<K, V>
fn default() -> BinaryTree<K, V>
impl<K: Eq, V: Eq> Eq for BinaryTree<K, V>
Source§impl<K, V> Index<&K> for BinaryTree<K, V>where
K: PartialOrd + Eq,
impl<K, V> Index<&K> for BinaryTree<K, V>where
K: PartialOrd + Eq,
Source§impl<K: PartialEq, V: PartialEq> PartialEq for BinaryTree<K, V>
impl<K: PartialEq, V: PartialEq> PartialEq for BinaryTree<K, V>
Source§fn eq(&self, other: &BinaryTree<K, V>) -> bool
fn eq(&self, other: &BinaryTree<K, V>) -> bool
self and other values to be equal, and is used by ==.