@@ -13,11 +13,25 @@ struct AVLNode<T: Ord> {
13
13
right : Option < Box < AVLNode < T > > > ,
14
14
}
15
15
16
- /// A set based on an AVL Tree .
16
+ /// An AVL tree .
17
17
///
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
+ /// ```
21
35
pub struct AVLTree < T : Ord > {
22
36
root : Option < Box < AVLNode < T > > > ,
23
37
length : usize ,
@@ -32,14 +46,46 @@ enum Side {
32
46
33
47
impl < T : Ord > AVLTree < T > {
34
48
/// 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
+ /// ```
35
59
pub fn new ( ) -> AVLTree < T > {
36
60
AVLTree {
37
61
root : None ,
38
62
length : 0 ,
39
63
}
40
64
}
41
65
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
+ /// ```
43
89
pub fn contains ( & self , value : & T ) -> bool {
44
90
let mut current = & self . root ;
45
91
while let Some ( node) = current {
@@ -54,7 +100,22 @@ impl<T: Ord> AVLTree<T> {
54
100
55
101
/// Adds a value to the tree.
56
102
///
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
+ /// ```
58
119
pub fn insert ( & mut self , value : T ) -> bool {
59
120
let inserted = insert ( & mut self . root , value) ;
60
121
if inserted {
@@ -65,7 +126,23 @@ impl<T: Ord> AVLTree<T> {
65
126
66
127
/// Removes a value from the tree.
67
128
///
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
+ /// ```
69
146
pub fn remove ( & mut self , value : & T ) -> bool {
70
147
let removed = remove ( & mut self . root , value) ;
71
148
if removed {
@@ -75,11 +152,39 @@ impl<T: Ord> AVLTree<T> {
75
152
}
76
153
77
154
/// 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
+ /// ```
78
169
pub fn len ( & self ) -> usize {
79
170
self . length
80
171
}
81
172
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
+ /// ```
83
188
pub fn is_empty ( & self ) -> bool {
84
189
self . length == 0
85
190
}
@@ -99,7 +204,21 @@ impl<T: Ord> AVLTree<T> {
99
204
node_iter
100
205
}
101
206
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
+ /// ```
103
222
pub fn iter ( & self ) -> Iter < T > {
104
223
Iter {
105
224
node_iter : self . node_iter ( ) ,
@@ -251,7 +370,21 @@ impl<T: Ord> AVLNode<T> {
251
370
}
252
371
}
253
372
373
+ /// Default implementation for `AVLTree`.
374
+ ///
375
+ /// Creates an empty `AVLTree`.
254
376
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
+ /// ```
255
388
fn default ( ) -> Self {
256
389
Self :: new ( )
257
390
}
@@ -285,6 +418,9 @@ struct NodeIter<'a, T: Ord> {
285
418
stack : Vec < & ' a AVLNode < T > > ,
286
419
}
287
420
421
+ /// An iterator over the nodes of an `AVLTree`.
422
+ ///
423
+ /// This struct is created by the `node_iter` method of `AVLTree`.
288
424
impl < ' a , T : Ord > Iterator for NodeIter < ' a , T > {
289
425
type Item = & ' a AVLNode < T > ;
290
426
@@ -310,9 +446,31 @@ pub struct Iter<'a, T: Ord> {
310
446
node_iter : NodeIter < ' a , T > ,
311
447
}
312
448
449
+ /// An iterator over the items of an `AVLTree`.
450
+ ///
451
+ /// This struct is created by the `iter` method of `AVLTree`.
313
452
impl < ' a , T : Ord > Iterator for Iter < ' a , T > {
314
453
type Item = & ' a T ;
315
454
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
+ /// ```
316
474
fn next ( & mut self ) -> Option < & ' a T > {
317
475
match self . node_iter . next ( ) {
318
476
Some ( node) => Some ( & node. value ) ,
@@ -331,37 +489,6 @@ mod tests {
331
489
. all ( |n| ( -1 ..=1 ) . contains ( & n. balance_factor ( ) ) )
332
490
}
333
491
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
-
365
492
#[ test]
366
493
fn sorted ( ) {
367
494
let tree: AVLTree < _ > = ( 1 ..8 ) . rev ( ) . collect ( ) ;
0 commit comments