From 33b319d8be7f550fee29e104e263021789a24dab Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Fri, 12 Apr 2024 20:04:37 +0200 Subject: feat: commit Rust alternatives Signed-off-by: Raito Bezarius --- src/main_mono.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main_mono.rs (limited to 'src/main_mono.rs') diff --git a/src/main_mono.rs b/src/main_mono.rs new file mode 100644 index 0000000..68da0ff --- /dev/null +++ b/src/main_mono.rs @@ -0,0 +1,45 @@ +use std::cmp::Ordering; + +struct AVLNode { + value: u32, + left: AVLTree, + right: AVLTree, +} + +type AVLTree = Option>; + +struct AVLTreeSet { + root: AVLTree, +} + +impl AVLTreeSet { + 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); +} -- cgit v1.2.3