diff --git a/arbre.c b/arbre.c index 7c34ad7..ac2440b 100644 --- a/arbre.c +++ b/arbre.c @@ -17,7 +17,7 @@ typedef struct dico { int calculcase(char c){ - return 'c'-'a'; + return c-'a'; } void arbre_vide(struct arbre ** pt_arbre) @@ -43,23 +43,13 @@ void ini_dico(struct dico * pt_dico){ } -void ajout_mot(struct arbre *arbrecourant,char *mot,int i){ -// Arbre *arbrecourant=malloc(sizeof(struct arbre)); -// if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){ -// arbrecourant->val=mot[0]; -// arbrecourant->finmot=false; -// for(int j=0;j<26;j++){ -// arbrecourant->suite[j]=NULL; -// } -// } -// arbrecourant=(*ppt_dico)->alpha[calculcase(mot[0])]; -// -// int i=1; - printf("%s:length:%d\n",mot,strlen(mot)); +void ajout_mot(struct arbre **arbrecourant,char *mot,int i){ + + printf("%s:length:%zu \n",mot,strlen(mot)); - while(mot[i]!='\0'){ + if(mot[i]!='\0'){ printf("\nlettre:%c; i=%d\n",mot[i],i); - if (arbrecourant->suite[calculcase(mot[i])]==NULL){ + if (*arbrecourant==NULL){ printf("creation arbre\n"); Arbre *monarbre=malloc(sizeof(struct arbre)); @@ -68,13 +58,11 @@ void ajout_mot(struct arbre *arbrecourant,char *mot,int i){ for(int j=0;j<26;j++){ monarbre->suite[j]=NULL; } - arbrecourant=monarbre; + *arbrecourant=monarbre; // printf("%c\n",(*pt_arbre)->val); - } - arbrecourant=arbrecourant->suite[calculcase(mot[i])]; i++; - ajout_mot(arbrecourant->suite[calculcase(mot[i])],mot,i); + ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i); } } @@ -83,19 +71,22 @@ void charger_arbre(FILE *fp, struct dico **ppt_dico) { char mot[20]; ini_dico(*ppt_dico); while (fscanf(fp, "%s", mot)==1){ - printf("mot:%s\n",mot); + printf("mot:%s\n\n\n",mot); - Arbre *arbrecourant=malloc(sizeof(struct arbre)); + printf("\nlettre:%c\n",mot[0]); + /* if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){ + printf("creation 1er niveau\n"); + Arbre *arbrecourant=malloc(sizeof(struct arbre)); arbrecourant->val=mot[0]; arbrecourant->finmot=false; for(int j=0;j<26;j++){ arbrecourant->suite[j]=NULL; } - } - arbrecourant=(*ppt_dico)->alpha[calculcase(mot[0])]; - int i=1; - ajout_mot(&arbrecourant,mot,i); + (*ppt_dico)->alpha[calculcase(mot[0])]=arbrecourant; + }*/ + //int index=1; + ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0); } return ; } @@ -106,29 +97,28 @@ void free_arbre(struct arbre *pt_arbre){ return ; } for(int i=0;i<26;i++){ - free_arbre(pt_arbre->suite[i]); + free_arbre((pt_arbre->suite[i])); } + printf("lettre:%c\n",pt_arbre->val); free(pt_arbre); - } void free_dico(struct dico *pt_dico){ if (pt_dico==NULL){ return ; } + for(int i=0;i<26;i++){ - free_arbre(pt_dico->alpha[i]); - free(pt_dico->alpha[i]); + free_arbre((pt_dico->alpha[i])); } - free(pt_dico); + free(pt_dico); } void affiche_arbre(struct arbre *arbre){ if(arbre==NULL){ return ; } - printf("%c\n",arbre->val); for(int i=0;i<26;i++){ affiche_arbre(arbre->suite[i]); } @@ -157,7 +147,7 @@ int main (){ affiche_dico(mondico); - free(mondico); + free_dico(mondico); fclose(fp); return 0; } -- libgit2 0.21.2