open Printf

type 'a bintree = Nil | T of 'a bintree * 'a * 'a bintree

let t1 = T (T(Nil, 2, Nil), 7,
   T(T(Nil, 5, Nil), 6, T(Nil, 11, Nil)));;

let preorder =
  let rec preord cnt = function
    | Nil -> cnt
    | T (arbstg, valnod, arbdr) -> printf "%d: %d\n" cnt valnod;
      let urm = preord (cnt+1) arbstg        (* urmatorul numar dupa ce parcurgem arbstg *)
      in preord urm arbdr
  (* sau direct: preord (preord (cnt + 1) arbstg) arbdr *)
  in preord 1                (* pornim de la 1 *)

let inorder =
  let rec inord cnt = function
    | Nil -> cnt
    | T (arbstg, valnod, arbdr) ->
       let urm = inord cnt arbstg in
       printf "%d: %d\n" urm valnod; inord (urm+1) arbdr
  in inord 1
  
let postorder =
  let rec postord cnt = function
    | Nil -> cnt
    | T (arbstg, valnod, arbdr) ->
       let urm = postord (postord cnt arbstg) arbdr
       in printf "%d: %d\n" urm valnod; urm+1
  in postord 1

let printedge =
  let rec print3 parinte l r =        (* tipareste arbore din 3 parti *)
    let printchld = function        (* tipareste arborele cu legatura de la parinte *)
      | Nil -> ()
      | T (arbstg, valnod, arbdr) -> printf "  %d -> %d;\n" parinte valnod;
        print3 valnod arbstg arbdr
    in printchld l; printchld r
  in function                (* pentru radacina *)
  | Nil -> ()
  | T (arbstg, valnod, arbdr) -> print3 valnod arbstg arbdr

let tip t = print_endline "digraph {"; printedge t; print_endline "}"
;;
tip t1

This document was generated using caml2html