diff --git a/arbre_de_codage/arbre_binaire.c b/arbre_de_codage/arbre_binaire.c index 910168e..13ecb04 100644 --- a/arbre_de_codage/arbre_binaire.c +++ b/arbre_de_codage/arbre_binaire.c @@ -31,7 +31,7 @@ arbre fils_droit(arbre b) return b->fils_droit; } -int racine(arbre a) +Elt racine(arbre a) { assert(!est_arbre_vide(a)); return a->elt; @@ -69,23 +69,19 @@ void free_arbre(arbre a) } } -char *arbre_rechercher(arbre a, Elt c, char s[]) -{ - if(a->elt == c){ - return s; - } - if (!est_arbre_vide(fils_gauche(a))) - { - char c1[]="0"; - strcat(s,c1); - printf("0"); - arbre_rechercher(fils_gauche(a),c,s); - } - if (!est_arbre_vide(fils_droit(a))) - { - char c2[]="1"; - strcat(s,c2); - printf("1"); - arbre_rechercher(fils_droit(a),c,s); +void arbre_rechercher(arbre a, Elt c, char s[], int s_len, int found[]){ + if(!est_arbre_vide(a)){ + if(est_feuille(a)&&racine(a)==c){ + found[0]=1; + return ; + } + if(!est_arbre_vide(fils_gauche(a))&&(found[0]==0)){ + s[s_len]='0'; + arbre_rechercher(fils_gauche(a),c,s,s_len+1,found); + } + if(!est_arbre_vide(fils_droit(a))&&(found[0]==0)){ + s[s_len]='1'; + arbre_rechercher(fils_droit(a),c,s,s_len+1,found); + } } } \ No newline at end of file diff --git a/arbre_de_codage/arbre_binaire.h b/arbre_de_codage/arbre_binaire.h index 83410c4..b7649a9 100644 --- a/arbre_de_codage/arbre_binaire.h +++ b/arbre_de_codage/arbre_binaire.h @@ -22,9 +22,9 @@ arbre creer_arbre_vide(void); arbre creer_arbre_huffman(Elt e, int p, arbre fg, arbre fd); arbre fils_gauche(arbre a); arbre fils_droit(arbre a); -int racine(arbre a); +Elt racine(arbre a); bool est_arbre_vide(arbre a); arbre creer_feuille(Elt e, int p) ; bool est_feuille(arbre a) ; -char *arbre_rechercher(arbre a, Elt c, char s[]); +void arbre_rechercher(arbre a, Elt c, char s[], int s_len, int found[]); #endif diff --git a/main_compress.c b/main_compress.c index ef1d6e5..e5ff134 100644 --- a/main_compress.c +++ b/main_compress.c @@ -41,13 +41,17 @@ int main(int argc, char **argv){ // Récupération du code de chaque charactere printf("Code : \n"); - code C[ASCII_EXT]; - - char s[]=""; - arbre_rechercher(huff,'C',s); - printf("\nfin test\n"); - printf("%s \n",s); - printf("FIN\n"); + char s[8]="2"; + int f[1]={0}; + char search=' '; + arbre_rechercher(huff,search,s,0,f); + if(f[0]==1){ + printf("\nTrouve : %s\n",s); + } + else{ + printf("\nPas Trouve\n"); + } + printf("\nFIN TEST\n"); return 0; } @@ -119,7 +123,7 @@ arbre huffman(arbre T[]){ Index=i; while(Indexelt=NULL; + tmp->elt=-1; tmp->fils_gauche=T[Index]; tmp->fils_droit=T[Index+1]; tmp->poids=T[Index]->poids +T[Index+1]->poids;