(********************************************************************)
(*  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 55 on page 256
   Removing an Element in a Patricia Tree *)

let node = function
  | (_, _, Empty, t)
  | (_, _, t, Empty) -> t
  | (p, b, l, r) -> Node (p, b, l, r)

let rec remove x = function
  | Empty ->
      Empty
  | Leaf j as t ->
      if x == j then Empty else t
  | Node (p, m, t0, t1) as t ->
      if matches_prefix x p m then
        if zero_bit x m then
          node (p, m, remove x t0, t1)
        else
          node (p, m, t0, remove x t1)
      else
        t

This document was generated using caml2html