Clemsim 2 år sedan
förälder
incheckning
092754faf4
2 ändrade filer med 553 tillägg och 0 borttagningar
  1. 422 0
      TP4/tp4.ml
  2. 131 0
      test2/Arbres.ml

+ 422 - 0
TP4/tp4.ml

@@ -0,0 +1,422 @@
+type arbre_bin =
+|Vide
+| Noeud of int*(arbre_bin)*(arbre_bin)
+;;
+
+let ex1 = Noeud(
+  27,
+  Noeud(
+    12,
+    Noeud(5,Vide,Vide),
+    Noeud(
+      19,
+      Noeud(17,Vide,Vide),
+      Noeud(
+        24,
+        Noeud(20,Vide,Vide),
+        Noeud(26,Vide,Vide)
+      )
+    )
+  ),
+  Noeud(
+    43,
+    Noeud(36,Vide,Vide),
+    Noeud(77,Vide,Vide)
+  )
+);;
+
+type comparaison = Inf | Egal | Sup;;
+
+let compare_int x y =
+  if x<y then Inf else if x=y then Egal else Sup
+;;
+
+let rec recherche a func x = match a with
+| Vide -> false
+| Noeud(n,g,d)->match func x n with
+| Inf -> recherche g func x
+| Egal -> true
+| Sup -> recherche d func x
+;;
+(*
+
+
+   
+C(0)=2
+Opf = Comparaisons
+
+
+2h+2
+*)
+
+(*a est un arbre de recherche dans tous les programmes suivants*)
+let rec min_gauche_max_droit a max1 min2= match a with
+| Vide -> failwith "Pas de min"
+| N(e,g,d) -> if max1 > min_gauche_max_droit d then max1 else min_gauche_max_droit d;
+  if min2 > min_gauche_max_droit a
+
+;;
+#trace min_gauche_max_droit;;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+let rec verif_encadre a fuck x y = match a with
+| Vide -> true
+| 
+;;
+let verif_arbre_rech a func = verif_encadre a func (fst (min_gauche_max_droit a)) (snd (min_gauche_max_droit a));;
+let verif_tri l func =
+;;
+(*Copier la fonction infixe ici*)
+
+let verif_arb_rech2 a func =
+;;
+let insere_feuille a func x =
+;;
+let decoupe a func x =
+;;
+let insere_racine a func x = 
+;;
+let construction_feuille l func = 
+
+;;
+
+let construction_racine l func =
+
+;;
+
+let liste_ex1 = [1;2;3;4;5;6;7;8;9];;
+let liste_ex2 = [1;3;5;7;9;8;6;4;2];;
+
+let rotation_gauche a =
+
+;;
+
+let rotation_droite a =
+
+;;
+
+let rec mystere a1 a2 func =
+  | _,Vide -> a1
+  | Vide,_ -> a2
+  | Noeud(e1,ag1,ad1), Noeud(e2, ag2,ad1) when compare e1 e2 = Inf
+    -> mystere ad1 (Noeud(e2,mystere Noeud(e1,ag1,Vide) ag2 func, ad2)) func
+  | Noeud(e1,ag1,ad1), Noeud(e2,ag2,ad2)
+    -> mystere ag1 (Noeud(e2,ag2,mystere Noeud(e1,Vide,ad1) ad2 func)) func
+;;

+ 131 - 0
test2/Arbres.ml

@@ -73,3 +73,134 @@ let rec hauteur2 f = match f with
 | N(_,[]) -> 0
 | N(r,b::q) -> max (1+ hauteur2 b) (1+hauteur2 (N(r,q)))
 ;;
