(********************************************************************) (* 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 75 on page 334 Lexicographic comparison of binary trees *) let rec leftmost z = function E -> z N (l, x, r) -> leftmost (Left (z, x, r)) l let rec compare cmp z1 z2 = match z1, z2 with Top, Top -> 0 Left (z1, x1, r1), Left (z2, x2, r2) -> let c = cmp x1 x2 in if c <> 0 then c else compare cmp (leftmost z1 r1) (leftmost z2 r2) Top, Left _ -> -1 Left _, Top -> 1 Right _, _ _, Right _ -> assert false let compare_tree cmp t1 t2 = compare cmp (leftmost Top t1) (leftmost Top t2)
This document was generated using caml2html