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:
Operation | Time complexity |
---|---|
Insertion | O(log n) |
Deletion | O(log n) |
Lookup | O(log n) |
Implementations§
Source§impl<T> AVL<T>
impl<T> AVL<T>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of nodes in this AVL tree. This operation has a strict time complexity of O(1)
pub fn height(&self) -> usize
pub fn clear(&mut self)
pub fn levels(&self) -> impl Iterator<Item = impl Iterator<Item = Option<&T>>>
pub fn iter(&self) -> impl Iterator<Item = &T>
Sourcepub fn increasing(&self) -> impl Iterator<Item = &T>
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.
pub fn into_increasing(self) -> impl Iterator<Item = T>
pub fn into_decreasing(self) -> impl Iterator<Item = T>
pub fn decreasing(&self) -> impl Iterator<Item = &T>
pub fn is_empty(&self) -> bool
Source§impl<T: Ord> AVL<T>
impl<T: Ord> AVL<T>
pub fn insert(&mut self, val: T)
pub fn insert_distinct(&mut self, val: T) -> bool
pub fn delete(&mut self, val: &T) -> bool
pub fn union(self, other: Self) -> Self
pub fn contains(&self, target: &T) -> bool
pub fn max(&self) -> Option<&T>
Sourcepub fn min(&self) -> Option<&T>
pub fn min(&self) -> Option<&T>
traverses the binary search tree and returns the minimum value.