arbres.ml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. type 'a arbre = Vide | Noeud of 'a * 'a arbre * 'a arbre;;
  2. let ex1 = Noeud(1, Noeud(2, Noeud(4, Vide, Vide), Noeud(5, Vide, Vide)), Noeud(3, Noeud(6, Vide, Vide), Noeud(7, Vide, Vide)));;
  3. let rec nbr_sommets a = match a with
  4. | Vide -> 0
  5. | Noeud(_, g, d) -> 1 + nbr_sommets g + nbr_sommets d;;
  6. let rec hauteur a = match a with
  7. | Vide -> 0
  8. | Noeud(_, g, d) -> 1 + max (hauteur g) (hauteur d);;
  9. let rec somme a = match a with
  10. | Vide -> 0
  11. | Noeud(x, g, d) -> x + somme g + somme d;;
  12. let rec miroir a = match a with
  13. | Vide -> Vide
  14. | Noeud(x, g, d) -> Noeud(x, miroir d, miroir g);;
  15. let rec est_miroir a1 a2 = match a1, a2 with
  16. | Vide, Vide -> true
  17. | Vide, Noeud(_, _, _) -> false
  18. | Noeud(_, _, _), Vide -> false
  19. | Noeud(_, g1, d1), Noeud(_, g2, d2) -> est_miroir g1 d2 && est_miroir d1 g2;;
  20. let rec est_complet a = match a with
  21. | Vide -> true
  22. | Noeud(_, Vide, Vide) -> true
  23. | Noeud(_, g, Vide) -> false
  24. | Noeud(_, Vide, d) -> false
  25. | Noeud(_, g, d) -> est_complet g && est_complet d;;
  26. let rec est_parfait a = match a with
  27. | Vide -> true
  28. | Noeud(_, Vide, Vide) -> true
  29. | Noeud(_, g, d) -> est_parfait g && est_parfait d && hauteur g = hauteur d;;
  30. let rec est_arbre_binaire a = match a with
  31. | Vide -> true
  32. | Noeud(_, Vide, Vide) -> true
  33. | Noeud(_, g, Vide) -> est_arbre_binaire g
  34. | Noeud(_, Vide, d) -> est_arbre_binaire d
  35. | Noeud(_, g, d) -> est_arbre_binaire g && est_arbre_binaire d;;
  36. let rec est_arbre_binaire_complet a = match a with
  37. | Vide -> true
  38. | Noeud(_, Vide, Vide) -> true
  39. | Noeud(_, g, Vide) -> false
  40. | Noeud(_, Vide, d) -> false
  41. | Noeud(_, g, d) -> est_arbre_binaire_complet g && est_arbre_binaire_complet d && hauteur g = hauteur d;;
  42. let rec est_arbre_binaire_parfait a = match a with
  43. | Vide -> true
  44. | Noeud(_, Vide, Vide) -> true
  45. | Noeud(_, g, d) -> est_arbre_binaire_parfait g && est_arbre_binaire_parfait d && hauteur g = hauteur d;;
  46. let rec est_arbre_binaire_miroir a = match a with
  47. | Vide -> true
  48. | Noeud(_, Vide, Vide) -> true
  49. | Noeud(_, g, Vide) -> est_arbre_binaire_miroir g
  50. | Noeud(_, Vide, d) -> est_arbre_binaire_miroir d
  51. | Noeud(_, g, d) -> est_arbre_binaire_miroir g && est_arbre_binaire_miroir d;;
  52. let rec est_arbre_binaire_complet_miroir a = match a with
  53. | Vide -> true
  54. | Noeud(_, Vide, Vide) -> true
  55. | Noeud(_, g, Vide) -> false
  56. | Noeud(_, Vide, d) -> false
  57. | Noeud(_, g, d) -> est_arbre_binaire_complet_miroir g && est_arbre_binaire_complet_miroir d && hauteur g = hauteur d;;
  58. let rec est_arbre_binaire_parfait_miroir a = match a with
  59. | Vide -> true
  60. | Noeud(_, Vide, Vide) -> true
  61. | Noeud(_, g, d) -> est_arbre_binaire_parfait_miroir g && est_arbre_binaire_parfait_miroir d && hauteur g = hauteur d;;
  62. let rec noeuds a = match a with
  63. | Vide -> []
  64. | Noeud(x, g, d) -> x::(noeuds g)@(noeuds d);;
  65. let rec nbr_noeuds a = match a with
  66. | Vide -> 0
  67. | Noeud(_,Vide,Vide) -> 0
  68. | Noeud(_,g,d) -> 1 + nbr_noeuds g + nbr_noeuds d;;
  69. let rec nbr_feuilles a = match a with
  70. | Vide -> 0
  71. | Noeud(_, Vide, Vide) -> 1
  72. | Noeud(_, g, d) -> nbr_feuilles g + nbr_feuilles d;;
  73. let rec est_feuille a = match a with
  74. | Vide -> false
  75. | Noeud(_, Vide, Vide) -> true
  76. | Noeud(_, g, d) -> false;;
  77. let rec feuilles a = match a with
  78. | Vide -> []
  79. | Noeud(x, Vide, Vide) -> [x]
  80. | Noeud(_, g, d) -> (feuilles g)@(feuilles d);;
  81. let rec maximum a = match a with
  82. | Vide -> failwith "arbre vide"
  83. | Noeud(x, Vide, Vide) -> x
  84. | Noeud(x, g, d) -> max x (max (maximum g) (maximum d));;
  85. let rec minimum a = match a with
  86. | Vide -> failwith "arbre vide"
  87. | Noeud(x, Vide, Vide) -> x
  88. | Noeud(x, g, d) -> min x (min (minimum g) (minimum d));;
  89. let rec entier a = match a with
  90. | Vide -> true
  91. | Noeud(_, Vide, Vide) -> true
  92. | Noeud(_, Vide, _) -> false
  93. | Noeud(_, _, Vide) -> false
  94. | Noeud(x, g, d) -> entier g && entier d;;
  95. let rec prefixe a = match a with
  96. | Vide -> []
  97. | Noeud(r,Vide,Vide)-> [r]
  98. | Noeud(x, g, d) -> (x::prefixe g)@(prefixe d);;
  99. let rec infixe a = match a with
  100. | Vide -> []
  101. | Noeud(r,Vide,Vide)-> [r]
  102. | Noeud(x, g, d) -> (infixe g)@(x::infixe d);;
  103. let rec postfixe a = match a with
  104. | Vide -> []
  105. | Noeud(r,Vide,Vide)-> [r]
  106. | Noeud(x, g, d) -> (postfixe g)@(postfixe d)@[x];;
  107. let rec liste_racines l = match l with
  108. | [] -> []
  109. | (Noeud(x, _, _)::q) -> x::(liste_racines q);;
  110. let rec liste_fils l = match l with
  111. | [] -> []
  112. | (Noeud(_, g, d)::q) -> (g::d::(liste_fils q));;
  113. let rec parcours_largeur a =
  114. let rec aux l = match l with
  115. | [] -> []
  116. | _ -> (liste_racines l)@(aux (liste_fils l)) in
  117. aux [a];;
  118. type arbre_bin = Vide | Noeud of int * arbre_bin * arbre_bin;;
  119. type compa = Inf | Sup | Egal;;
  120. let compare_int x y = if x < y then Inf else if x > y then Sup else Egal;;
  121. let rec recherche (a:arbre_bin) f (x:int) = match a with
  122. | Vide -> false
  123. | Noeud(y, g, d) -> match f x y with
  124. | Egal -> true
  125. | Inf -> recherche g f x
  126. | Sup -> recherche d f x;;
  127. let rec min_gauche_max_droit a = match a with
  128. | Vide -> failwith "arbre vide"
  129. | Noeud(e,Vide,Vide) -> (e,e)
  130. | Noeud(e,ag,Vide)-> let (min, max) = min_gauche_max_droit ag in (min, e)
  131. | Noeud(e,Vide,ad)-> let (min, max) = min_gauche_max_droit ad in (e, max)
  132. | Noeud(e,ag,ad)-> let (min, max) = min_gauche_max_droit ag in (min, max);;
  133. let rec verif_encadre a f x y= match a with
  134. | Vide -> true
  135. | Noeud(e,ag,ad) -> not(f e x = Inf) && not(f e y = Sup) && verif_encadre ag f x e && verif_encadre ad f e y;;
  136. let verif_arbre_recherche a f = let (min, max) = min_gauche_max_droit a in verif_encadre a f min max;;
  137. let rec verif_tri l f = match l with
  138. | [] -> true
  139. | [x] -> true
  140. | t::t1::q -> not(f t t1 = Sup) && verif_tri (t1::q) f;;
  141. let rec infixe2 a = match a with
  142. | Vide -> []
  143. | Noeud(r,Vide,Vide)-> [r]
  144. | Noeud(x, g, d) -> (infixe2 g)@(x::infixe2 d);;
  145. let verif_arbre_recherche2 a f = verif_tri (infixe2 a) f;;
  146. let rec insere_feuille a f x = match a with
  147. | Vide -> Noeud(x, Vide, Vide)
  148. | Noeud(e, g, d) -> match f x e with
  149. | Egal -> a
  150. | Inf -> Noeud(e, insere_feuille g f x, d)
  151. | Sup -> Noeud(e, g, insere_feuille d f x);;
  152. let rec decoupe (a:arbre_bin) f (x:int) : arbre_bin * arbre_bin= match a with
  153. | Vide -> (Vide, Vide)
  154. | Noeud(e, g, d) -> match f x e with
  155. | Egal -> (g, d)
  156. | Inf -> let (g1, d1) = decoupe g f x in (g1, Noeud(e, d1, d))
  157. | Sup -> let (g1, d1) = decoupe d f x in (Noeud(e, g, g1), d1);;
  158. let insere_racine a f x = let (g, d) = decoupe a f x in Noeud(x, g, d);;
  159. let rec construction_feuille l f = match l with
  160. | [] -> Vide
  161. | t::q -> insere_feuille (construction_feuille q f) f t;;
  162. let rec construction_racine l f = match l with
  163. | [] -> Vide
  164. | t::q -> insere_racine (construction_racine q f) f t;;
  165. let rec rotation_gauche a = match a with
  166. | Vide -> Vide
  167. | Noeud(x, Vide, Vide) -> Noeud(x, Vide, Vide)
  168. | Noeud(x, Noeud(y, g, d), Vide) -> Noeud(y, g, Noeud(x, d, Vide))
  169. | Noeud(x, g, d) -> Noeud(x, rotation_gauche g, d);;
  170. let rec rotation_droite a = match a with
  171. | Vide -> Vide
  172. | Noeud(x, Vide, Vide) -> Noeud(x, Vide, Vide)
  173. | Noeud(x, Vide, Noeud(y, g, d)) -> Noeud(y, Noeud(x, Vide, g), d)
  174. | Noeud(x, g, d) -> Noeud(x, g, rotation_droite d);;
  175. let rec fusionne a1 a2 = match a1, a2 with
  176. | Vide, Vide -> Vide
  177. | Vide, Noeud(x, g, d) -> Noeud(x, g, d)
  178. | Noeud(x, g, d), Vide -> Noeud(x, g, d)
  179. | Noeud(x, g, d), Noeud(y, g1, d1) -> let (min, max) = min_gauche_max_droit a2 in Noeud(min, g, fusionne d a2);;