Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. typedef struct _noeud
  5. {
  6. int valeur ;
  7. struct _noeud * fils_gauche;
  8. struct _noeud * fils_droite;
  9. }noeud;
  10.  
  11. /* AUtre possibilité pour déclarer la structure
  12. * // Définition du type structuré
  13. * struct _noeud
  14. * {
  15. * int valeur ;
  16. * struct _noeud * fils_gauche ;
  17. * struct _noeud * fils_droite ;
  18. * };
  19. * //Alias
  20. * typedef struct _noeud noeud ;
  21. */
  22.  
  23. //allocation mémoire d'un maillon (noeud)
  24. noeud* creer_noeud(int v){
  25.  
  26. noeud * element = (noeud*)malloc(sizeof(noeud));
  27. element -> valeur = v;
  28. //initialisation des pointeurs gauche et droite
  29. element -> fils_gauche = NULL;
  30. element -> fils_droite = NULL;
  31. return element;
  32. }
  33.  
  34. //asocier un noeud gauche
  35. bool associer_fils_gauche(noeud* parent, noeud* enfant){
  36. bool ok = false;
  37. if(parent != NULL){
  38. /* vérifier si le parent n'a pas déjà de fils gauche et
  39. l'enfant existe
  40. */
  41. if(parent-> fils_gauche == NULL && enfant != NULL){
  42. parent -> fils_gauche = enfant;
  43. }
  44. }
  45. return ok;
  46. }
  47.  
  48. //asocier un noeud droite
  49. bool associer_fils_droite(noeud* parent, noeud* enfant){
  50. bool ok = false;
  51. if(parent != NULL){
  52. /* vérifier si le parent n'a pas déjà de fils droit et
  53. l'enfant existe
  54. */
  55. if(parent -> fils_droite == NULL && enfant != NULL){
  56. parent -> fils_droite = enfant;
  57. }
  58. }
  59. return ok;
  60. }
  61.  
  62. bool est_feuille (noeud* element){
  63. bool ok = false;
  64.  
  65. if (element != NULL){
  66. if(element -> fils_gauche == NULL && element -> fils_droite == NULL){
  67. ok = true;
  68. }
  69. }
  70. return ok;
  71. }
  72.  
  73. void exo1(){
  74.  
  75. noeud* racine = creer_noeud(20);
  76. associer_fils_gauche(racine,creer_noeud(10));
  77. associer_fils_droite(racine,creer_noeud(15));
  78. est_feuille(15);
  79. }
  80.  
  81.  
  82. void affichage_prefixe(noeud * arbre){
  83.  
  84. if (arbre != NULL){
  85. printf("%d \n",arbre -> valeur);
  86. affichage_prefixe(arbre ->fils_gauche);
  87. affichage_prefixe(arbre -> fils_droite);
  88. }
  89. }
  90.  
  91. void affichage_infixe(noeud * arbre){
  92.  
  93. if (arbre != NULL){
  94. affichage_infixe(arbre ->fils_gauche);
  95. printf("%d \n",arbre -> valeur);
  96. affichage_infixe(arbre -> fils_droite);
  97. }
  98. }
  99.  
  100. void affichage_postfixe(noeud * arbre){
  101.  
  102. if(arbre != NULL){
  103. affichage_postfixe(arbre -> fils_gauche);
  104. affichage_postfixe(arbre -> fils_droite);
  105. printf("%d \n", arbre -> valeur);
  106. }
  107. }
  108.  
  109. void exo2(){
  110.  
  111. noeud * racine = creer_noeud (12) ;
  112.  
  113. // Sous - arbre de gauche
  114. noeud * n1 = creer_noeud (1) ;
  115. associer_fils_gauche (n1 , creer_noeud (91));
  116. associer_fils_droite (n1 , creer_noeud (67));
  117.  
  118. // Sous - arbre de droite
  119. noeud * n7 = creer_noeud (7) ;
  120. noeud * n82 = creer_noeud (82) ;
  121. associer_fils_droite (n7 , n82);
  122. associer_fils_gauche (n82 , creer_noeud (61));
  123.  
  124. // Assemblage final
  125. associer_fils_gauche (racine , n1);
  126. associer_fils_droite (racine , n7);
  127. affichage_prefixe(racine);
  128. printf("%d \n");
  129. affichage_infixe(racine);
  130. printf("%d \n");
  131. affichage_postfixe(racine);
  132. }
  133.  
  134.  
  135. int nombre_enfants(noeud * parent){
  136. int cpt = 0;
  137. if (parent != NULL){
  138. if(parent -> fils_gauche != NULL)
  139. cpt++;
  140. if(parent -> fils_droite != NULL)
  141. cpt++;
  142. }
  143. return cpt;
  144. }
  145.  
  146. int nbr_elements(noeud * arbre){
  147. if (arbre == NULL)
  148. return 0;
  149. else
  150. return 1 + nbr_elements(arbre -> fils_gauche) + nbr_elements(arbre -> fils_droite);
  151. }
  152.  
  153. int nombre_descendants(noeud * parent) {
  154. return nbr_elements(parent) - 1; //tous les éléments moins celui de la racine
  155. }
  156.  
  157. int nombre_feuilles(noeud * parent){
  158.  
  159. if (parent == NULL)
  160. return 0;
  161. else {
  162. if (est_feuille(parent))
  163. return 1;
  164. else
  165. return nombre_feuilles(parent -> fils_gauche) + nombre_feuilles(parent -> fils_droite);
  166. }
  167. }
  168.  
  169.  
  170. void exo4(){
  171. }
  172.  
  173.  
  174. int main(int argc, char **argv)
  175. {
  176. //exo1();
  177. //exo2();
  178. //exo3();
  179. exo4();
  180.  
  181. scanf("\n");
  182. return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement