Yûki - Update Insert and rename rechercher fct

This commit is contained in:
name 2019-12-13 00:26:12 +01:00
parent 5e43344955
commit 959f1b5938
No known key found for this signature in database
GPG key ID: 03E8F3CF3183323A
5 changed files with 58 additions and 38 deletions

View file

@ -68,7 +68,7 @@ void free_arbre(arbre a)
} }
} }
char *rechercher (arbre a, char c, char*s) char *arbre_rechercher(arbre a, char c, char*s)
{ {
if (racine(a)==c) if (racine(a)==c)
{ {
@ -76,10 +76,10 @@ char *rechercher (arbre a, char c, char*s)
} }
if (!est_arbre_vide(fils_gauche(a))) if (!est_arbre_vide(fils_gauche(a)))
{ {
rechercher (fils_gauche(a),c,s+'0') ; arbre_rechercher(fils_gauche(a),c,s+'0') ;
} }
if (!est_arbre_vide(fils_droit(a))) if (!est_arbre_vide(fils_droit(a)))
{ {
rechercher (fils_droit(a),c,s+'1') ; arbre_rechercher(fils_droit(a),c,s+'1') ;
} }
} }

View file

@ -21,5 +21,5 @@ bool est_arbre_vide(arbre a);
arbre creer_feuille(Elt e, int p) ; arbre creer_feuille(Elt e, int p) ;
bool est_feuille(arbre a) ; bool est_feuille(arbre a) ;
char *arbre_rechercher(arbre a, char c, char*s);
#endif #endif

View file

@ -19,32 +19,43 @@ Freq ajouter(int nb, int lettre, Freq l){
return tmp; return tmp;
} }
Freq inserer(int nb, int lettre, Freq l){ Freq inserer(Occ *place, Freq l){
printf("Inserer :\n"); printf(" Inserer :\n");
if(est_liste_vide(l)){ if(est_liste_vide(l)){
printf("Liste vide :\n"); printf(" Liste vide :\n");
return ajouter(nb,lettre,creer_liste_vide()); return ajouter(place->nb,place->lettre,creer_liste_vide());
} }
else{ else{
printf("Liste non vide :\n"); printf(" Liste non vide :\n");
Occ * tmp; Occ * tmp;
tmp = NULL; tmp = l;
Freq new,csl,elem; if(tmp->suiv == NULL){
new = l; if(tmp->nb < place->nb){
elem->nb=nb; tmp->suiv=place;
elem->lettre=lettre; place->suiv=NULL;
printf("Boucle : freq : \n",tete_freq(new)); }else{
while( new && tete_freq(new)<nb){ place->suiv=tmp;
printf("nb: %d / freq : %d \n",nb,tete_freq(new)); l=place;
tmp=new; }
new = queue(new); return l;
} }
printf("Fin boucle :\n"); while(tmp->suiv != NULL){
elem->suiv=csl; if(tmp->suiv->nb >= place->nb){
tmp->suiv=elem; place->suiv=tmp->suiv;
return tmp; tmp->suiv=place;
return l;
}
else{
tmp=tmp->suiv;
}
}
// Ajoute à la fin
while(l->suiv != NULL){
l = l->suiv;
}
l->suiv=place;
return l;
} }
} }
int tete_lettre(Freq l){ int tete_lettre(Freq l){
@ -76,7 +87,7 @@ Freq liberer_maillon(Freq l){
return tmp; return tmp;
} }
int rechercher(int lettre,Freq l){ int liste_rechercher(int lettre,Freq l){
if(est_liste_vide(l)){ if(est_liste_vide(l)){
return 0; return 0;
} }
@ -84,7 +95,7 @@ int rechercher(int lettre,Freq l){
return 1; return 1;
} }
else{ else{
return rechercher(lettre,queue(l)); return liste_rechercher(lettre,queue(l));
} }
} }
@ -104,3 +115,12 @@ Freq incrementer(int lettre, Freq l){
return l; return l;
} }
} }
void afficher(Freq L){
printf("Affichage ...\n");
while(queue(L) != NULL){
printf(" P : %d | Char : %d\n",tete_freq(L),tete_lettre(L));
L=queue(L);
}
printf("Fin Affichage.\n");
}

View file

@ -11,13 +11,13 @@ typedef struct zoccurence * Freq;
Freq creer_liste_vide(); Freq creer_liste_vide();
int est_liste_vide(Freq l); int est_liste_vide(Freq l);
Freq ajouter(int nb, int lettre, Freq l); Freq ajouter(int nb, int lettre, Freq l);
Freq inserer(int nb, int lettre, Freq l); Freq inserer(Occ *place, Freq l);
int tete_lettre(Freq l); int tete_lettre(Freq l);
int tete_freq(Freq l); int tete_freq(Freq l);
Freq queue(Freq l); Freq queue(Freq l);
void liberer_liste(Freq l); void liberer_liste(Freq l);
Freq liberer_maillon(Freq l); Freq liberer_maillon(Freq l);
int rechercher(int lettre, Freq l); int liste_rechercher(int lettre, Freq l);
Freq incrementer(int lettre, Freq l); Freq incrementer(int lettre, Freq l);
void afficher(Freq L);
#endif #endif

View file

@ -49,7 +49,7 @@ Freq freq_apparition(FILE *file){
text = creer_liste_vide(); text = creer_liste_vide();
int c; int c;
while((c=fgetc(file))!=EOF){ while((c=fgetc(file))!=EOF){
if(rechercher(c,text)==1){ if(liste_rechercher(c,text)==1){
text=incrementer(c,text); text=incrementer(c,text);
} }
else{ else{
@ -73,8 +73,10 @@ arbre huffman(arbre H, Freq L){
} }
int i; int i;
i=0; i=0;
while((L->suiv != NULL)||i<50){ while((L->suiv->suiv != NULL)||i<50){
printf("Test n°%d \n",i++); printf("Test n°%d \n",i++);
l=L;
afficher(l);
int al,bl,ap,bp; int al,bl,ap,bp;
al = tete_lettre(L); al = tete_lettre(L);
ap = tete_freq(L); ap = tete_freq(L);
@ -87,15 +89,13 @@ arbre huffman(arbre H, Freq L){
fd=creer_feuille(bl,bp); fd=creer_feuille(bl,bp);
H=creer_arbre_huffman(0,ap+bp,fg,fd); H=creer_arbre_huffman(0,ap+bp,fg,fd);
printf("Test insérer\n"); printf("Test insérer\n");
L=inserer(ap+bp,0,L);
printf("Affichage vérification\n");
l=L; l=L;
while(!est_liste_vide(l)){ afficher(l);
printf("%d (%d)\n",tete_lettre(l),tete_freq(l)); L=inserer(ajouter(0,ap+bp,creer_liste_vide()),L);
l=queue(l); l=L;
} afficher(l);
printf("Affichage vérification\n");
} }
return H; return H;
} }