Arbres.ml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. type 'a arbre1 =
  2. | Vide
  3. | N of 'a * 'a arbre1 * 'a arbre1
  4. ;;
  5. let test1 = N(2,N(1,Vide, Vide), N(4,N(6,Vide,Vide), Vide));;
  6. let rec nb_sommet f = match f with
  7. | Vide -> 0
  8. | N(_,b,c) -> 1 + nb_sommet b + nb_sommet c
  9. ;;
  10. let rec nb_noeud f = match f with
  11. | Vide -> 0
  12. | N(n,Vide,Vide) -> 0
  13. | N(_,g,d) -> 1 + nb_noeud g + nb_noeud d
  14. ;;
  15. let rec nb_feuille f = match f with
  16. | Vide -> 0
  17. | N(n,Vide,Vide) -> 1
  18. | N(_,g,d) -> nb_feuille g + nb_feuille d
  19. ;;
  20. let rec hauteur f = match f with
  21. | Vide -> -1
  22. | N(n,a,b) -> if hauteur a > hauteur b then 1 + hauteur a
  23. else 1 + hauteur b
  24. ;;
  25. let rec etiquette f = match f with
  26. | Vide -> failwith "stop"
  27. | N(r,Vide,Vide) -> r
  28. | N(r,g,Vide)-> max r (etiquette g)
  29. | N(r,Vide,d) -> max r (etiquette d)
  30. | N(r,g,d) -> max r (max (etiquette g) (etiquette g))
  31. ;;
  32. (* Pas sur de celui au dessus *)
  33. let rec entier a = match a with
  34. | Vide -> true
  35. | N(_,Vide,Vide) -> true
  36. | N(_,g,Vide) ->false
  37. | N(_,Vide,d) ->false
  38. | N(_,g,d) ->entier g && entier d
  39. ;;
  40. type 'a arbre2 =
  41. | F of 'a
  42. | N of 'a * 'a arbre2 * 'a arbre2
  43. ;;
  44. let test2 = N(2,F(1),N(4,F(7), F(8)));;
  45. type ('f, 'n) arbre3 =
  46. | F of 'f
  47. | N of 'n * (('f, 'n) arbre3) * (('f,'n) arbre3)
  48. ;;
  49. let test3 = N((+), N(( * ), F 7, N((-), F 4, F 2)), N(( * ), F 3, F 1))
  50. type 'a arbre4 = N of 'a * ('a arbre4 list);;
  51. let test4 = N(2, [N(3,[N(5, [])]); N(6,[N(8, []); N(4,[])]); N(7,[])]);;
  52. let rec nb_sommet2 f = match f with
  53. | N(_,[]) -> 0
  54. | N(r,b::q) -> 1 + (nb_sommet2 b) + (nb_sommet2 (N(r,q)))
  55. ;;
  56. let rec hauteur2 f = match f with
  57. | N(_,[]) -> 0
  58. | N(r,b::q) -> max (1+ hauteur2 b) (1+hauteur2 (N(r,q)))
  59. ;;
  60. type 'a arbre5 =
  61. |Vide
  62. | N of 'a * 'a arbre5 * 'a arbre5
  63. ;;
  64. let rec prefixe f = match f with
  65. |Vide -> []
  66. | N(r,Vide,Vide) -> [r]
  67. | N(r,g,d)-> r::(prefixe g)@(prefixe d)
  68. ;;
  69. let rec postfixe f = match f with
  70. | Vide -> []
  71. |N(r,Vide,Vide)-> [r]
  72. |N(r,g,d)->postfixe g@(postfixe d)@[r]
  73. ;;
  74. let rec infixe f = match f with
  75. | Vide -> []
  76. |N(r,Vide,Vide)-> [r]
  77. |N(r,g,d)->((infixe g)@[r])@(infixe d)
  78. ;;
  79. type 'a sommet6 =
  80. |SF of 'a
  81. |SN of 'a
  82. ;;
  83. type 'a arbre6 =
  84. |F of 'a
  85. |N of 'a * 'a arbre6 * 'a arbre6
  86. ;;
  87. let rec postfixe_aux l_arbre l = match l_arbre,l with
  88. | [a],[] -> a
  89. |_,(SF x)::q -> postfixe_aux ( F x::l_arbre) q
  90. | d::g::r, (SN x)::q -> postfixe_aux (N(x,g,d)::r) q
  91. ;;
  92. let postfixe6 l = postfixe_aux [] l;;
  93. let rec prefixe_aux l = match l with
  94. | []-> failwith "suce, liste vide"
  95. | (SF x)::q-> F x,q
  96. | (SN x)::q->let g,qg = prefixe_aux q in
  97. let d,qd = prefixe_aux qg in
  98. N(x,g,d),qd
  99. ;;
  100. let prefixe l = fst (prefixe_aux l);;
  101. let ex6 = [SF(4); SF(5); SN(6)];;
  102. type 'a arbre7 =
  103. | Vide
  104. | N of 'a * 'a arbre7 * 'a arbre7
  105. ;;
  106. let rec liste_racines l_arbres = match l_arbres with
  107. | [] -> []
  108. | N(n,_,_)::q-> n::liste_racines q
  109. ;;
  110. let rec branches l = match l with
  111. | [] -> []
  112. | N(x,Vide,Vide)::q-> branches q
  113. | N(x,g,d)::q-> g::(d::branches q)
  114. ;;
  115. let rec parcours_largeur_aux l = match l with
  116. | [] -> []
  117. | _ -> (liste_racines l)@(parcours_largeur_aux (branches l))
  118. ;;
  119. let parcours_largeur a = parcours_largeur_aux [a];;
  120. type 'a arbre8 = Vide | N of 'a * 'a arbre8 * 'a arbre8;;
  121. let ex8 = N(1,
  122. N(6,
  123. N(8,N(3,N(5,Vide,Vide),Vide),Vide),
  124. N(4,Vide,Vide)
  125. ),
  126. N(7,
  127. N(2,
  128. N(9,Vide,Vide),
  129. N(10,Vide,Vide)), Vide)
  130. )
  131. let rec branchedroite arbre = match arbre with
  132. | Vide -> []
  133. | N(x,Vide,Vide)->[x]
  134. | N(x,g,Vide)->x::branchedroite g
  135. | N(x,g,d)->x::branchedroite d
  136. ;;
  137. let rec feuilles arbre = match arbre with
  138. | Vide -> []
  139. | N(x,Vide,Vide)->[x]
  140. | N(_,g,d)-> feuilles g @ feuilles d
  141. ;;
  142. let rec feuilles_prof arbre = match arbre with
  143. | Vide -> []
  144. | N(x,g,Vide)->x ::branchedroite g
  145. | N(x,g,d)-> x::branchedroite d
  146. ;;
  147. let rec feuilles_aux arbre acc = match arbre with
  148. | Vide -> acc
  149. | N(x,Vide,Vide)->acc@[x]
  150. | N(_,g,d)-> feuilles_aux g (feuilles_aux d acc)
  151. ;;
  152. let feuilles_term arbre = feuilles_aux arbre [];;
  153. let rec squelette arbre1 arbre2 = match arbre1,arbre2 with
  154. | Vide,Vide -> true
  155. | N(n1,g1,d1),N(n2,g2,d2)-> squelette g1 g2 && squelette d1 d2
  156. | _ -> false
  157. ;;
  158. let rec miroir arbre = match arbre with
  159. | Vide -> Vide
  160. | N(x,g,d)->N(x,miroir d,miroir g)
  161. ;;
  162. let rec sosa a = match a with
  163. | Vide -> Vide
  164. | N(x,g,d)->