(********************************************************************)
(*  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 35 on page 215
   Binary Search Trees (2/2) *)

  let rec remove_min_elt = function
    | Empty -> Empty
    | Node (Empty, _, r) -> r
    | Node (l, v, r) -> Node (remove_min_elt l, v, r)

  let merge t1 t2 = match t1, t2 with
    | Empty, t | t, Empty -> t
    | _ -> Node (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 Node (remove x l, v, r)
        else Node (l, v, remove x r)

This document was generated using caml2html