Skip to content

Commit 308803e

Browse files
feat(docs): document AVL trees (#89)
1 parent ee8695a commit 308803e

File tree

1 file changed

+167
-40
lines changed

1 file changed

+167
-40
lines changed

src/data_structures/avl_tree.rs

Lines changed: 167 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,25 @@ struct AVLNode<T: Ord> {
1313
right: Option<Box<AVLNode<T>>>,
1414
}
1515

16-
/// A set based on an AVL Tree.
16+
/// An AVL tree.
1717
///
18-
/// An AVL Tree is a self-balancing binary search tree. It tracks the height of each node
19-
/// and performs internal rotations to maintain a height difference of at most 1 between
20-
/// each sibling pair.
18+
/// An AVL tree is a self-balancing binary search tree where the height difference between the left
19+
/// and right subtrees of each node is at most 1. This guarantees a logarithmic height and
20+
/// efficient operations.
21+
///
22+
/// # Examples
23+
///
24+
/// ```rust
25+
/// use rust_algorithms::data_structures::AVLTree;
26+
///
27+
/// let mut tree = AVLTree::new();
28+
/// tree.insert(1);
29+
/// tree.insert(2);
30+
///
31+
/// assert!(tree.contains(&1));
32+
/// assert!(tree.contains(&2));
33+
/// assert!(!tree.contains(&3));
34+
/// ```
2135
pub struct AVLTree<T: Ord> {
2236
root: Option<Box<AVLNode<T>>>,
2337
length: usize,
@@ -32,14 +46,46 @@ enum Side {
3246

3347
impl<T: Ord> AVLTree<T> {
3448
/// Creates an empty `AVLTree`.
49+
///
50+
/// # Examples
51+
///
52+
/// ```rust
53+
/// use rust_algorithms::data_structures::AVLTree;
54+
///
55+
/// let tree: AVLTree<i32> = AVLTree::new();
56+
///
57+
/// assert!(tree.is_empty());
58+
/// ```
3559
pub fn new() -> AVLTree<T> {
3660
AVLTree {
3761
root: None,
3862
length: 0,
3963
}
4064
}
4165

42-
/// Returns `true` if the tree contains a value.
66+
/// Checks if the tree contains a value.
67+
///
68+
/// # Arguments
69+
///
70+
/// * `value`: A reference to the value to check for.
71+
///
72+
/// # Returns
73+
///
74+
/// `true` if the tree contains the value, `false` otherwise.
75+
///
76+
/// # Examples
77+
///
78+
/// ```rust
79+
/// use rust_algorithms::data_structures::AVLTree;
80+
///
81+
/// let mut tree = AVLTree::new();
82+
/// tree.insert(1);
83+
///
84+
/// // The tree should contain the value 1.
85+
/// assert!(tree.contains(&1));
86+
/// // The tree should not contain the value 2.
87+
/// assert!(!tree.contains(&2));
88+
/// ```
4389
pub fn contains(&self, value: &T) -> bool {
4490
let mut current = &self.root;
4591
while let Some(node) = current {
@@ -54,7 +100,22 @@ impl<T: Ord> AVLTree<T> {
54100

55101
/// Adds a value to the tree.
56102
///
57-
/// Returns `true` if the tree did not yet contain the value.
103+
/// # Returns
104+
///
105+
/// `true` if the tree did not yet contain the value, `false` otherwise.
106+
///
107+
/// # Examples
108+
///
109+
/// ```rust
110+
/// use rust_algorithms::data_structures::AVLTree;
111+
///
112+
/// let mut tree = AVLTree::new();
113+
///
114+
/// // The first insertion should succeed.
115+
/// assert!(tree.insert(1));
116+
/// // The second insertion should fail, since the value is already in the tree.
117+
/// assert!(!tree.insert(1));
118+
/// ```
58119
pub fn insert(&mut self, value: T) -> bool {
59120
let inserted = insert(&mut self.root, value);
60121
if inserted {
@@ -65,7 +126,23 @@ impl<T: Ord> AVLTree<T> {
65126

66127
/// Removes a value from the tree.
67128
///
68-
/// Returns `true` if the tree contained the value.
129+
/// # Returns
130+
///
131+
/// `true` if the tree contained the value, `false` otherwise.
132+
///
133+
/// # Examples
134+
///
135+
/// ```rust
136+
/// use rust_algorithms::data_structures::AVLTree;
137+
///
138+
/// let mut tree = AVLTree::new();
139+
/// tree.insert(1);
140+
///
141+
/// // First removal should succeed, since the value is in the tree.
142+
/// assert!(tree.remove(&1));
143+
/// // The second removal should fail, since the value is no longer in the tree.
144+
/// assert!(!tree.remove(&1));
145+
/// ```
69146
pub fn remove(&mut self, value: &T) -> bool {
70147
let removed = remove(&mut self.root, value);
71148
if removed {
@@ -75,11 +152,39 @@ impl<T: Ord> AVLTree<T> {
75152
}
76153

77154
/// Returns the number of values in the tree.
155+
///
156+
/// # Returns
157+
///
158+
/// The number of values in the tree.
159+
///
160+
/// # Examples
161+
///
162+
/// ```rust
163+
/// use rust_algorithms::data_structures::AVLTree;
164+
///
165+
/// let tree: AVLTree<_> = (1..4).collect();
166+
///
167+
/// assert_eq!(tree.len(), 3);
168+
/// ```
78169
pub fn len(&self) -> usize {
79170
self.length
80171
}
81172

82-
/// Returns `true` if the tree contains no values.
173+
/// Detects if the AVL Tree is empty.
174+
///
175+
/// # Returns
176+
///
177+
/// `true` if the tree contains no values, `false` otherwise.
178+
///
179+
/// # Examples
180+
///
181+
/// ```rust
182+
/// use rust_algorithms::data_structures::AVLTree;
183+
///
184+
/// let tree: AVLTree<i32> = AVLTree::new();
185+
///
186+
/// assert!(tree.is_empty());
187+
/// ```
83188
pub fn is_empty(&self) -> bool {
84189
self.length == 0
85190
}
@@ -99,7 +204,21 @@ impl<T: Ord> AVLTree<T> {
99204
node_iter
100205
}
101206

102-
/// Returns an iterator that visits the values in the tree in ascending order.
207+
/// Gets an iterator that visits the values in the tree in ascending order.
208+
///
209+
/// # Returns
210+
///
211+
/// An iterator that visits the values in the tree in ascending order.
212+
///
213+
/// # Examples
214+
///
215+
/// ```rust
216+
/// use rust_algorithms::data_structures::AVLTree;
217+
///
218+
/// let tree: AVLTree<_> = (1..4).collect();
219+
///
220+
/// assert_eq!(tree.iter().collect::<Vec<_>>(), vec![&1, &2, &3]);
221+
/// ```
103222
pub fn iter(&self) -> Iter<T> {
104223
Iter {
105224
node_iter: self.node_iter(),
@@ -251,7 +370,21 @@ impl<T: Ord> AVLNode<T> {
251370
}
252371
}
253372

373+
/// Default implementation for `AVLTree`.
374+
///
375+
/// Creates an empty `AVLTree`.
254376
impl<T: Ord> Default for AVLTree<T> {
377+
/// Creates an empty `AVLTree`.
378+
///
379+
/// # Examples
380+
///
381+
/// ```rust
382+
/// use rust_algorithms::data_structures::AVLTree;
383+
///
384+
/// let tree: AVLTree<i32> = Default::default();
385+
///
386+
/// assert!(tree.is_empty());
387+
/// ```
255388
fn default() -> Self {
256389
Self::new()
257390
}
@@ -285,6 +418,9 @@ struct NodeIter<'a, T: Ord> {
285418
stack: Vec<&'a AVLNode<T>>,
286419
}
287420

421+
/// An iterator over the nodes of an `AVLTree`.
422+
///
423+
/// This struct is created by the `node_iter` method of `AVLTree`.
288424
impl<'a, T: Ord> Iterator for NodeIter<'a, T> {
289425
type Item = &'a AVLNode<T>;
290426

@@ -310,9 +446,31 @@ pub struct Iter<'a, T: Ord> {
310446
node_iter: NodeIter<'a, T>,
311447
}
312448

449+
/// An iterator over the items of an `AVLTree`.
450+
///
451+
/// This struct is created by the `iter` method of `AVLTree`.
313452
impl<'a, T: Ord> Iterator for Iter<'a, T> {
314453
type Item = &'a T;
315454

455+
/// Returns the next value in the tree.
456+
///
457+
/// # Returns
458+
///
459+
/// The next value in the tree, or `None` if there are no more values.
460+
///
461+
/// # Examples
462+
///
463+
/// ```rust
464+
/// use rust_algorithms::data_structures::AVLTree;
465+
///
466+
/// let tree: AVLTree<_> = (1..4).collect();
467+
/// let mut iter = tree.iter();
468+
///
469+
/// assert_eq!(iter.next(), Some(&1));
470+
/// assert_eq!(iter.next(), Some(&2));
471+
/// assert_eq!(iter.next(), Some(&3));
472+
/// assert_eq!(iter.next(), None);
473+
/// ```
316474
fn next(&mut self) -> Option<&'a T> {
317475
match self.node_iter.next() {
318476
Some(node) => Some(&node.value),
@@ -331,37 +489,6 @@ mod tests {
331489
.all(|n| (-1..=1).contains(&n.balance_factor()))
332490
}
333491

334-
#[test]
335-
fn len() {
336-
let tree: AVLTree<_> = (1..4).collect();
337-
assert_eq!(tree.len(), 3);
338-
}
339-
340-
#[test]
341-
fn contains() {
342-
let tree: AVLTree<_> = (1..4).collect();
343-
assert!(tree.contains(&1));
344-
assert!(!tree.contains(&4));
345-
}
346-
347-
#[test]
348-
fn insert() {
349-
let mut tree = AVLTree::new();
350-
// First insert succeeds
351-
assert!(tree.insert(1));
352-
// Second insert fails
353-
assert!(!tree.insert(1));
354-
}
355-
356-
#[test]
357-
fn remove() {
358-
let mut tree: AVLTree<_> = (1..8).collect();
359-
// First remove succeeds
360-
assert!(tree.remove(&4));
361-
// Second remove fails
362-
assert!(!tree.remove(&4));
363-
}
364-
365492
#[test]
366493
fn sorted() {
367494
let tree: AVLTree<_> = (1..8).rev().collect();

0 commit comments

Comments
 (0)