Tiens, j'ai extrait et nettoyé ce bout de code d'un programme, c'est pas du C# et c'est pas très joli, mais si ça peut t'aider pour l'algo...
open Graphics
open Printf
module NTree = struct
type 'a t = O | N of 'a * 'a t * 'a t
let make_random_ntree max_h max_w leaf_builder =
assert (max_h >= 0 && max_w >= 0) ;
Random.self_init () ;
let rec make h w =
match (Random.float 1.0 > 0.3), h, w with
| _, 0, _
| _, _, 0
| false, _, _ ->
O
| true, h, w ->
N (leaf_builder (), make (pred h) max_w, make h (pred w))
in make max_h max_w
let make_full_ntree max_h max_w leaf_builder =
assert (max_h >= 0 && max_w >= 0) ;
let rec make h w =
match h, w with
| 0, _ | _, 0 ->
O
| h, w ->
N (leaf_builder (), make (pred h) max_w, make h (pred w))
in make max_h max_w
let height t =
let rec height t acc cur cont =
match t, cont with
| O, [] ->
max acc cur
| O, (nt, ncur) :: ncont ->
height nt (max acc cur) ncur ncont
| N (_, l, r), _ ->
height l acc (succ cur) ((r, cur) :: cont)
in height t 0 0 []
let width t =
let rec width t acc cont =
match t, cont with
| O, [] ->
acc
| O, nt :: ncont ->
width nt acc ncont
| N (_, O, r), _ ->
width O (succ acc) (r :: cont)
| N (_, l, r), _ ->
width l acc (r :: cont)
in width t 0 []
let map_on_brothers t f =
let rec map acc t =
match t with
| O -> acc
| N (v, l, r) -> map (f v l :: acc) r
in map [] t
let draw t to_string =
let w,h = width t, height t in
open_graph (sprintf " %dx%d" (w * 24 + 10) (h * 24 + 10)) ;
let rec draw t x y lx ly =
let b = map_on_brothers t (fun x l -> (x, l)) in
let rec iterx l x =
match l with
| (v,l) :: tl ->
let w = max 24 ((width l) * 24) in
let tv = to_string v in
let tw,th = text_size tv in
set_color red;
moveto lx (ly + 8) ;
lineto (x + ((w - 24) / 2) + 12) (y + 12) ;
set_color white;
fill_circle (x + ((w - 24) / 2) + 12) (y + 12) 8 ;
set_color black;
draw_circle (x + ((w - 24) / 2) + 12) (y + 12) 8 ;
moveto (x + ((w - 24) / 2) + 12 - tw / 2) (y + 12 - th / 2) ;
draw_string tv ;
draw l x (y + 24) (x + ((w - 24) / 2) + 12) (y + 12);
iterx tl (w + x)
| [] -> ()
in iterx b x
in
draw t 5 5 (w * 24 / 2) (-5);
ignore (wait_next_event [Key_pressed ; Button_up ]);
close_graph ()
end
open NTree
let _ = draw (make_random_ntree 6 3 (fun x -> Random.int 25)) string_of_int