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
);
157 /*****************************************************************************/
158 NOEUD
*charge_dico(char *nom_fichier
, int *nb_mots
)
166 fp
= fopen(nom_fichier
, "rt");
169 fscanf(fp
, "%d", nb_mots
); /* sur la 1ère ligne du fichier, le nombre de mots */
170 for (i
= 0; i
< *nb_mots
; i
++) {
171 fscanf(fp
, "%s", mot
);
172 p
= insere(p
, mot
, 0);
179 NOEUD
*detruis_arbre(NOEUD
* p
)
184 p
->fils
= detruis_arbre(p
->fils
);
185 p
->frere
= detruis_arbre(p
->frere
);
193 /*****************************************************************************/
194 void sauve_dico(NOEUD
* p
, char *nom_fichier
, int nb_mots
)
199 fp
= fopen(nom_fichier
, "wt");
202 fprintf(fp
, "%d\n", nb_mots
);
203 affiche_fich(fp
, p
, mot
, 0);
208 /*****************************************************************************/
209 char EstSeparateur(char c
)
211 return (((c
== '\'') || (c
== '"') || (c
== ',') || (c
== ';')
212 || (c
== '.') || (c
== '\n') || (c
== '?') || (c
== '!')
213 || (c
== ':') || (c
== ' ') || (c
== '\t') || (c
== '-')
214 || (c
== '*') || (c
== '+') || (c
== '=') || (c
== '\b')
215 || (c
== '{') || (c
== '}') || (c
== '(') || (c
== ')')));
219 /****************************************************************************/
220 /* Lit un mot a partir du fichier fd.
221 texte == <separateur>* <mot> <separateur>*
223 Le mot est retourné sous une forme minuscule.
224 Quant on a atteint la fin de fichier, la fonction retourne 0 */
225 int Lectmot(FILE * fp
, char *mot
)
230 /* Lecture des separateurs */
231 while (((c
= (char) fgetc(fp
)) != (char) EOF
) && (EstSeparateur(c
)));
234 while (c
!= (char) EOF
) {
235 if (!EstSeparateur(c
))
236 mot
[i
++] = tolower(c
);
239 c
= (char) fgetc(fp
);
246 /*****************************************************************************/
247 int main(int argc
, char *argv
[])
254 printf("\nEntrez un mot a inserer (0 pour arreter) : ");
256 if (strcmp(mot
, "0")) {
257 printf("insertion de %s\n", mot
);
258 arbre
= insere(arbre
, mot
, 0);
260 } while (strcmp(mot
, "0"));
263 printf("\naffichage arbre :\n");
264 affiche_arbre(arbre
, 0);
266 printf("\nmots dans l'arbre :\n");
267 affiche(arbre
, mot
, 0);
269 /* test suppression */
271 printf("\nEntrez un mot a supprimer (0 pour arreter) : ");
273 if (strcmp(mot
, "0")) {
274 printf("suppression de %s\n", mot
);
275 arbre
= supprime(arbre
, mot
, 0);
276 affiche(arbre
, mot
, 0);
278 } while (strcmp(mot
, "0"));
280 /* affiche_fich(stdout,arbre,mot,0); */
281 detruis_arbre(arbre
);
285 /* exemple d'execution ***
286 Entrez un mot a inserer (0 pour arreter) : baba
289 Entrez un mot a inserer (0 pour arreter) : baobab
292 Entrez un mot a inserer (0 pour arreter) : bonjour
295 Entrez un mot a inserer (0 pour arreter) : salut
298 Entrez un mot a inserer (0 pour arreter) : salopette
299 insertion de salopette
301 Entrez un mot a inserer (0 pour arreter) : salutation
302 insertion de salutation
304 Entrez un mot a inserer (0 pour arreter) : 0
352 Entrez un mot a supprimer (0 pour arreter) : ba
362 Entrez un mot a supprimer (0 pour arreter) : baoba
364 baoba n'est pas present
372 Entrez un mot a supprimer (0 pour arreter) : baobab
373 suppression de baobab
379 Entrez un mot a supprimer (0 pour arreter) : salut
386 Entrez un mot a supprimer (0 pour arreter) : 0 */