arbre.c 3.5 KB
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>


typedef struct arbre{
    char val;
    struct arbre *suite[26];
    bool finmot; //1 si fin de mot
}Arbre;

typedef struct dico {
    Arbre *alpha[26];
}Dico;



int calculcase(char c){
    return 'c'-'a';
}

void arbre_vide(struct arbre ** pt_arbre)
{
	*pt_arbre = NULL;
}

bool est_vide(struct arbre *arbre)
{
	return arbre==NULL;
}

bool fin_de_mot(struct arbre *arbre)
{
	return arbre->finmot;
}

void ini_dico(struct dico * pt_dico){
    printf("ini_dico\n");
    for(int i=0;i<26;i++){
        pt_dico->alpha[i]=NULL;        
    }
}


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));
    
    while(mot[i]!='\0'){
        printf("\nlettre:%c; i=%d\n",mot[i],i);
        if (arbrecourant->suite[calculcase(mot[i])]==NULL){
            
            printf("creation arbre\n");
            Arbre *monarbre=malloc(sizeof(struct arbre));
            monarbre->val=mot[i];
            monarbre->finmot=false;
            for(int j=0;j<26;j++){
                monarbre->suite[j]=NULL;     
            }
            arbrecourant=monarbre;
//             printf("%c\n",(*pt_arbre)->val);
                
        }
        arbrecourant=arbrecourant->suite[calculcase(mot[i])];
        i++;
        ajout_mot(arbrecourant->suite[calculcase(mot[i])],mot,i);
    }

}

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);
            
        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;
        ajout_mot(&arbrecourant,mot,i);
    }
    return ;
}


void free_arbre(struct arbre *pt_arbre){
   if (pt_arbre==NULL){
        return ;
    }
    for(int i=0;i<26;i++){
        free_arbre(pt_arbre->suite[i]);
    }
    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(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]);
    }
}

void affiche_dico(struct dico *dico){
    if(dico==NULL){
        return ;
    }
    printf("---------------------------------------\n");
    for(int i=0;i<26;i++){
        printf("%d:",i);
        affiche_arbre(dico->alpha[i]);
        printf("\n");
    }
   
}


int main (){
    FILE* fp = fopen("dicotest.txt","r"); //amelioration entrée
	if(fp == NULL){ return EXIT_FAILURE;} //File is not readable
	
    struct dico *mondico=malloc(sizeof(struct dico));
    charger_arbre(fp,&mondico);
    affiche_dico(mondico);
    
    
    free(mondico);
    fclose(fp);    
    return 0;
}