1 /*****************************************************************************/
3 /* Representation de mots sous forme d'arbre n-aire */
4 /*****************************************************************************/
14 typedef struct noeud
{
21 /* Recherche d'un mot dans l'arbre *****************************************/
22 NOEUD
*cherche(NOEUD
* p
, char *mot
, int i
)
26 if (p
->lettre
== mot
[i
]) {
28 return cherche(p
->fils
, mot
, i
+ 1);
31 } else if (p
->lettre
> mot
[i
])
34 return cherche(p
->frere
, mot
, i
);
37 /***************************************************************************/
38 NOEUD
*recherche(NOEUD
* p
, char *mot
)
40 return cherche(p
, mot
, 0);
44 /* Création d'une fin de mot - liste chaînée suivant le lien fils **********/
45 NOEUD
*insere_fin(char *mot
, int i
)
47 NOEUD
*p
= (NOEUD
*) malloc(sizeof(NOEUD
));
55 p
->fils
= insere_fin(mot
, i
+ 1);
60 /* Insertion d'un mot dans l'arbre ******************************************/
61 /* on respecte l'ordre lexicographique des frères **************************/
62 NOEUD
*insere(NOEUD
* p
, char *mot
, int i
)
67 p
= insere_fin(mot
, i
);
68 else if (p
->lettre
== mot
[i
])
69 if (mot
[i
] != '\0') /* if (mot[i]) */
70 p
->fils
= insere(p
->fils
, mot
, i
+ 1);
72 printf("Le mot est deja dans le dictionnaire\n");
73 else if (p
->lettre
> mot
[i
]) {
74 p1
= insere_fin(mot
, i
);
78 p
->frere
= insere(p
->frere
, mot
, i
);
83 /*****************************************************************************/
84 /* Affichage par ordre alphabétique de tous les mots stockés dans l'arbre */
85 void affiche_fich(FILE * fp
, NOEUD
* p
, char *mot
, int i
)
92 for (j
= 0; j
< i
; j
++)
93 fprintf(fp
, "%c", mot
[j
]);
96 affiche_fich(fp
, p
->fils
, mot
, i
+ 1);
97 affiche_fich(fp
, p
->frere
, mot
, i
);
102 /* Affichage par ordre alphabétique de tous les mots stockés dans l'arbre */
103 /* parcours en profondeur d'abord, prefixe (idem si infixe, voir la remarque plus loin) */
104 void affiche(NOEUD
* p
, char *mot
, int i
)
111 printf("%s\n", mot
); /* ici ou apres l'appel sur le fils ('\0' n'a pas de fils , donc l'appel sur le fils n'affichera rien */
112 affiche(p
->fils
, mot
, i
+ 1);
113 affiche(p
->frere
, mot
, i
);
118 /*****************************************************************************/
119 /* Visualisation de l'arbre n-aire - parcour "frère - racine - fils" */
120 void affiche_arbre(NOEUD
* p
, int prof
)
125 affiche_arbre(p
->frere
, prof
);
126 for (i
= 0; i
< prof
; i
++)
128 printf("%c\n", p
->lettre
);
129 affiche_arbre(p
->fils
, prof
+ 1);
134 /*****************************************************************************/
135 /* Attention à ne supprimer que la fin du mot qui n'est partagée par aucun
136 autre mot de l'arbre */
137 NOEUD
*supprime(NOEUD
* p
, char *mot
, int i
)
142 if (p
->lettre
< mot
[i
])
143 p
->frere
= supprime(p
->frere
, mot
, i
);
144 else if (p
->lettre
== mot
[i
]) {
145 p
->fils
= supprime(p
->fils
, mot
, i
+ 1);
152 printf("%s n'est pas present\n", mot
);
156 /*****************************************************************************/
157 NOEUD
*charge_dico(char *nom_fichier
, int *nb_mots
)
165 fp
= fopen(nom_fichier
, "rt");
168 fscanf(fp
, "%d", nb_mots
); /* sur la 1ère ligne du fichier, le nombre de mots */
169 for (i
= 0; i
< *nb_mots
; i
++) {
170 fscanf(fp
, "%s", mot
);
171 p
= insere(p
, mot
, 0);
178 /*****************************************************************************/
179 void sauve_dico(NOEUD
* p
, char *nom_fichier
, int nb_mots
)
184 fp
= fopen(nom_fichier
, "wt");
187 fprintf(fp
, "%d\n", nb_mots
);
188 affiche_fich(fp
, p
, mot
, 0);
193 /*****************************************************************************/
194 char EstSeparateur(char c
)
196 return (((c
== '\'') || (c
== '"') || (c
== ',') || (c
== ';')
197 || (c
== '.') || (c
== '\n') || (c
== '?') || (c
== '!')
198 || (c
== ':') || (c
== ' ') || (c
== '\t') || (c
== '-')
199 || (c
== '*') || (c
== '+') || (c
== '=') || (c
== '\b')
200 || (c
== '{') || (c
== '}') || (c
== '(') || (c
== ')')));
204 /****************************************************************************/
205 /* Lit un mot a partir du fichier fd.
206 texte == <separateur>* <mot> <separateur>*
208 Le mot est retourné sous une forme minuscule.
209 Quant on a atteint la fin de fichier, la fonction retourne 0 */
210 int Lectmot(FILE * fp
, char *mot
)
215 /* Lecture des separateurs */
216 while (((c
= (char) fgetc(fp
)) != (char) EOF
) && (EstSeparateur(c
)));
219 while (c
!= (char) EOF
) {
220 if (!EstSeparateur(c
))
221 mot
[i
++] = tolower(c
);
224 c
= (char) fgetc(fp
);
231 /*****************************************************************************/
232 int main(int argc
, char *argv
[])
239 printf("\nEntrez un mot a inserer (0 pour arreter) : ");
241 if (strcmp(mot
, "0")) {
242 printf("insertion de %s\n", mot
);
243 arbre
= insere(arbre
, mot
, 0);
245 } while (strcmp(mot
, "0"));
248 printf("\naffichage arbre :\n");
249 affiche_arbre(arbre
, 0);
251 printf("\nmots dans l'arbre :\n");
252 affiche(arbre
, mot
, 0);
254 /* test suppression */
256 printf("\nEntrez un mot a supprimer (0 pour arreter) : ");
258 if (strcmp(mot
, "0")) {
259 printf("suppression de %s\n", mot
);
260 arbre
= supprime(arbre
, mot
, 0);
261 affiche(arbre
, mot
, 0);
263 } while (strcmp(mot
, "0"));
265 /* affiche_fich(stdout,arbre,mot,0); */
269 /* exemple d'execution ***
270 Entrez un mot a inserer (0 pour arreter) : baba
273 Entrez un mot a inserer (0 pour arreter) : baobab
276 Entrez un mot a inserer (0 pour arreter) : bonjour
279 Entrez un mot a inserer (0 pour arreter) : salut
282 Entrez un mot a inserer (0 pour arreter) : salopette
283 insertion de salopette
285 Entrez un mot a inserer (0 pour arreter) : salutation
286 insertion de salutation
288 Entrez un mot a inserer (0 pour arreter) : 0
336 Entrez un mot a supprimer (0 pour arreter) : ba
346 Entrez un mot a supprimer (0 pour arreter) : baoba
348 baoba n'est pas present
356 Entrez un mot a supprimer (0 pour arreter) : baobab
357 suppression de baobab
363 Entrez un mot a supprimer (0 pour arreter) : salut
370 Entrez un mot a supprimer (0 pour arreter) : 0 */