(********************************************************************) (* OCaml code from the book ``Learn Programming with OCaml'' *) (* See https://usr.lmf.cnrs.fr/lpo/ *) (* *) (* Sylvain Conchon and Jean-Christophe Filliâtre *) (* Copyright 2025 Université Paris-Saclay and CNRS *) (* *) (* Openly licensed via CC BY SA 4.0 *) (* See https://creativecommons.org/licenses/by-sa/4.0/deed.en *) (********************************************************************) (* Program 37 on page 224 Insertion and Removal in an AVL Tree *) let rec add x = function Empty -> Node (Empty, x, Empty, 1) Node (l, v, r, _) as t -> let c = X.compare x v in if c = 0 then t else if c < 0 then balance (add x l) v r else balance l v (add x r) let rec remove_min_elt = function Empty -> Empty Node (Empty, _, r, _) -> r Node (l, v, r, _) -> balance (remove_min_elt l) v r let merge t1 t2 = match t1, t2 with Empty, t t, Empty -> t _ -> balance t1 (min_elt t2) (remove_min_elt t2) let rec remove x = function Empty -> Empty Node (l, v, r, _) -> let c = X.compare x v in if c = 0 then merge l r else if c < 0 then balance (remove x l) v r else balance l v (remove x r)
This document was generated using caml2html