Pouknouki

Tri par tas Caml

Oct 12th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.38 KB | None | 0 0
  1. type Tas = Vide | Noeud of int * Tas * Tas;;
  2.  
  3. let monTas =
  4. Noeud(1,
  5.     Noeud(4,
  6.         Noeud(5, Vide, Vide),
  7.         Noeud(7,
  8.             Noeud(9,
  9.                 Noeud(14, Vide, Vide),
  10.                 Vide),
  11.             Noeud(8, Vide, Vide))),
  12.     Noeud(2,
  13.         Noeud(6, Vide, Vide),
  14.         Noeud(3, Vide, Vide)));;
  15.  
  16. let min_tas T = match T with
  17.     | Vide -> failwith "Tas vide.";
  18.     | Noeud(r, f, g) -> r;;
  19.  
  20. let rec enleve_min tas = match tas with
  21.     | Vide -> Vide;
  22.     | Noeud(m, Vide, Vide) -> Vide;
  23.     | Noeud(m, Vide, f) -> Noeud(min_tas f, enleve_min f, Vide);
  24.     | Noeud(m, f, Vide) -> Noeud(min_tas f, enleve_min f, Vide);
  25.     | Noeud(m, g, d) -> let mg = min_tas g and md = min_tas d in
  26.         if (mg < md) then
  27.             Noeud(mg, enleve_min g, d)
  28.         else
  29.             Noeud(md, g, enleve_min d);;
  30.  
  31. let rec ajout tas x = match tas with
  32.     | Vide -> Noeud(x, Vide, Vide);
  33.     | Noeud(m, g, d) -> if x <= m then Noeud(x, d, ajout g m)
  34.                                             else Noeud(m, d, ajout g x);;
  35.  
  36. let ajouter_liste l =
  37.     let rec aux l acc = match l with
  38.         | [] -> acc
  39.         | t::q -> aux q (ajout acc t);
  40.     in aux l Vide;;
  41.  
  42. let rec vider tas = match tas with
  43.     | Vide -> []
  44.     | Noeud(m, Vide, Vide) -> [m];
  45.     | Noeud(m, Vide, d)    -> m::(vider d);
  46.     | Noeud(m, g, Vide)    -> m::(vider g);
  47.     | Noeud(m, g, d)       -> m::(vider (enleve_min tas));;
  48.  
  49. vider monTas;;
  50.  
  51. let rec triParTas liste = vider (ajouter_liste liste);;
  52.  
  53. let test = triParTas [2; 1; 45; 6; 3; 6; 0; 8; 69; 45; 89];;
Advertisement
Add Comment
Please, Sign In to add comment