(********************************************************************)
(*  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 34 on page 213
   Binary Search Trees (1/2) *)

  type t = Empty | Node of t * elt * t

  let rec min_elt = function
    | Empty -> raise Not_found
    | Node (Empty, v, __) -> v
    | Node (l, _, _) -> min_elt l

  let rec mem x = function
    | Empty ->
        false
    | Node (l, v, r) ->
        let c = X.compare x v in
        c = 0 || if c < 0 then mem x l else mem x r

  let rec add x t =
    match t with
    | Empty ->
        Node (Empty, x, Empty)
    | Node (l, v, r) ->
        let c = X.compare x v in
        if c = 0 then t
        else if c < 0 then Node (add x l, v, r)
        else Node (l, v, add x r)

This document was generated using caml2html