type tree =
| Leaf of int
| Node of tree*tree
let t =
Node (
Node (Leaf 25,
(Node (Node (Leaf 41, Leaf 9), Leaf 12))),
Node (Leaf 8, Node (Leaf 25, Leaf 55)))
let rec iter f = function
| Leaf i -> f i
| Node (l,r) -> iter f l ; iter f r
let () =
iter (Printf.printf "%d ") t ;
Printf.printf "\n"
let rec iter_k f t k =
match t with
| Leaf i -> f i k
| Node (l,r) -> iter_k f l (fun x -> iter_k f r k)
let () =
Printf.printf "\n" ;
iter_k
(fun i k -> Printf.printf "%d " i ; k ())
t
(fun () -> Printf.printf "\n")
let sum100 t =
let total = ref 0 in
let counter = ref 0 in
let on_next n k =
counter := !counter + n ;
k () ;
counter := !counter - n ;
k ()
in
iter_k on_next t
(fun () -> if !counter = 100 then incr total) ;
!total
let () =
Printf.printf "%d solutions\n" (sum100 t)