+
+type 'a arbre5 =
+  |Vide
+  | N of 'a * 'a arbre5 * 'a arbre5
+;;
+
+let rec prefixe f = match f with
+|Vide -> []
+| N(r,Vide,Vide) -> [r]
+| N(r,g,d)-> r::(prefixe g)@(prefixe d)
+;;
+
+let rec postfixe f = match f with
+| Vide -> []
+|N(r,Vide,Vide)-> [r]
+|N(r,g,d)->postfixe g@(postfixe d)@[r]
+;;
+
+let rec infixe f = match f with
+| Vide -> []
+|N(r,Vide,Vide)-> [r]
+|N(r,g,d)->((infixe g)@[r])@(infixe d)
+;;
+
+type 'a sommet6 =
+|SF of 'a
+|SN of 'a
+;;
+
+type 'a arbre6 = 
+|F of 'a
+|N of 'a * 'a arbre6 * 'a arbre6
+;;
+
+let rec postfixe_aux l_arbre l = match l_arbre,l with
+| [a],[] -> a
+|_,(SF x)::q -> postfixe_aux ( F x::l_arbre) q
+| d::g::r, (SN x)::q -> postfixe_aux (N(x,g,d)::r) q
+;;
+
+let postfixe6 l = postfixe_aux [] l;;
+
+let rec prefixe_aux l = match l with
+| []-> failwith "suce, liste vide"
+| (SF x)::q-> F x,q
+| (SN x)::q->let g,qg = prefixe_aux q in
+  let d,qd = prefixe_aux qg in
+  N(x,g,d),qd
+
+;;
+
+let prefixe l = fst (prefixe_aux l);;
+
+let ex6 = [SF(4); SF(5); SN(6)];;
+
+type 'a arbre7 =
+| Vide
+| N of 'a * 'a arbre7 * 'a arbre7
+;;
+
+let rec liste_racines l_arbres = match l_arbres with
+| [] -> []
+| N(n,_,_)::q-> n::liste_racines q
+;;
+
+let rec branches l = match l with
+| [] -> []
+| N(x,Vide,Vide)::q-> branches q
+| N(x,g,d)::q-> g::(d::branches q)
+;;
+
+let rec parcours_largeur_aux l = match l with
+| [] -> []
+| _ -> (liste_racines l)@(parcours_largeur_aux (branches l))
+;;
+
+let parcours_largeur a = parcours_largeur_aux [a];;
+
+type 'a arbre8 = Vide | N of 'a * 'a arbre8 * 'a arbre8;;
+
+let ex8 = N(1,
+  N(6,
+  N(8,N(3,N(5,Vide,Vide),Vide),Vide),
+  N(4,Vide,Vide)
+  ),
+  N(7,
+  N(2,
+  N(9,Vide,Vide),
+  N(10,Vide,Vide)), Vide)
+)
+
+let rec branchedroite arbre = match arbre with
+| Vide -> []
+| N(x,Vide,Vide)->[x]
+| N(x,g,Vide)->x::branchedroite g
+| N(x,g,d)->x::branchedroite d
+;;
+
+let rec feuilles arbre = match arbre with
+| Vide -> []
+| N(x,Vide,Vide)->[x]
+| N(_,g,d)-> feuilles g @ feuilles d
+;;
+
+let rec feuilles_prof arbre = match arbre with
+| Vide -> []
+| N(x,g,Vide)->x ::branchedroite g
+| N(x,g,d)-> x::branchedroite d
+;;
+let rec feuilles_aux arbre acc = match arbre with
+| Vide -> acc
+| N(x,Vide,Vide)->acc@[x]
+| N(_,g,d)-> feuilles_aux g (feuilles_aux d acc)
+;;
+
+let feuilles_term arbre = feuilles_aux arbre [];;
+
+let rec squelette arbre1 arbre2 = match arbre1,arbre2 with
+| Vide,Vide -> true
+| N(n1,g1,d1),N(n2,g2,d2)-> squelette g1 g2 && squelette d1 d2
+| _ -> false
+;;
+
+let rec miroir arbre = match arbre with
+| Vide -> Vide
+| N(x,g,d)->N(x,miroir d,miroir g)
+;;
+
+let rec sosa a = match a with
+| Vide -> Vide
+| N(x,g,d)->