pub enum Ordering { Less, Equal, Greater, } trait Ord { fn cmp(&self, other: &Self) -> Ordering; } // TODO: la structure AVLNode est extrait comme un inductif à un cas // au lieu d'être extrait comme une structure struct AVLNode { value: T, 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 } } impl Ord for u32 { fn cmp(&self, other: &Self) -> Ordering { if *self < *other { Ordering::Less } else if *self == *other { Ordering::Equal } else { Ordering::Greater } } }