AVL

Struct AVL 

Source
pub struct AVL<K: Ord, V> { /* private fields */ }
Expand description

An AVL balanced tree with owned nodes.

Implementations§

Source§

impl<K: Ord, V> AVL<K, V>

Source

pub fn new() -> Self

Create an empty AVL tree

§Examples
use ABtree::AVL;

let t = AVL::<i32, i32>::new();
Source

pub fn add(&mut self, k: K, v: V)

Adding key-value pair into the tree

§Example
use ABtree::AVL;
let mut t = AVL::<i32, i32>::new();
t.add(2, 3);
assert_eq!(t.len(), 1);
Source

pub fn insert(&mut self, k: K, v: V)

Adding key-value pair into the tree this method is an alias of method add

§Example
use ABtree::AVL;
let mut t = AVL::<i32, i32>::new();
t.insert(2, 3);
assert_eq!(t.len(), 1);
Source

pub fn set(&mut self, k: K, v: V)

Setting a key-value pair if the key exists it will update the value otherwise it will insert the key-value into the tree

§Example
use ABtree::AVL;
let mut t = AVL::<i32, i32>::new();
t.set(2, 2);
t.set(2, 31);
assert_eq!(t.get(&2), Some(&31));
Source

pub fn len(&self) -> usize

Get the length of this tree

§Example
use ABtree::AVL;
let mut t = AVL::<i32, i32>::new();
t.insert(2, 2);
t.insert(3, 3);
assert_eq!(t.len(), 2);
Source

pub fn iter<'a>(&'a self) -> Iter<'a, K, V>

Provides a forward iterator.

§Examples
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();

t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);

let mut iter = t.iter();
assert_eq!(iter.next(), Some((&0, &0)));
assert_eq!(iter.next(), Some((&1, &1)));
assert_eq!(iter.next_back(), Some((&2, &2)));
Source

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

Containment check

§Example
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();

t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert!(t.contains(&1));
Source

pub fn remove(&mut self, k: &K) -> Option<V>

Removing key-value pair

§Example
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();

t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.remove(&1), Some(1));
assert_eq!(t.len(), 2);
Source

pub fn peek_root<'a>(&'a self) -> Option<(&'a K, &'a V)>

Peeking the root node

§Example
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();

t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.peek_root(), Some((&1, &1)));
Source

pub fn is_balanced_tree(&self) -> bool

To check if shis tree is balanced

§Example
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();

t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.is_balanced_tree(), true);
Source

pub fn is_empty(&self) -> bool

To check if shis tree is empty

§Example
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();

t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.is_empty(), false);
Source

pub fn clear(&mut self)

Removes all elements from the AVL tree

§Example
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();

t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
t.clear();
assert_eq!(t.len(), 0);
Source

pub fn get(&self, k: &K) -> Option<&V>

Get the value by key

§Example
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();

t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.get(&1), Some(&1));
Source

pub fn get_mut(&mut self, k: &K) -> Option<&mut V>

Get a mutable reference of value by key

§Example
use ABtree::AVL;

let mut t: AVL<u32, u32> = AVL::new();
t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
let v = t.get_mut(&2);
v.map(|i| *i += 10);
assert_eq!(t.get(&2), Some(&12))

Trait Implementations§

Source§

impl<K: Ord + Copy, V: Copy> Clone for AVL<K, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Ord, V> Drop for AVL<K, V>

Drop

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<K: Ord, V> FromIterator<(K, V)> for AVL<K, V>

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<K: Ord, V> IntoIterator for AVL<K, V>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Ord + Send, V: Send> Send for AVL<K, V>

Source§

impl<K: Ord + Sync, V: Sync> Sync for AVL<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for AVL<K, V>

§

impl<K, V> RefUnwindSafe for AVL<K, V>

§

impl<K, V> Unpin for AVL<K, V>

§

impl<K, V> UnwindSafe for AVL<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.