dsa_rust/hierarchies/
avl_tree.rs

1/*! A safe, arena-backed (indexed) AVL tree
2
3# About
4Adelson-Velsky and Landis (AVL) trees represent theoretically optimal balanced binary search trees. AVL trees guarantee _~1.44 * log(n)_ height, and provide _O(log(n))_ search, insert, and delete operations. Red-black trees tend to be more popular even though they only guarantee _<= 2 * log(n)_ height. The imperfect height is offset by the fact that red-black trees often require fewer rotations, and the average number of rebalance operations is fewer than with AVL trees.
5
6# Design
7The design uses a flat, [Vec]-backed structure with iterative (read non-recursive) navigation. This arena-allocated design provides robust, performant operations while keeping runtime checks to a minimum. The goal of this approach is to avoid unnecessary overhead with recursive operations and extra heap allocations, making it suitable for low-spec environments.
8
9The structure trades spatial efficiency for _O(1)_ insert operations. All "pointers" represent absolute positions (as indexes) and cannot be re-calculated in less than _O(n)_ time. Thus, `remove(key)` operations do not actually shrink the physical size of the structure, leaving a `None` "hole" in the removed node's place. The structure can only grow.
10
11Due to common usage when implementing sorted map and set structures, this implementation does not accept duplicate entries by default. The structure contains an internal `SearchResult` enum that allows for duplicates by way of `Ordering::Equal`, but it is yet implemented in this version.
12
13# Example
14```rust
15    use dsa_rust::hierarchies::avl_tree::AVLTree;
16
17    let mut tree: AVLTree<u8> = AVLTree::new();
18
19    // Create the following AVL tree
20    //
21    //           39
22    //          /  \
23    //        17    41
24    //       /  \     \
25    //     13   23     43
26    //     /   /  \
27    //    8   19  31
28    //
29    let v = [31, 13, 23, 39, 41, 43, 8, 17, 19];
30    for e in v.iter() {
31        tree.insert(*e);
32    }
33    assert_eq!(tree.get_root(), Some(&39)); // Prove that its properly rooted
34
35
36    // Remove 41 which results in the following restructure
37    //
38    //         17
39    //        /  \
40    //      13    39
41    //     /     /  \
42    //    8     23   43
43    //         /  \
44    //        19   31
45    //
46    assert!(tree.contains(&41)); // Prove that its here today
47    let removed = tree.remove(&41).unwrap();
48    assert_eq!(removed, 41);
49    assert!(!tree.contains(&41)); // ...and gone tomorrow
50
51    tree.insert(34);
52    tree.insert(67);
53    tree.insert(2);
54    tree.insert(4);
55    tree.insert(5);
56    tree.insert(1);
57
58    // The tree is still intact, and its root has shifted
59    assert_eq!(tree.get_root().unwrap(), &31);
60
61    // In-order "snapshot" iterator
62    let mut sorted = Vec::new();
63    for e in tree.iter() {
64        sorted.push(*e)
65    }
66    assert_eq!(sorted, [1, 2, 4, 5, 8, 13, 17, 19, 23, 31, 34, 39, 43, 67]);
67```
68*/
69
70use std::borrow::Borrow;
71use std::cmp::{max, Ordering};
72
73// Custom sum type for search algorithm
74enum SearchResult {
75    // The tree is empty (and uninitialized)
76    None,
77    // Index of a found key
78    Exists(usize),
79    // Index for insertion of a new key
80    Parent(usize),
81    //Parent { index: usize, side: Side },
82}
83
84//#[derive(PartialEq)]
85enum Side {
86    Left,
87    Right,
88}
89// NOTE: Implementing Not provides the ability to use the
90// logical negation operator !. Implementing a custom
91// opposite() does the same thing, but more explicitly.
92// The choice to implement Not for &Side instead of Side
93// allows the re-use of Side without making it Copy/Clone.
94// Example:
95//    let subtree = self.arena[child_idx].child(!side);
96//    let subtree = self.arena[child_idx].child(opposite(side));
97impl<'a> std::ops::Not for &'a Side {
98    type Output = &'a Side;
99    fn not(self) -> &'a Side {
100        match self {
101            Side::Left => &Side::Right,
102            Side::Right => &Side::Left,
103        }
104    }
105}
106fn opposite(side: &Side) -> &Side {
107    match side {
108        Side::Left => &Side::Right,
109        Side::Right => &Side::Left,
110    }
111}
112
113#[derive(Debug)]
114pub struct AVLNode<T> {
115    value: Option<T>,      // Option allows take() operations for efficient node removals
116    parent: Option<usize>, // None indicates the root of the tree
117    left: Option<usize>,
118    right: Option<usize>,
119    height: usize,
120}
121impl<T> AVLNode<T> {
122    // Creates a new node with its current index, a value, and its parent (if Some).
123    // Guarantees that all nodes have a value.
124    // All initial inserts are leafs before restructuring, so left and right are set to None.
125    fn new(value: T, parent: Option<usize>, height: usize) -> Self {
126        AVLNode {
127            value: Some(value),
128            parent,
129            left: None,
130            right: None,
131            height,
132        }
133    }
134
135    // Returns a reference to the node's value, if Some
136    fn get_value(&self) -> Option<&T> {
137        self.value.as_ref()
138    }
139
140    /// Get child index for a given side
141    fn child(&self, side: &Side) -> Option<usize> {
142        match side {
143            Side::Left => self.left,
144            Side::Right => self.right,
145        }
146    }
147
148    /// Set child index for a given side
149    fn set_child(&mut self, side: &Side, idx: Option<usize>) {
150        match side {
151            Side::Left => self.left = idx,
152            Side::Right => self.right = idx,
153        }
154    }
155}
156
157/// # About
158///
159/// See the [module-level documentation](crate::hierarchies::avl_tree) for more information.
160#[derive(Debug)]
161pub struct AVLTree<T> {
162    // Option wrapper for efficient take() operations during removal
163    // without incurring the wrath of O(n) resize ops
164    arena: Vec<Option<AVLNode<T>>>,
165    // Wont always be 0, and wont always be Some!
166    root: Option<usize>,
167}
168// Im just here to make Clippy happy
169impl<T> Default for AVLTree<T>
170where
171    T: Ord,
172{
173    fn default() -> Self {
174        Self::new()
175    }
176}
177impl<T> AVLTree<T>
178where
179    T: Ord,
180{
181    /// Creates a new, empty binary search tree.
182    pub fn new() -> Self {
183        AVLTree {
184            arena: Vec::new(),
185            root: None,
186        }
187    }
188
189    /// Creates a new, empty binary search tree with a given (growable) initial capacity.
190    pub fn new_with_capacity(size: usize) -> Self {
191        AVLTree {
192            arena: Vec::with_capacity(size),
193            root: Some(0),
194        }
195    }
196
197    /// Gets a reference to the value of a key, if Some.
198    pub fn get_node<Q>(&self, key: &Q) -> Option<&T>
199    where
200        Q: Ord + ?Sized,
201        //T::Key: std::borrow::Borrow<Q>,
202        T: Borrow<Q>,
203    {
204        match self.search(key) {
205            SearchResult::Exists(index) => self.node(index).get_value(),
206            _ => None,
207        }
208    }
209
210    pub fn get_root(&self) -> Option<&T> {
211        if let Some(node) = self.root {
212            self.node(node).get_value()
213        } else {
214            None
215        }
216    }
217
218    /// Returns true if the key exists in the tree.
219    pub fn contains<Q>(&self, key: &Q) -> bool
220    where
221        Q: Ord + ?Sized,
222        T: Borrow<Q>,
223    {
224        matches!(self.search(key), SearchResult::Exists(_))
225    }
226
227    /// Inserts the given key into the tree maintaining an AVL structure.
228    ///
229    /// NOTE: Does not handle duplicate keys by convention, but may overwrite values for
230    /// arbitrarily complex T with custom Ordering.
231    pub fn insert(&mut self, key: T)
232    where
233        T: Ord,
234    {
235        // Pass &T::Key to search; search itself is generic over Q
236        match self.search(&key) {
237            SearchResult::Parent(parent) => {
238                // Determine child position
239                let new_idx = self.arena.len();
240                // SAFETY: Parent is guaranteed by search() enums
241                //if let Some(val) = &self.arena[parent].as_mut().unwrap().value {
242                if let Some(val) = &self.node_mut(parent).value {
243                    match key.cmp(val) {
244                        Ordering::Less => {
245                            //self.arena[parent].as_mut().unwrap().left = Some(new_idx);
246                            self.node_mut(parent).left = Some(new_idx);
247                        }
248                        Ordering::Greater => {
249                            //self.arena[parent].as_mut().unwrap().right = Some(new_idx);
250                            self.node_mut(parent).right = Some(new_idx);
251                        }
252                        Ordering::Equal => {}
253                    }
254                };
255
256                // Insert new node
257                self.arena.push(Some(AVLNode::new(key, Some(parent), 1)));
258
259                // Walk up the tree to update heights and rebalance
260                let mut current = Some(parent);
261                while let Some(idx) = current {
262                    self.update_node_height(idx);
263                    self.restructure(idx);
264                    //current = self.arena[idx].as_mut().unwrap().parent;
265                    current = self.node_mut(idx).parent;
266                }
267            }
268            SearchResult::None => {
269                // Empty tree, insert root
270                let new_node = AVLNode::new(key, None, 1);
271                self.arena.push(Some(new_node));
272                self.root = Some(0);
273            }
274            SearchResult::Exists(_) => {}
275        }
276    }
277
278    /// Removes and returns an element from the AVL tree as an owned value.
279    pub fn remove<Q>(&mut self, key: &Q) -> Option<T>
280    where
281        Q: Ord + ?Sized,
282        T: Borrow<Q>,
283    {
284        //let target_index = match self.search(key.key()) {
285        let target_index = match self.search(key) {
286            SearchResult::Exists(idx) => idx,
287            _ => return None,
288        };
289
290        // Step 1: Find node to physically remove (node with ≤1 child)
291        let mut remove_index = target_index;
292        if self.node(remove_index).left.is_some() && self.node(remove_index).right.is_some() {
293            // Node has two children: find in-order successor
294            let mut succ_index = self.node(remove_index).right.unwrap();
295            while let Some(left) = self.node(succ_index).left {
296                succ_index = left;
297            }
298
299            // Move successor's value into target node
300            //let succ_value = self.arena[succ_index].take().unwrap().value;
301            //self.node_mut(remove_index).value = succ_value;
302
303            // 1. Take the value out of the successor node.
304            let succ_value = self.node_mut(succ_index).value.take();
305
306            // 2. Replace the target's value with the successor's,
307            // getting the original target value back.
308            let original_value = self
309                .node_mut(target_index)
310                .value
311                .replace(succ_value.unwrap());
312
313            // 3. Place the original target value into the successor node,
314            // which we are about to remove.
315            self.node_mut(succ_index).value = original_value;
316
317            // Now, the successor node can be safely removed, and it
318            // contains the correct value to return.
319
320            // Now remove the successor node (guaranteed ≤1 child)
321            remove_index = succ_index;
322        }
323
324        // Step 2: Identify the child of the node to remove (if any)
325        let child_index = self
326            .node(remove_index)
327            .left
328            .or(self.node(remove_index).right);
329
330        // Step 3: Update parent to point to the child
331        let parent_index = self.node(remove_index).parent;
332        if let Some(p_idx) = parent_index {
333            let parent = self.node_mut(p_idx);
334            if parent.left == Some(remove_index) {
335                parent.left = child_index;
336            } else {
337                parent.right = child_index;
338            }
339        } else {
340            // Removing root
341            self.root = child_index;
342        }
343
344        // Step 4: Update child's parent
345        if let Some(c_idx) = child_index {
346            self.node_mut(c_idx).parent = parent_index;
347        }
348
349        // Step 5: Take the node for return
350        let removed_value = self.arena[remove_index].take().map(|n| n.value);
351
352        // Step 6: Walk up ancestors to update heights and rebalance
353        let mut current = parent_index;
354        while let Some(idx) = current {
355            self.update_node_height(idx);
356            self.restructure(idx);
357            current = self.node(idx).parent;
358        }
359
360        removed_value?
361    }
362
363    /// Produces a "snapshot" iterator over immutable references to the
364    /// tree in its current state.
365    pub fn iter(&self) -> InOrderIter<'_, T> {
366        InOrderIter::new(&self.arena, self.root)
367    }
368
369    // Utility functions
370    ////////////////////
371
372    /// Immutable node accessor
373    fn node(&self, index: usize) -> &AVLNode<T> {
374        //self.arena[index]
375        //    .as_ref()
376        //    .expect("Error: Invalid immutable node access")
377        unsafe {
378            self.arena
379                .get_unchecked(index)
380                .as_ref()
381                .expect("Error: Invalid immutable node access")
382        }
383    }
384
385    /// Mutable node accessor
386    fn node_mut(&mut self, index: usize) -> &mut AVLNode<T> {
387        //self.arena[index]
388        //    .as_mut()
389        //    .expect("Error: Invalid mutable node access")
390        unsafe {
391            self.arena
392                .get_unchecked_mut(index)
393                .as_mut()
394                .expect("Error: Invalid mutable node access")
395        }
396    }
397
398    /// Returns a `SearchResult` enum with the following variants:
399    /// - None: Indicates an empty tree
400    /// - Parent: The key is not in the tree, but can be inserted at the parent index value
401    /// - Exists: The key was found in the tree; The caller can decide how to use this index
402    ///   to deal with multi-maps and sets
403    ///
404    /// SAFETY: May panic if a node does not contain a value, but that
405    /// would violate the AVL tree invariant, so its highly unlikely, and only present to
406    /// handle the possibility of corrupted structures.
407    // Original impl: WORKS for T
408    fn search<Q>(&self, key: &Q) -> SearchResult
409    where
410        Q: Ord + ?Sized,
411        T: Borrow<Q>,
412    {
413        // Early return for empty structures
414        if self.arena.is_empty() {
415            return SearchResult::None;
416        };
417
418        // Sets the starting point for the search
419        // Safety: Valueless nodes violate the AVL tree invariant
420        let mut current = self
421            .root
422            .expect("Error: Root should always contain a value");
423
424        // Uses iterative loop instead of recursive search
425        // because fuck stack overflows (and recursion)
426        loop {
427            //if let Some(val) = &self.arena[current] {
428            if let Some(val) = unsafe { &self.arena.get_unchecked(current) } {
429                // Converts &AVLNode<T> to &T
430                // Safety: Valueless nodes violate the AVL tree invariant
431                let value = val
432                    .get_value()
433                    .expect("Error: Node does not contain a value");
434
435                let value_key: &Q = value.borrow();
436
437                match value_key.cmp(key) {
438                    // Go right or return parent
439                    Ordering::Less => {
440                        if let Some(right) = val.right {
441                            current = right;
442                        } else {
443                            return SearchResult::Parent(current);
444                        }
445                    }
446                    // Go left or return parent
447                    Ordering::Greater => {
448                        if let Some(left) = val.left {
449                            current = left;
450                        } else {
451                            return SearchResult::Parent(current);
452                        }
453                    }
454                    // The key already exists in the tree at the current index
455                    Ordering::Equal => return SearchResult::Exists(current),
456                }
457            };
458        }
459    }
460
461    /// Updates the height of an arbitrary node in an AVL tree
462    /// where leaf nodes are defined as having height 1
463    fn update_node_height(&mut self, index: usize) {
464        let left = self
465            .node_mut(index)
466            .left
467            .map_or(0, |idx| self.node_mut(idx).height);
468        let right = self
469            .node_mut(index)
470            .right
471            .map_or(0, |idx| self.node_mut(idx).height);
472        // Works for internal and leaf nodes, because max(0, 0) + 1 = 1
473        self.node_mut(index).height = max(left, right) + 1
474    }
475
476    /// Rotate the subtree rooted at `root_idx` in the given direction.
477    /// `side` is the direction of the original heavy side (Side::Left or Side::Right).
478    fn rotate(&mut self, root_idx: usize, side: &Side) {
479        // Heavy child becomes the new root of the subtree
480        let child_idx = self
481            .node_mut(root_idx)
482            .child(side)
483            .expect("Rotation requires heavy child");
484
485        // Move child's opposite subtree into root's heavy side
486        //let subtree = self.arena[child_idx].child(opposite(side));
487        let subtree = self.node_mut(child_idx).child(!side);
488        self.node_mut(root_idx).set_child(side, subtree);
489        if let Some(sub_idx) = subtree {
490            self.node_mut(sub_idx).parent = Some(root_idx);
491        }
492
493        // Update parent pointers
494        let parent_idx = self.node_mut(root_idx).parent;
495        self.node_mut(child_idx).parent = parent_idx;
496
497        if let Some(p_idx) = parent_idx {
498            if self.node_mut(p_idx).left == Some(root_idx) {
499                self.node_mut(p_idx).left = Some(child_idx);
500            } else {
501                self.node_mut(p_idx).right = Some(child_idx);
502            }
503        } else {
504            self.root = Some(child_idx);
505        }
506
507        // Make old root the child of new root
508        self.node_mut(child_idx)
509            .set_child(opposite(side), Some(root_idx));
510        self.node_mut(root_idx).parent = Some(child_idx);
511
512        // Update heights
513        self.update_node_height(root_idx);
514        self.update_node_height(child_idx);
515    }
516
517    /// Determines left-heavy (>0) or right-heavy (<0) balance factors
518    /// for a given node index. The necessity for restructure
519    /// operations can be determined agnostically by
520    /// `abs(balance_factor(index)) >= 2`
521    fn balance_factor(&self, index: usize) -> isize {
522        let node = &self.node(index);
523        let left = node.left.map_or(0, |l| self.node(l).height as isize);
524        let right = node.right.map_or(0, |r| self.node(r).height as isize);
525        left - right
526    }
527
528    /// Rebalances the subtree rooted at `index`.
529    /// Performs single or double rotations as necessary.
530    fn restructure(&mut self, index: usize) {
531        // Not all inserts require restructuring
532        let balance = self.balance_factor(index);
533        if balance.abs() < 2 {
534            return;
535        }
536
537        // Determine heavy side
538        let heavy_side = if balance > 1 { Side::Left } else { Side::Right };
539
540        // Determine the child index for the heavy side
541        let child_idx = match self.node(index).child(&heavy_side) {
542            Some(idx) => idx,
543            // SAFETY: A None value on the child violates the AVL invariant
544            None => panic!("Error: Heavy child is None"),
545        };
546
547        // Double rotation check
548        //let child_balance = self.balance_factor(child_idx);
549        //if heavy_side == Side::Left && child_balance < 0 {
550        //    self.rotate(child_idx, &Side::Right);
551        //} else if heavy_side == Side::Right && child_balance > 0 {
552        //    self.rotate(child_idx, &Side::Left);
553        //}
554        match (&heavy_side, self.balance_factor(child_idx)) {
555            // LR
556            (Side::Left, b) if b < 0 => self.rotate(child_idx, &Side::Right),
557            // RL
558            (Side::Right, b) if b > 0 => self.rotate(child_idx, &Side::Left),
559            // Keep the compiler happy
560            _ => {}
561        }
562
563        // Single rotation on parent
564        self.rotate(index, &heavy_side);
565    }
566}
567
568pub struct InOrderIter<'a, T> {
569    arena: &'a [Option<AVLNode<T>>],
570    stack: Vec<usize>, // store indices, not references
571    current: Option<usize>,
572}
573impl<'a, T> InOrderIter<'a, T> {
574    fn new(arena: &'a [Option<AVLNode<T>>], root: Option<usize>) -> Self {
575        Self {
576            arena,
577            stack: Vec::new(),
578            current: root,
579        }
580    }
581}
582impl<'a, T> Iterator for InOrderIter<'a, T> {
583    type Item = &'a T;
584
585    fn next(&mut self) -> Option<Self::Item> {
586        while let Some(idx) = self.current {
587            if let Some(node) = self.arena[idx].as_ref() {
588                self.stack.push(idx);
589                self.current = node.left;
590                continue;
591            } else {
592                // Node was removed, skip
593                self.current = None;
594            }
595        }
596
597        if let Some(idx) = self.stack.pop() {
598            if let Some(node) = self.arena[idx].as_ref() {
599                self.current = node.right;
600                node.value.as_ref()
601            } else {
602                // Skip removed node
603                self.next()
604            }
605        } else {
606            None
607        }
608    }
609}
610
611#[test]
612fn avl_construction() {
613    let mut tree: AVLTree<u8> = AVLTree::new();
614
615    let v = [31, 13, 23, 39, 41, 43, 8, 17, 19];
616    // Produces the following AVL tree
617    //
618    //           39
619    //          /  \
620    //        17    41
621    //       /  \     \
622    //     13   23     43
623    //     /   /  \
624    //    8   19  31
625    //
626    for e in v.iter() {
627        tree.insert(*e);
628    }
629
630    // Tests that the root is being updated properly
631    assert_eq!(tree.get_root().unwrap(), &39);
632    assert_eq!(tree.node(tree.root.expect("You fucked up")).value, Some(39));
633    let root_node = &tree.node(tree.root.expect("nah, brah"));
634    assert_eq!(tree.node(root_node.left.unwrap()).value, Some(17));
635    assert_eq!(tree.node(root_node.right.unwrap()).value, Some(41));
636    assert_eq!(tree.node(0).left, None);
637    assert_eq!(tree.node(0).right, None);
638
639    assert_eq!(tree.node(tree.node(7).left.unwrap()).value, Some(13));
640    assert_eq!(tree.node(tree.node(7).right.unwrap()).value, Some(23));
641
642    let mut sorted = Vec::new();
643    for e in tree.iter() {
644        sorted.push(*e)
645    }
646    assert_eq!(sorted, [8, 13, 17, 19, 23, 31, 39, 41, 43]);
647
648    let mut tree: AVLTree<u8> = AVLTree::new();
649    let v = [1, 2, 3, 4, 5, 6, 7];
650    // Produces the following AVL tree
651    //
652    //          4
653    //        /   \
654    //      2       6
655    //     / \     / \
656    //    1   3   5   7
657    //
658    for e in v.iter() {
659        tree.insert(*e);
660    }
661
662    assert_eq!(tree.get_root().unwrap(), &4);
663    assert_eq!(tree.node(tree.root.expect("You fucked up")).value, Some(4));
664    let root_node = &tree.node(tree.root.expect("nah, brah"));
665    assert_eq!(tree.node(root_node.left.unwrap()).value, Some(2));
666    assert_eq!(tree.node(root_node.right.unwrap()).value, Some(6));
667    assert_eq!(tree.node(0).left, None);
668    assert_eq!(tree.node(0).right, None);
669
670    assert_eq!(tree.node(tree.node(5).left.unwrap()).value, Some(5));
671    assert_eq!(tree.node(tree.node(5).right.unwrap()).value, Some(7));
672
673    let mut sorted = Vec::new();
674    for e in tree.iter() {
675        sorted.push(*e)
676    }
677    assert_eq!(sorted, [1, 2, 3, 4, 5, 6, 7]);
678}
679
680#[test]
681fn avl_removals() {
682    let mut tree: AVLTree<u8> = AVLTree::new();
683
684    // Construct the following AVL tree
685    //
686    //           39
687    //          /  \
688    //        17    41
689    //       /  \     \
690    //     13   23     43
691    //     /   /  \
692    //    8   19  31
693    //
694    let v = [31, 13, 23, 39, 41, 43, 8, 17, 19];
695    for e in v.iter() {
696        tree.insert(*e);
697    }
698
699    // Remove 31 which results in the following AVL tree
700    //
701    //           39
702    //          /  \
703    //        17    41
704    //       /  \     \
705    //     13   23     43
706    //     /   /
707    //    8   19
708    //
709    assert_eq!(tree.get_root().unwrap(), &39);
710    assert!(tree.contains(&31));
711    let removed = tree.remove(&31).unwrap();
712    assert_eq!(removed, 31);
713    assert!(!tree.contains(&31));
714
715    assert_eq!(tree.node(tree.node(2).left.expect("")).value, Some(19));
716    assert_eq!(tree.node(2).right, None);
717
718    // Remove 41 which results in the following AVL tree
719    //
720    //         17
721    //        /  \
722    //      13    39
723    //     /     /  \
724    //    8     23   43
725    //          /
726    //        19
727    //
728    assert!(tree.contains(&41));
729    let removed = tree.remove(&41).unwrap();
730    assert_eq!(removed, 41);
731    assert!(tree.remove(&41).is_none()); // Test that 41 was really removed
732    assert!(!tree.contains(&41));
733
734    // 39 now has L 23 and R 43
735    assert_eq!(tree.node(tree.node(3).left.expect("")).value, Some(23));
736    assert_eq!(tree.node(tree.node(3).right.expect("")).value, Some(43));
737
738    // 17 is now rooth with L 13 and R 39
739    assert_eq!(tree.get_root().unwrap(), &17);
740    assert_eq!(tree.node(tree.root.expect("You fucked up")).value, Some(17));
741    assert_eq!(tree.node(tree.node(7).left.expect("")).value, Some(13));
742    assert_eq!(tree.node(tree.node(7).right.expect("")).value, Some(39));
743    // The old root 39 now has L 23 and R 43
744    assert_eq!(tree.node(tree.node(3).left.expect("")).value, Some(23));
745    assert_eq!(tree.node(tree.node(3).right.expect("")).value, Some(43));
746
747    // Remove 8 which results in the following AVL tree
748    //
749    //         23
750    //        /  \
751    //      17    39
752    //     /  \     \
753    //    13   19    43
754    //
755    assert!(tree.contains(&8));
756    let removed = tree.remove(&8).unwrap();
757    assert_eq!(removed, 8);
758    assert!(tree.remove(&8).is_none()); // Test that 8 was really removed
759    assert!(!tree.contains(&8));
760
761    // 23 is now rooth with L 17 and R 39
762    assert_eq!(tree.get_root().unwrap(), &23);
763    assert_eq!(tree.node(tree.root.expect("You fucked up")).value, Some(23));
764    assert_eq!(tree.node(tree.node(2).left.expect("")).value, Some(17));
765    assert_eq!(tree.node(tree.node(2).right.expect("")).value, Some(39));
766    // The old root 17 now has L 13 and R 19
767    assert_eq!(tree.node(tree.node(7).left.expect("")).value, Some(13));
768    assert_eq!(tree.node(tree.node(7).right.expect("")).value, Some(19));
769}