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.

[src]

impl<K: Ord, V> RBTree<K, V>
[src]

[src]

Creates an empty RBTree.

[src]

Returns the len of RBTree.

[src]

Returns true if the RBTree is empty.

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

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

Return the keys iter

[src]

Return the value iter

[src]

Return the value iter mut

[src]

Return the key and value iter

[src]

Return the key and mut value iter

Trait Implementations

impl<K: Ord, V> Drop for RBTree<K, V>
[src]

[src]

Executes the destructor for this type. Read more

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.

[src]

Returns a copy of the value. Read more

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]

[src]

Formats the value using the given formatter.

impl<K, V> PartialEq for RBTree<K, V> where
    K: Eq + Ord,
    V: PartialEq
[src]

all key be same, but it has multi key, if has multi key, it perhaps no correct

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

1.0.0
[src]

This method tests for !=.

impl<K, V> Eq for RBTree<K, V> where
    K: Eq + Ord,
    V: Eq
[src]

impl<'a, K, V> Index<&'a K> for RBTree<K, V> where
    K: Ord
[src]

The returned type after indexing.

[src]

Performs the indexing (container[index]) operation.

impl<K: Ord, V> FromIterator<(K, V)> for RBTree<K, V>
[src]

[src]

Creates a value from an iterator. Read more

impl<K: Ord, V> Extend<(K, V)> for RBTree<K, V>
[src]

RBTree into iter

[src]

Extends a collection with the contents of an iterator. Read more

impl<K: Ord, V> IntoIterator for RBTree<K, V>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more