From 2e84492d968fd5d00dc132c9c6d40f4e03b52cb6 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 17:06:10 +0000 Subject: [PATCH] ocaml: phase 5.1 tree_depth.ml baseline (binary tree depth, longest path = 4) Same 'tree = Leaf | Node of int * tree * tree' ADT as iter-159 max_path_tree.ml, but the recursion ignores the value: let rec depth t = match t with | Leaf -> 0 | Node (_, l, r) -> let dl = depth l in let dr = depth r in 1 + (if dl > dr then dl else dr) For the test tree: 1 / 2 3 / 4 5 / 8 longest path is 1 -> 2 -> 5 -> 8, depth = 4. Tests wildcard pattern in constructor 'Node (_, l, r)', two nested let-bindings in match arm, inline if-as-expression for max. 83 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/tree_depth.ml | 20 ++++++++++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 27 insertions(+) create mode 100644 lib/ocaml/baseline/tree_depth.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index fe1185bc..9057ae65 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -78,6 +78,7 @@ "zip_unzip.ml": 1000, "sieve.ml": 15, "sum_squares.ml": 385, + "tree_depth.ml": 4, "triangle.ml": 11, "twosum.ml": 5, "unique_set.ml": 9, diff --git a/lib/ocaml/baseline/tree_depth.ml b/lib/ocaml/baseline/tree_depth.ml new file mode 100644 index 00000000..c29d9707 --- /dev/null +++ b/lib/ocaml/baseline/tree_depth.ml @@ -0,0 +1,20 @@ +type tree = Leaf | Node of int * tree * tree + +let rec depth t = match t with + | Leaf -> 0 + | Node (_, l, r) -> + let dl = depth l in + let dr = depth r in + 1 + (if dl > dr then dl else dr) + +;; + +let t = Node (1, + Node (2, + Node (4, Leaf, Leaf), + Node (5, + Node (8, Leaf, Leaf), + Leaf)), + Node (3, Leaf, Leaf)) +in +depth t diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 5f4ddf4a..4d49d180 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — tree_depth.ml baseline (binary tree depth, + longest path = 4). Same `tree = Leaf | Node of int * tree * tree` + ADT as iter 159, but recursion now ignores the value + (`Node (_, l, r)`) and returns `1 + max (depth l) (depth r)`. Uses + wildcard pattern in constructor, two nested let-bindings in arm, + and inline if-as-expression for max. 83 baseline programs total. - 2026-05-09 Phase 5.1 — stable_unique.ml baseline (Hashtbl-tracked dedupe preserving order, length+sum = 8+38 = 46). Walks input with `Hashtbl.mem` + `Hashtbl.add seen x ()` (unit-payload to use