Struct lib::tree::AVLTree [] [src]

pub struct AVLTree<K: Ord + Copy, D> {
    pub root: Option<Box<Node<K, D>>>,
}

Fields

root

Methods

impl<K: Ord + Copy, D> AVLTree<K, D>

fn new() -> AVLTree<K, D>

This function will construct a new empty AVLTree.

Examples

extern crate avl_tree;
let mut t=avl_tree::AVLTree::<u64,i32>::new();

fn insert(&mut self, key: K, data: D)

This function will insert the key,value pair into the tree, overwriting the old data if the key is allready part of the tree.

Examples

let mut t=avl_tree::AVLTree::<u64,i32>::new();
t.insert(2,25);
assert_eq!(t.get(2), Some(&25));
t.insert(2,30);
assert_eq!(t.get(2), Some(&30));

fn delete(&mut self, key: K)

This function will remove the key,value pair from the tree, doing nothing if the key is not part of the tree.

Examples

let mut t=avl_tree::AVLTree::<u64,i32>::new();
t.insert(2,25);
t.delete(2);
assert!(t.empty());
t.delete(3);
assert!(t.empty());

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

This function will return the Some(data) stored under the given key or None if the key is not known.

Examples

let mut t=avl_tree::AVLTree::<u64,i32>::new();
t.insert(2,25);
assert_eq!(t.get(2), Some(&25));
assert_eq!(t.get(3), None);

fn get_or<'a>(&'a self, key: K, default: &'a D) -> &D

This function will return the data stored under the given key or the default if the key is not known.

Examples

let mut t=avl_tree::AVLTree::<u64,i32>::new();
t.insert(2,25);
assert_eq!(t.get_or(2,&2000), &25);
assert_eq!(t.get_or(3,&2000), &2000);

fn contains(&self, key: K) -> bool

This function will return true if the tree contains the given key, false otherwise

Examples

let mut t=avl_tree::AVLTree::<u64,i32>::new();
t.insert(2,25);
assert!(!t.contains(3));
assert!(t.contains(2));

fn empty(&self) -> bool

This function will return true if the tree is empty, false otherwise.

Examples

let mut t=avl_tree::AVLTree::<u64,i32>::new();
assert!(t.empty());
t.insert(2,25);
assert!(!t.empty());

fn min<'a>(&'a self) -> Option<(&'a K, &'a D)>

This function will return the key/value pair with the smallest key in the tree, or None if the tree is empty.

Examples

let mut t=avl_tree::AVLTree::<u64,i32>::new();
t.insert(2,25);
t.insert(3,50);
assert_eq!(t.min().unwrap().0, &2);
assert_eq!(t.min().unwrap().1, &25);

fn max<'a>(&'a self) -> Option<(&'a K, &'a D)>

This function will return the key/value pair with the biggest key in the tree, or None if the tree is empty.

Examples

let mut t=avl_tree::AVLTree::<u64,i32>::new();
t.insert(2,25);
t.insert(3,50);
assert_eq!(t.max().unwrap().0, &3);
assert_eq!(t.max().unwrap().1, &50);

fn iter(&self) -> RangePairIter<K, D>

This function will return a read only iterator for all (key,value) pairs in the tree.

Examples

for (key,val) in t.iter() {
    println!("{} -> {}",key,val)
}

fn range(&self, min: Bound<K>, max: Bound<K>) -> RangePairIter<K, D>

This function will return a read only iterator for all (key,value) pairs between the two bounds (which can be inclusive, exclusive or unbounded).

Examples

#![feature(collections_bound)]
use std::collections::Bound;
//[...]
for (key,val) in t.range(Bound::Excluded(32), Bound::Excluded(38)) {
    println!("{} -> {}",key,val)
}