{ Let MAP compare item_key. (make compare item_key) Let SET compare. Let item_key item. item In Let MAP. (make compare item_key) In Let union set set'. (LIST.reduce (MAP.list set) set' MAP.insert) In { Let compare. compare Let empty. MAP.empty Let search. MAP.search Let insert. MAP.insert Let list. MAP.list Let union. union } } Where Let make compare item_key. { Let compare. compare Let item_key. item_key Let empty. { Let depth. 0 Let root. `zero } Let insert tree item. Define inserting depth node. If (depth = 1) Match node | `one.item' Match (compare (item_key item) (item_key item')) | `less `no_split.`two.{item item'} | `greater `no_split.`two.{item' item} | `equal `no_split.`one.item ; | `two.{item'1 item'2} Let key. (item_key item) Let key'1. (item_key item'1) Let key'2. (item_key item'2) In Match (compare key key'1) | `less `split.{`one.item item'1 `one.item'2} | `greater Match (compare key key'2) | `less `split.{`one.item'1 item `one.item'2} | `greater `split.{`one.item'1 item'2 `one.item} | `equal `no_split.`two.{item'1 item} ; | `equal `no_split.`two.{item item'2} ; ; Match node | `one.{node'1 item' node'2} Match (compare (item_key item) (item_key item')) | `less Match (inserting (depth + -1) node'1) | `no_split.node'' `no_split.`one.{node'' item' node'2} | `split.{node''1 item'' node''2} `no_split.`two.{node''1 item'' node''2 item' node'2} ; | `greater Match (inserting (depth + -1) node'2) | `no_split.node'' `no_split.`one.{node'1 item' node''} | `split.{node''1 item'' node''2} `no_split.`two.{node'1 item' node''1 item'' node''2} ; | `equal `no_split.`one.{node'1 item node'2} ; | `two.{node'1 item'1 node'2 item'2 node'3} Let key. (item_key item) Let key'1. (item_key item'1) Let key'2. (item_key item'2) In Match (compare key key'1) | `less Match (inserting (depth + -1) node'1) | `no_split.node'' `no_split.`two.{node'' item'1 node'2 item'2 node'3} | `split.{node''1 item'' node''2} `split.{ `one.{node''1 item'' node''2} item'1 `one.{node'2 item'2 node'3} } ; | `greater Match (compare key key'2) | `less Match (inserting (depth + -1) node'2) | `no_split.node'' `no_split.`two.{node'1 item'1 node'' item'2 node'3} | `split.{node''1 item'' node''2} `split.{ `one.{node'1 item'1 node''1} item'' `one.{node''2 item'2 node'3} } ; | `greater Match (inserting (depth + -1) node'3) | `no_split.node'' `no_split.`two.{node'1 item'1 node'2 item'2 node''} | `split.{node''1 item'' node''2} `split.{ `one.{node'1 item'1 node'2} item'2 `one.{node''1 item'' node''2} } ; | `equal `no_split.`two.{node'1 item'1 node'2 item node'3} ; | `equal `no_split.`two.{node'1 item node'2 item'2 node'3} ; ; In If (tree.depth = 0) { Let depth. 1 Let root. `one.item } Match (inserting tree.depth tree.root) | `no_split.node { Let depth. tree.depth Let root. node } | `split.{node1 item node2} { Let depth. (tree.depth + 1) Let root. `one.{node1 item node2} } ; Let search tree key. Define searching depth node. If (depth = 1) Match node | `one.item Match (compare key (item_key item)) | `equal `just.item | _ `nothing ; | `two.{item1 item2} Match (compare key (item_key item1)) | `equal `just.item1 | _ Match (compare key (item_key item2)) | `equal `just.item2 | _ `nothing ; ; ; Match node | `one.{node1 item node2} Match (compare key (item_key item)) | `less Goto (searching (depth + -1) node1) | `equal `just.item | `greater Goto (searching (depth + -1) node2) ; | `two.{node1 item1 node2 item2 node3} Match (compare key (item_key item1)) | `less Goto (searching (depth + -1) node1) | `equal `just.item1 | `greater Match (compare key (item_key item2)) | `less Goto (searching (depth + -1) node2) | `equal `just.item2 | `greater Goto (searching (depth + -1) node3) ; ; ; In If (tree.depth = 0) `nothing (searching tree.depth tree.root) Let list tree. Define collecting depth node list. If (depth = 1) Match node | `one.item (item::list) | `two.{item1 item2} (item1::item2::list) ; Match node | `one.{node1 item node2} Let depth'. (depth + -1) In (collecting depth' node1 (item::collecting depth' node2 list)) | `two.{node1 item1 node2 item2 node3} Let depth'. (depth + -1) In Let list. (collecting depth' node2 (item2::collecting depth' node3 list)) In (collecting depth' node1 (item1::list)) ; In If (tree.depth = 0) [] (collecting tree.depth tree.root []) } Where Let write_numbers numbers. (LIST.for_each numbers Func n. (STDIO.print_line $ Z.show n)) Where Let STDIO. Package "stdio" Let LIST. Package "list" Let Z. Package "z"