let rec btree_fold e f btree = match btree with
Empty -> e
| Node(x, l, r) -> f x (btree_fold e f l) (btree_fold e f r)
let btree_size btree =
btree_fold 0 (fun x l r -> 1 + y + z) btree
let btree_sum btree =
btree_fold 0 (fun x l r -> x+y+z) btree
let btree_prod btree =
btree_fold 1 (fun x l r -> x*y*z) btree
let btree_height btree =
btree_fold (-1) (fun x l r -> if y > z then y + 1 else z + 1) btree
let btree_to_list btree =
btree_fold [] (fun x l r -> (x :: l) @ r) btree
(* equivalent to btree_mirror *)
let btree_twist btree =
btree_fold Empty (fun x l r -> Node(x, r, l)) btree
let btree_map f btree =
btree_fold Empty (fun x l r -> Node(f x, l, r)) btree