pub struct RBTree<K: Ord, V> { /* private fields */ }
Expand description
A red black tree implemented with Rust
It is required that the keys implement the Ord
traits.
§Examples
use algorithm::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 algorithm::RBTree;
let timber_resources: RBTree<&str, i32> =
[("Norway", 100),
("Denmark", 50),
("Iceland", 10)]
.iter().cloned().collect();
// use the values stored in rbtree
Implementations§
source§impl<K: Ord + Debug, V: Debug> RBTree<K, V>
impl<K: Ord + Debug, V: Debug> RBTree<K, V>
This is a method to help us to get inner struct.
pub fn print_tree(&self)
source§impl<K: Ord, V> RBTree<K, V>
impl<K: Ord, V> RBTree<K, V>
sourcepub fn replace_or_insert(&mut self, k: K, v: V) -> Option<V>
pub fn replace_or_insert(&mut self, k: K, v: V) -> Option<V>
replace value if key exist, if not exist insert it.
§Examples
use algorithm::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);
pub fn insert(&mut self, k: K, v: V)
pub fn get_first(&self) -> Option<(&K, &V)>
pub fn get_last(&self) -> Option<(&K, &V)>
pub fn pop_first(&mut self) -> Option<(K, V)>
pub fn pop_last(&mut self) -> Option<(K, V)>
pub fn get_first_mut(&mut self) -> Option<(&K, &mut V)>
pub fn get_last_mut(&mut self) -> Option<(&K, &mut V)>
pub fn get(&self, k: &K) -> Option<&V>
pub fn get_mut(&mut self, k: &K) -> Option<&mut V>
pub fn contains_key(&self, k: &K) -> bool
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
clear all red back tree elements.
§Examples
use algorithm::RBTree;
let mut m = RBTree::new();
for i in 0..6 {
m.insert(i, i);
}
assert_eq!(m.len(), 6);
m.clear();
assert_eq!(m.len(), 0);
pub fn remove(&mut self, k: &K) -> Option<V>
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
Return the value iter mut
pub fn drain(&mut self) -> Drain<'_, K, V>
Trait Implementations§
source§impl<K: Ord + Clone, V: Clone> Clone for RBTree<K, V>
impl<K: Ord + Clone, V: Clone> Clone for RBTree<K, V>
If key and value are both impl Clone, we can call clone to get a copy.
source§impl<K: Ord, V> Extend<(K, V)> for RBTree<K, V>
impl<K: Ord, V> Extend<(K, V)> for RBTree<K, V>
RBTree into iter
source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
source§fn extend_one(&mut self, item: A)
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)
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
source§impl<K: Ord, V> IntoIterator for RBTree<K, V>
impl<K: Ord, V> IntoIterator for RBTree<K, V>
source§impl<K, V> PartialEq for RBTree<K, V>
impl<K, V> PartialEq for RBTree<K, V>
all key be same, but it has multi key, if has multi key, it perhaps no correct
impl<K, V> Eq for RBTree<K, V>
impl<K: Ord, V> Send for RBTree<K, V>
impl<K: Ord, V> Sync for RBTree<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for RBTree<K, V>
impl<K, V> RefUnwindSafe for RBTree<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Unpin for RBTree<K, V>
impl<K, V> UnwindSafe for RBTree<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)