diff --git a/correcteur.c b/correcteur.c index 29580f7..a139887 100644 --- a/correcteur.c +++ b/correcteur.c @@ -13,6 +13,24 @@ typedef struct node { int dernier; } Node; +bool is_empty(struct node *tree) +{ + return tree==NULL; +} + +bool is_leaf(struct node *tree) +{ + int cpt; + int i=0; + if (is_empty(tree)) return 1; + while (tree->lettres[i] != NULL){ + cpt++; + i++; + } + if (cpt !=0) cpt=1; + return(!is_empty(tree) && !cpt); +} + void ajout(Node **N, char mot) { @@ -27,6 +45,10 @@ void ajout_alphab(Node ** pn, char * mot,int cpt) { int i = 0; while (mot[cpt] != '\0'){ + if ((*pn)==NULL){ + ajout(pn,mot[cpt]); + return; + } while ((*pn)->lettres[i] != NULL){ if (strcmp(&(*pn)->l,mot) != 0){ i++; @@ -34,17 +56,19 @@ void ajout_alphab(Node ** pn, char * mot,int cpt) *pn=(*pn)->lettres[i]; return ajout_alphab(pn,mot,cpt++); } + (*pn)->dernier++; ajout(&(*pn)->lettres[i],mot[cpt]); *pn=(*pn)->lettres[i]; cpt++; } + (*pn)->fin_de_mot=1; } Node * charger_arbre(Node ** Arbre){ FILE * dico; char mot[MAX_LETTRES]; - dico = fopen("words.txt","r"); + dico = fopen("test.txt","r"); while (fscanf(dico,"%s",mot) == 1){ ajout_alphab(Arbre,mot,0); } @@ -52,7 +76,40 @@ Node * charger_arbre(Node ** Arbre){ return *Arbre; } +void affichage_arbre(Node * Arbre) +{ + if (is_empty(Arbre)) return; + if (Arbre->lettres[0]== NULL) return; + for (int i=0; idernier; i++){ + printf("%c -> %c",Arbre->l,Arbre->lettres[i]->l); + affichage_arbre(Arbre->lettres[i]); + if (Arbre->fin_de_mot) printf("fin de mot"); + } + +} + + +void detruire_arbre(Node ** Arbre) +{ + for (int i=0; i<(*Arbre)->dernier ; i++){ + if (*Arbre!=NULL) detruire_arbre(&(*Arbre)->lettres[i]); + } + free(*Arbre); +} + +void initialisation(Node ** Arbre){ + Node *nouveau = malloc(sizeof(struct node)); + (*nouveau).l='?'; + for (int i=0; i<27; i++) nouveau->lettres[i]=NULL; + nouveau->dernier=0; + *Arbre = nouveau; +} int main(){ + Node * Arbre; + initialisation(&Arbre); + charger_arbre(&Arbre); + affichage_arbre(Arbre); + detruire_arbre(&Arbre); return 0; } -- libgit2 0.21.2