Struct rbtree::RBTree
[−]
[src]
pub struct RBTree<K: Ord, V> { /* fields omitted */ }
A red black tree implemented with Rust
It is required that the keys implement the [Ord
] traits.
Examples
use rbtree::RBTree; // type inference lets us omit an explicit type signature (which // would be `RBTree<&str, &str>` in this example). let mut book_reviews = RBTree::new(); // review some books. book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book."); book_reviews.insert("Grimms' Fairy Tales", "Masterpiece."); book_reviews.insert("Pride and Prejudice", "Very enjoyable."); book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot."); // check for a specific one. if !book_reviews.contains_key(&"Les Misérables") { println!( "We've got {} reviews, but Les Misérables ain't one.", book_reviews.len() ); } // oops, this review has a lot of spelling mistakes, let's delete it. book_reviews.remove(&"The Adventures of Sherlock Holmes"); // look up the values associated with some keys. let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"]; for book in &to_find { match book_reviews.get(book) { Some(review) => println!("{}: {}", book, review), None => println!("{} is unreviewed.", book), } } // iterate over everything. for (book, review) in book_reviews.iter() { println!("{}: \"{}\"", book, review); } book_reviews.print_tree();
// A RBTree
with fixed list of elements can be initialized from an array:
use rbtree::RBTree; let timber_resources: RBTree<&str, i32> = [("Norway", 100), ("Denmark", 50), ("Iceland", 10)] .iter().cloned().collect(); // use the values stored in rbtree
Methods
impl<K: Ord + Debug, V: Debug> RBTree<K, V>
[src]
This is a method to help us to get inner struct.
fn print_tree(&self)
[src]
impl<K: Ord, V> RBTree<K, V>
[src]
fn new() -> RBTree<K, V>
[src]
Creates an empty RBTree
.
fn len(&self) -> usize
[src]
Returns the len of RBTree
.
fn is_empty(&self) -> bool
[src]
Returns true
if the RBTree
is empty.
fn replace_or_insert(&mut self, k: K, v: V) -> Option<V>
[src]
replace value if key exist, if not exist insert it.
Examples
use rbtree::RBTree; let mut m = RBTree::new(); assert_eq!(m.len(), 0); m.insert(2, 4); assert_eq!(m.len(), 1); assert_eq!(m.replace_or_insert(2, 6).unwrap(), 4); assert_eq!(m.len(), 1); assert_eq!(*m.get(&2).unwrap(), 6);
fn insert(&mut self, k: K, v: V)
[src]
fn get_first(&self) -> Option<(&K, &V)>
[src]
fn get_last(&self) -> Option<(&K, &V)>
[src]
fn pop_first(&mut self) -> Option<(K, V)>
[src]
fn pop_last(&mut self) -> Option<(K, V)>
[src]
fn get_first_mut(&mut self) -> Option<(&K, &mut V)>
[src]
fn get_last_mut(&mut self) -> Option<(&K, &mut V)>
[src]
fn get(&self, k: &K) -> Option<&V>
[src]
fn get_mut(&mut self, k: &K) -> Option<&mut V>
[src]
fn contains_key(&self, k: &K) -> bool
[src]
fn clear(&mut self)
[src]
fn remove(&mut self, k: &K) -> Option<V>
[src]
fn keys(&self) -> Keys<K, V>
[src]
Return the keys iter
fn values(&self) -> Values<K, V>
[src]
Return the value iter
fn values_mut(&mut self) -> ValuesMut<K, V>
[src]
Return the value iter mut
fn iter(&self) -> Iter<K, V>
[src]
Return the key and value iter
fn iter_mut(&mut self) -> IterMut<K, V>
[src]
Return the key and mut value iter
Trait Implementations
impl<K: Ord, V> Drop for RBTree<K, V>
[src]
impl<K: Ord + Clone, V: Clone> Clone for RBTree<K, V>
[src]
If key and value are both impl Clone, we can call clone to get a copy.
fn clone(&self) -> RBTree<K, V>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<K, V> Debug for RBTree<K, V> where
K: Ord + Debug,
V: Debug,
[src]
K: Ord + Debug,
V: Debug,
impl<K, V> PartialEq for RBTree<K, V> where
K: Eq + Ord,
V: PartialEq,
[src]
K: Eq + Ord,
V: PartialEq,
all key be same, but it has multi key, if has multi key, it perhaps no correct
fn eq(&self, other: &RBTree<K, V>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<K, V> Eq for RBTree<K, V> where
K: Eq + Ord,
V: Eq,
[src]
K: Eq + Ord,
V: Eq,
impl<'a, K, V> Index<&'a K> for RBTree<K, V> where
K: Ord,
[src]
K: Ord,
type Output = V
The returned type after indexing.
fn index(&self, index: &K) -> &V
[src]
Performs the indexing (container[index]
) operation.
impl<K: Ord, V> FromIterator<(K, V)> for RBTree<K, V>
[src]
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> RBTree<K, V>
[src]
Creates a value from an iterator. Read more
impl<K: Ord, V> Extend<(K, V)> for RBTree<K, V>
[src]
RBTree into iter
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
[src]
Extends a collection with the contents of an iterator. Read more