diff options
author | Raito Bezarius | 2024-03-25 11:43:20 +0100 |
---|---|---|
committer | Raito Bezarius | 2024-03-25 11:43:20 +0100 |
commit | d6dce7238ad59f67b6b9bad1b3e50984bb69e84e (patch) | |
tree | 19f60bd57efd52b51c966eac6a404519a3bc56a8 /src/main_gen.rs |
Initial commit
Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
Diffstat (limited to 'src/main_gen.rs')
-rw-r--r-- | src/main_gen.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/main_gen.rs b/src/main_gen.rs new file mode 100644 index 0000000..b3f6add --- /dev/null +++ b/src/main_gen.rs @@ -0,0 +1,45 @@ +use std::cmp::Ordering; + +struct AVLNode<T: Ord> { + value: T, + left: AVLTree<T>, + right: AVLTree<T>, +} + +type AVLTree<T> = Option<Box<AVLNode<T>>>; + +struct AVLTreeSet<T: Ord> { + root: AVLTree<T>, +} + +impl<T: Ord> AVLTreeSet<T> { + pub fn new() -> Self { + Self { root: None } + } + + pub fn insert(&mut self, value: T) -> bool { + let mut current_tree = &mut self.root; + + while let Some(current_node) = current_tree { + match current_node.value.cmp(&value) { + Ordering::Less => current_tree = &mut current_node.right, + Ordering::Equal => return false, + Ordering::Greater => current_tree = &mut current_node.left, + } + } + + *current_tree = Some(Box::new(AVLNode { + value, + left: None, + right: None, + })); + + true + } +} + +fn main() { + let mut tree = AVLTreeSet::new(); + tree.insert(3); + tree.insert(2); +} |