open Printf type inttree = Nil T of inttree * int * inttree let inorder = let rec inord cnt = function Nil -> cnt T (l, v, r) -> let cnt1 = inord cnt l in printf "%d: %d\n" cnt1 v; inord (cnt1+1) r in inord 1 let preorder = let rec preord cnt = function Nil -> cnt T (l, v, r) -> printf "%d: %d\n" cnt v; preord (preord (cnt+1) l) r in preord 1 let postorder = let rec postord cnt = function Nil -> cnt T (l, v, r) -> let cnt1 = postord (postord cnt l) r in printf "%d: %d\n" cnt1 v; cnt1 + 1 in postord 1 let t = T (T(Nil, 4, Nil), 6, T(T(T(Nil, 2, Nil), 5, T(Nil, 3, Nil)), 7, T(Nil, 9, Nil))) ;; ignore (inorder t);; ignore (preorder t);; ignore (postorder t);;
This document was generated using caml2html