Struct AVL

Source
pub struct AVL<T> { /* private fields */ }
Expand description

§Description

An AVL tree is a self-balancing binary search tree that maintains a height difference of at most 1 between its left and right subtrees. This property ensures that the tree remains balanced, which in turn guarantees that the time complexities of insertion, deletion,lookup are all O(log(n)) Compared to normal binary search trees, AVL trees provide faster lookups and insertions, but slower deletions. Compared to heaps, AVL trees are more strictly balanced, which makes them faster for lookups, but slower for insertions and deletions.

§Time complexities

The following table shows the time complexities of various operations in an AVL tree:

OperationTime complexity
InsertionO(log n)
DeletionO(log n)
LookupO(log n)

Implementations§

Source§

impl<T> AVL<T>

Source

pub fn new() -> Self

Creates and returns a new AVL tree

Source

pub fn len(&self) -> usize

Returns the number of nodes in this AVL tree. This operation has a strict time complexity of O(1)

Source

pub fn height(&self) -> usize

Source

pub fn clear(&mut self)

Source

pub fn levels(&self) -> impl Iterator<Item = impl Iterator<Item = Option<&T>>>

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Source

pub fn increasing(&self) -> impl Iterator<Item = &T>

Returns an in-order traversal iterator over the elements in the binary tree. This ensures the returned values are in an increasing order according to their implementation of the Ord trait.

Although this implementation does not make the iterator lazy, that is, initializing this iterator uses time complexity of O(log(n)), it makes the average time complexity of next be amortized O(1) with worst case scenario of O(log(n)) and ratio of average case to worst case is 1: log(n). More generally speaking, this implementation performs better than other implementations and also uses no extra space.

Source

pub fn into_increasing(self) -> impl Iterator<Item = T>

Source

pub fn into_decreasing(self) -> impl Iterator<Item = T>

Source

pub fn decreasing(&self) -> impl Iterator<Item = &T>

Source

pub fn is_empty(&self) -> bool

Source§

impl<T: Ord> AVL<T>

Source

pub fn insert(&mut self, val: T)

Source

pub fn insert_distinct(&mut self, val: T) -> bool

Source

pub fn delete(&mut self, val: &T) -> bool

Source

pub fn union(self, other: Self) -> Self

Source

pub fn contains(&self, target: &T) -> bool

Source

pub fn max(&self) -> Option<&T>

Source

pub fn min(&self) -> Option<&T>

traverses the binary search tree and returns the minimum value.

Source

pub fn nearest_to<'a, F>(&'a self, target: &'a T, by: F) -> Option<&'a T>
where F: 'static + Fn(&'a T, &'a T) -> &'a T, T: 'a,

Source

pub fn farthest_to<'a, F>(&'a self, target: &'a T, by: F) -> Option<&'a T>
where F: 'static + Fn(&'a T, &'a T) -> &'a T, T: 'a,

Source

pub fn greater_than<'a>(&'a self, lower: &'a T) -> impl Iterator<Item = &'a T>

Source

pub fn less_than<'a>(&'a self, upper: &'a T) -> impl Iterator<Item = &'a T>

Source§

impl<T: Ord + Nearness> AVL<T>

Source

pub fn nearest<'a>(&'a self, target: &'a T) -> Option<&'a T>

Source

pub fn farthest<'a>(&'a self, target: &'a T) -> Option<&'a T>

Trait Implementations§

Source§

impl<T: Clone> Clone for AVL<T>

Source§

fn clone(&self) -> AVL<T>

Returns a copy 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<T: Debug> Debug for AVL<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Ord> FromIterator<T> for AVL<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> IntoIterator for AVL<T>

Source§

type IntoIter = IntoIter<T>

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

type Item = T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for AVL<T>

§

impl<T> RefUnwindSafe for AVL<T>
where T: RefUnwindSafe,

§

impl<T> Send for AVL<T>
where T: Send,

§

impl<T> Sync for AVL<T>
where T: Sync,

§

impl<T> Unpin for AVL<T>

§

impl<T> UnwindSafe for AVL<T>
where T: UnwindSafe,

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.