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 rec printleaves = function   (* tipareste doar frunzele *)
  | T (Nil, valnod, Nil) -> print_int valnod; print_char ' '
  | T (arbstg, _, arbdr) -> printleaves arbstg; printleaves arbdr
  | Nil -> ()

let rec print2chld = function (* tipareste doar nodurile cu 2 fii *)
  | T (Nil, _, t) | T(t, _, Nil) -> print2chld t
  | T (arbstg, valnod, arbdr) -> print_int valnod; print_char ' ';
    print2chld arbstg; print2chld arbdr
  | Nil -> ()

let rec afiseaza_nivel nivel = function (* tipareste noduri de pe un nivel, 0 = rad. *)
  | Nil -> ()
  | T (arbstg, valnod, arbdr) -> if nivel = 0 then (print_int valnod; print_char ' ')
    else afiseaza_nivel (nivel - 1) arbstg; afiseaza_nivel (nivel - 1) arbdr

let rec inaltime = function (* inaltimea arborelui = adancimea maxima; radacina = 1 *)
  | Nil -> 0
  | T (arbstg, _, arbdr) -> 1 + max (inaltime arbstg) (inaltime arbdr)

let rec printpath cale = function  (* toate frunzele cu calea pana la radacina *)
  | Nil -> ()
  | T (Nil, valnod, Nil) -> List.iter (printf "%d ") (valnod::cale); print_newline()
  | T (arbstg, valnod, arbdr) -> let cale1 =  valnod :: cale in
                                 printpath cale1 arbstg; printpath cale1 arbdr

let rec mem x = function (* se afla in arbore ? *)
  | Nil -> false
  | T (arbstg, valnod, arbdr) -> x = valnod || mem x arbstg || mem x arbdr

let rec insert t valnou = match t with (* inserare in arbore de cautare *)
  | Nil -> T(Nil, valnou, Nil)
  | T (arbstg, valrad, arbdr) as t -> if valnou = valrad then t
    else if valnou < valrad then T(insert arbstg valnou, valrad, arbdr)
    else T(arbstg, valrad, insert arbdr valnou)

let tree_of_list lst = List.fold_left insert Nil lst
  ;;
tree_of_list [4;5;3;2;0;6;7;8;4;5;6;2;5];;

This document was generated using caml2html