Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5.  
  6. typedef struct node {
  7. char value;
  8.  
  9. struct node *left_c;
  10. struct node *righ_c;
  11. } nodes;
  12.  
  13. nodes *init() {
  14. nodes *nouv = malloc(sizeof(nodes));
  15. nouv->value = 'E';
  16. nouv->left_c = NULL;
  17. nouv->righ_c = NULL;
  18. return nouv;
  19. }
  20.  
  21. void addchild(nodes *racine, int x, char value) {
  22. nodes *nouv = malloc(sizeof(nodes));
  23. nouv->value = value;
  24. nouv->left_c = NULL;
  25. nouv->righ_c =NULL;
  26. if (x==0) {
  27. racine->left_c=nouv;
  28. }
  29. else {
  30. racine->righ_c=nouv;
  31. }
  32. }
  33.  
  34. int mystrlen(char *str) {
  35. int k =0;
  36. while (str[k]!='\0')
  37. k++;
  38.  
  39. return k;
  40. }
  41.  
  42. char *suppr(char *str){
  43. char *s = malloc(sizeof(char) *(mystrlen(str)));
  44. s = strncpy(s, str, mystrlen(str));
  45. s[(mystrlen(str))] = '\0';
  46. return s;
  47. }
  48.  
  49. void printnode(nodes *racine, char *chemin, int nb) {
  50.  
  51.  
  52.  
  53. int hauteur= strlen(chemin);
  54.  
  55. if (nb == 0) {
  56. printf("\t %c", racine->value);
  57. return;
  58. }
  59.  
  60. char *nchemin = malloc(sizeof(char) *(strlen(chemin)+1));
  61. nchemin = strcpy(nchemin, chemin);
  62.  
  63.  
  64. if (hauteur == nb)
  65. printf("\t %c", racine->value);
  66. // else
  67. // printf(">%d ", hauteur);
  68.  
  69.  
  70. if (racine->left_c !=NULL){
  71. nchemin = strcat(nchemin, "0");
  72. printnode(racine->left_c, nchemin, nb);
  73. nchemin = suppr(nchemin);
  74. }
  75. if (racine->righ_c !=NULL) {
  76. nchemin = strcat(nchemin, "1");
  77. printnode(racine->righ_c, nchemin, nb);
  78. nchemin = suppr(nchemin);
  79. }
  80.  
  81. }
  82.  
  83.  
  84. nodes *find(nodes *racine, char *str) {
  85. nodes *tmp =racine;
  86. int k = 0;
  87. printf("début du find()\n");
  88. while (str[k]!='\0') {
  89. printf("%c\n", tmp->value);
  90.  
  91. if (str[k]=='0' && tmp->left_c !=NULL)
  92. tmp= tmp->left_c;
  93.  
  94. else if (str[k]=='1' && tmp->righ_c !=NULL)
  95. tmp= tmp->righ_c;
  96.  
  97. else
  98. {
  99. printf("chemin non valide\n");
  100. return NULL;
  101. }
  102. k++;
  103. }
  104. return tmp;
  105. }
  106.  
  107. void addmore(nodes *racine, int x)
  108. {
  109.  
  110. addchild(racine, 0, '0');
  111. addchild(racine, 1, '1');
  112. if (x>1){
  113. addmore(racine->left_c, x-1);
  114. addmore(racine->righ_c, x-1);
  115. }
  116.  
  117. }
  118.  
  119.  
  120. void occurrence(int *no, char *occ, int *hauteur, nodes *racine) {
  121. *no = 1;
  122. *hauteur = 0;
  123. int k = 0;
  124. nodes *temp = racine;
  125.  
  126. if (occ[k]=='E'){
  127. return;
  128. }
  129.  
  130. while (occ[k]!='\0') {
  131. if (occ[k]=='0') {
  132. temp = temp->left_c;
  133. *no = *no*2;
  134. }
  135. if (occ[k]=='1') {
  136. temp=temp->righ_c;
  137. *no=(*no*2)+1;
  138. }
  139. *hauteur = *hauteur+1;
  140. k++;
  141. }
  142. }
  143.  
  144.  
  145.  
  146. int main()
  147. {
  148. nodes *racine;
  149. nodes *tmp1;
  150. nodes *tmp2;
  151. int no =1;
  152. int *pno= &no;
  153. char occ[] ="E";
  154. int hauteur;
  155. char *chemin = malloc(sizeof(char)*50);
  156. chemin[0] = 'E';
  157.  
  158. racine = init();
  159.  
  160. addmore(racine, 10);
  161.  
  162.  
  163. int k;
  164. for (k=0; k<10; k++)
  165. {
  166. printf("k = %d ", k);
  167. printnode(racine, chemin, k);
  168. printf("\n");
  169. }
  170.  
  171. return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement