Yûki - Huffman

This commit is contained in:
name 2019-12-14 22:15:45 +01:00
parent 6c967c6fac
commit 7acf2f20fa
No known key found for this signature in database
GPG key ID: 03E8F3CF3183323A
4 changed files with 38 additions and 17 deletions

View file

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <string.h>
#include "arbre_binaire.h" #include "arbre_binaire.h"
arbre creer_arbre_vide (void) arbre creer_arbre_vide (void)
@ -8,7 +9,7 @@ arbre creer_arbre_vide (void)
return NULL ; return NULL ;
} }
arbre creer_arbre_huffman(int e, int p, arbre fg, arbre fd) arbre creer_arbre_huffman(Elt e, int p, arbre fg, arbre fd)
{ {
noeud * tmp = malloc(sizeof(noeud)); noeud * tmp = malloc(sizeof(noeud));
tmp->elt=e; tmp->elt=e;
@ -41,7 +42,7 @@ bool est_arbre_vide(arbre a)
return (a == NULL) ; return (a == NULL) ;
} }
arbre creer_feuille(int e, int p) arbre creer_feuille(Elt e, int p)
{ {
return creer_arbre_huffman(e, p, creer_arbre_vide(), creer_arbre_vide()) ; return creer_arbre_huffman(e, p, creer_arbre_vide(), creer_arbre_vide()) ;
} }
@ -68,18 +69,23 @@ void free_arbre(arbre a)
} }
} }
char *arbre_rechercher(arbre a, char c, char*s) char *arbre_rechercher(arbre a, Elt c, char s[])
{ {
if (racine(a)==c) if(a->elt == c){
{ return s;
return s ;
} }
if (!est_arbre_vide(fils_gauche(a))) if (!est_arbre_vide(fils_gauche(a)))
{ {
arbre_rechercher(fils_gauche(a),c,s+'0') ; char c1[]="0";
strcat(s,c1);
printf("0");
arbre_rechercher(fils_gauche(a),c,s);
} }
if (!est_arbre_vide(fils_droit(a))) if (!est_arbre_vide(fils_droit(a)))
{ {
arbre_rechercher(fils_droit(a),c,s+'1') ; char c2[]="1";
strcat(s,c2);
printf("1");
arbre_rechercher(fils_droit(a),c,s);
} }
} }

View file

@ -1,9 +1,10 @@
#ifndef __ARBRE_BINAIRE__ #ifndef __ARBRE_BINAIRE__
#define __ARBRE_BINAIRE__ #define __ARBRE_BINAIRE__
typedef char Elt;
typedef int bool; typedef int bool;
struct znoeud { struct znoeud {
int elt ; char elt ;
int poids; int poids;
struct znoeud *fils_gauche; struct znoeud *fils_gauche;
struct znoeud *fils_droit; struct znoeud *fils_droit;
@ -11,14 +12,19 @@ struct znoeud {
typedef struct znoeud noeud ; typedef struct znoeud noeud ;
typedef struct znoeud * arbre; typedef struct znoeud * arbre;
typedef struct zcode{
int lettre;
char *s;
} codage;
typedef struct zcode * code;
arbre creer_arbre_vide(void); arbre creer_arbre_vide(void);
arbre creer_arbre_huffman(int e, int p, arbre fg, arbre fd); arbre creer_arbre_huffman(Elt e, int p, arbre fg, arbre fd);
arbre fils_gauche(arbre a); arbre fils_gauche(arbre a);
arbre fils_droit(arbre a); arbre fils_droit(arbre a);
int racine(arbre a); int racine(arbre a);
bool est_arbre_vide(arbre a); bool est_arbre_vide(arbre a);
arbre creer_feuille(Elt e, int p) ;
arbre creer_feuille(int e, int p) ;
bool est_feuille(arbre a) ; bool est_feuille(arbre a) ;
char *arbre_rechercher(arbre a, char c, char*s); char *arbre_rechercher(arbre a, Elt c, char s[]);
#endif #endif

View file

@ -1 +1 @@
DDDCCCBBBBBAAAAAA Ceci est un test.

View file

@ -38,7 +38,16 @@ int main(int argc, char **argv){
arbre huff; arbre huff;
printf("Huffman : \n"); printf("Huffman : \n");
huff = huffman(N); huff = huffman(N);
printf("Arbre :\n");
// 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");
return 0; return 0;
} }
@ -108,13 +117,13 @@ arbre huffman(arbre T[]){
} }
int Index; int Index;
Index=i; Index=i;
while(Index<(ASCII_EXT-1)){ while(Index<ASCII_EXT-1){
arbre tmp=malloc(sizeof(noeud*)); arbre tmp=malloc(sizeof(noeud*));
tmp->fils_gauche=T[Index]; tmp->fils_gauche=T[Index];
tmp->fils_droit=T[Index+1]; tmp->fils_droit=T[Index+1];
tmp->poids=T[Index]->poids +T[Index+1]->poids; tmp->poids=T[Index]->poids +T[Index+1]->poids;
T[Index+1]=tmp; T[Index+1]=tmp;
printf("%d\n",tmp->poids); printf("%d %c %c\n",tmp->poids,tmp->fils_gauche->elt,tmp->fils_droit->elt);
Index++; Index++;
tri_tab(T,ASCII_EXT); tri_tab(T,ASCII_EXT);
} }