1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
struct node *sn;
int v;
size_t s_sn;
} n_t;
int add_branch(int* branch, size_t s_branch, n_t** tree, size_t *s_tree) {
int i=0;
int* p=branch;
size_t s_p=s_branch;
n_t* new_tree;
//fprintf(stderr,"s_tree : %d\n",*s_tree);
while (i < *s_tree && (*tree)[i].v!=*branch) { i++; }
if (i==(*s_tree)) { /* ajout d un noeud */
fprintf(stderr,"nouvelle val %d au niveau %d\n",*branch,s_branch);
(*s_tree)++;
fprintf(stderr,"allocating sizeof(n_t)*%d\n",*s_tree);
if ( ( new_tree=realloc(*tree,sizeof(n_t)*(*s_tree) )) == NULL ) {
fprintf(stderr,"error... could not realloc...\n");
exit(1);
}
else {
*tree=new_tree;
(*tree)[i].sn=NULL;
(*tree)[i].s_sn=0;
(*tree)[i].v=*branch;
}
}
else {
fprintf(stderr,"val %d deja trouvee au niveau %d\n",*branch,s_branch);
}
// recursivite
s_p--;
if ( s_p > 0) {
p++;
fprintf(stderr,"recursion profondeur %d\n",s_p);
add_branch(p,s_p,&((*tree)[i].sn),&((*tree)[i].s_sn));
}
return 0;
}
int main(int argc, char* argv[]) {
n_t* t=NULL;
size_t s_t=0;
int b[5]={7,2,3,4,5};
fprintf(stderr,"ajout de :");
for (int i=0 ; i< sizeof(b)/sizeof(int) ; i++)
fprintf(stderr," %d",b[i]);
fprintf(stderr,"\n");
add_branch(b,sizeof(b)/sizeof(int),&t,&s_t);
b[2]=2;b[1]=3; /* 1 3 2 4 5 */
fprintf(stderr,"ajout de :");
for (int i=0 ; i< sizeof(b)/sizeof(int) ; i++)
fprintf(stderr," %d",b[i]);
fprintf(stderr,"\n");
add_branch(b,sizeof(b)/sizeof(int),&t,&s_t);
} |
Partager