(********************************************************************)
(*  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 50 on page 244
   Insertion and Removal in a Prefix Tree *)

  let rec add x t =
    match x with
    | [] ->
        if t.word then t else { t with word = true }
    | i::l ->
        let b = try M.find i t.branches with Not_found -> empty in
        { t with branches = M.add i (add l b) t.branches }

  let rec remove x t =
    match x with
    | [] ->
        { t with word = false }
    | i::l ->
        try
          let s = remove l (M.find i t.branches) in
          let new_branches =
            if is_empty s then M.remove i t.branches
            else M.add i s t.branches
          in
          { t with branches = new_branches }
        with Not_found -> t

This document was generated using caml2html