ocaml: phase 5.1 btree.ml baseline (13/13 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
Polymorphic binary search tree with insert + in-order traversal. Exercises parametric ADT (type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree), recursive match, List.append, List.fold_left.
This commit is contained in:
25
lib/ocaml/baseline/btree.ml
Normal file
25
lib/ocaml/baseline/btree.ml
Normal file
@@ -0,0 +1,25 @@
|
||||
(* Baseline: binary search tree with insert + in-order traversal *)
|
||||
type 'a tree =
|
||||
| Leaf
|
||||
| Node of 'a * 'a tree * 'a tree
|
||||
;;
|
||||
|
||||
let rec insert x t =
|
||||
match t with
|
||||
| Leaf -> Node (x, Leaf, Leaf)
|
||||
| Node (v, l, r) ->
|
||||
if x < v then Node (v, insert x l, r)
|
||||
else if x > v then Node (v, l, insert x r)
|
||||
else t
|
||||
;;
|
||||
|
||||
let rec inorder t =
|
||||
match t with
|
||||
| Leaf -> []
|
||||
| Node (v, l, r) -> List.append (inorder l) (v :: inorder r)
|
||||
;;
|
||||
|
||||
let from_list xs = List.fold_left (fun t x -> insert x t) Leaf xs ;;
|
||||
|
||||
let t = from_list [5; 3; 8; 1; 4; 7; 9; 2] ;;
|
||||
List.fold_left (fun a b -> a + b) 0 (inorder t)
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"btree.ml": 39,
|
||||
"calc.ml": 13,
|
||||
"closures.ml": 315,
|
||||
"exception_handle.ml": 4,
|
||||
|
||||
@@ -399,6 +399,10 @@ _Newest first._
|
||||
recognise `!` as the prefix-deref of an application argument, so
|
||||
`String.concat "" (List.rev !b)` parses as `(... (deref b))`. Buffer
|
||||
uses a ref holding a string list; contents reverses and concats.
|
||||
- 2026-05-08 Phase 5.1 — btree.ml baseline (13/13 pass). Polymorphic
|
||||
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-08 Phase 5.1 — fizzbuzz.ml baseline (12/12 pass). Classic
|
||||
fizzbuzz using ref-cell accumulator, for-loop, mod, if/elseif chain,
|
||||
String.concat, Int.to_string. Verifies output via String.length.
|
||||
|
||||
Reference in New Issue
Block a user