Advertisement
csmine

TD5 - Parcours d'arbres binaires non entiers

Apr 9th, 2019
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.13 KB | None | 0 0
  1. (* TD n°5 : parcours d'arbres *)
  2.  
  3. type ('f, 'n) abe =
  4. |Feuille of 'f
  5. |Noeud of 'n * ( ('f,'n) abe)*(('f,'n) abe);;
  6.  
  7. type 'a tree =
  8. |Vide
  9. |Node of 'a * ('a tree) * ('a tree);;
  10.  
  11. (* Exercice 1 *)
  12. let toto =
  13. Node(   1,
  14.         Node(   2,
  15.                 Node( 4,Vide,Vide),
  16.                 Node(   5,
  17.                         Node(7,Vide,Vide),
  18.                         Node(8,Vide,Vide))),
  19.         Node( 3,
  20.                 Vide,
  21.                 Node(   6,
  22.                         Node(9,Vide,Vide),
  23.                         Vide)));;
  24. (* Exercice 2 *)
  25. let rec abe_to_tree abe = match abe with
  26. |Feuille f -> Node(f,Vide,Vide)
  27. |Noeud (n,fg,fd) -> Node(n,abe_to_tree fg,abe_to_tree fd);;
  28.  
  29. let exemple1 =
  30. Noeud(  1,
  31.         Noeud(  2,
  32.                 Feuille 4,
  33.                 Noeud(  5,
  34.                         Feuille 7,
  35.                         Feuille 8)),
  36.         Noeud( 3,
  37.                 Feuille 0,
  38.                 Noeud(  6,
  39.                         Feuille 9,
  40.                         Feuille 0)));;
  41. abe_to_tree exemple1;;
  42.  
  43.  
  44. (* Exercice 3 *)
  45.  
  46. let rec parcours_prefixe tree = match tree with
  47. |Vide -> []
  48. |Node (a,Vide,Vide) -> [a]
  49. |Node (a,fg,Vide) -> a::(parcours_prefixe fg)
  50. |Node (a,Vide,fd) -> a::(parcours_prefixe fd)
  51. |Node (a,fg,fd) -> a::((parcours_prefixe fg)@(parcours_prefixe fd));;
  52.  
  53. parcours_prefixe toto;;
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. (* Exercice 4 *)
  64.  
  65. let rec parcours_suffixe tree = match tree with
  66. |Vide -> []
  67. |Node (a,Vide,Vide) -> [a]
  68. |Node (a,fg,Vide) -> (parcours_infixe fg)@[a]
  69. |Node (a,Vide,fd) -> (parcours_infixe fd)@[a]
  70. |Node (a,fg,fd) -> let (g,d) = (parcours_infixe fg,parcours_infixe fd) in (g@d)@[a];;
  71. parcours_suffixe toto;;
  72.  
  73.  
  74. let rec parcours_infixe tree = match tree with
  75. |Vide -> []
  76. |Node (a,Vide,Vide) -> [a]
  77. |Node (a,fg,Vide) -> (parcours_infixe fg)@[a]
  78. |Node (a,Vide,fd) -> [a]@(parcours_infixe fd)
  79. |Node (a,fg,fd) -> let (g,d) = (parcours_infixe fg,parcours_infixe fd) in g@[a]@d;;
  80. parcours_infixe toto;;
  81.  
  82.  
  83. (* Exercice 5 *)
  84.  
  85. let parcours_largeur tree =
  86.     let rec aux liste_of_tree liste_parcourue = match liste_of_tree with
  87.     |[] -> List.rev liste_parcourue
  88.     |(Node (n,Vide,Vide))::t -> aux t (n::liste_parcourue)
  89.     |(Node (n,Vide,fd))::t -> aux (t@[fd]) (n::liste_parcourue)
  90.     |(Node (n,fg,Vide))::t -> aux (t@[fg]) (n::liste_parcourue)
  91.     |(Node (n,fg,fd))::t -> aux (t@([fg]@[fd])) (n::liste_parcourue)
  92.     in aux [tree] [];;
  93.  
  94. parcours_largeur toto;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement