Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3.  
  4. typedef struct node_st {
  5. int v;
  6. struct node_st *g, *d;
  7. } Node;
  8.  
  9. typedef Node * Tree;
  10.  
  11. /* Renvoie l'arbre construit à partir de la valeur val, des sous-arbres droit et gauche */
  12. Tree build (int val, Tree gauche, Tree droit) {
  13. Tree nw = (Tree) malloc(sizeof(Node));
  14. nw->v = val;
  15. nw->g = gauche;
  16. nw->d = droit;
  17. return nw;
  18. }
  19.  
  20. /* Affichage infixe des valeurs d'un arbre binaire */
  21. void print_inorder (Tree A) {
  22. if (A != NULL) {
  23. print_inorder(A->g);
  24. printf("%d ",A->v);
  25. print_inorder(A->d);
  26. }
  27. }
  28.  
  29. /* Renvoie un arbre binaire pour exemple */
  30. Tree arbre_test() {
  31. Tree a = build(5,
  32. build(6,
  33. build(3,
  34. build(10,
  35. NULL,
  36. NULL
  37. ),
  38. build(42,
  39. NULL,
  40. build(0,
  41. NULL,
  42. NULL
  43. )
  44. )
  45. ),
  46. NULL
  47. ),
  48. build(12,
  49. NULL,
  50. build(2,
  51. NULL,
  52. NULL
  53. )
  54. )
  55. );
  56. return a;
  57. }
  58.  
  59. int main() {
  60. Tree a = arbre_test();
  61. print_inorder(a);
  62. printf("\n");
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement