diff --git a/tp-huffman/hufftree.c b/tp-huffman/hufftree.c index 9cff40d..affed94 100644 --- a/tp-huffman/hufftree.c +++ b/tp-huffman/hufftree.c @@ -1,4 +1,6 @@ #include "hufftree.h" +#include "error.h" +#include /************************************/ /* Primitives des arbres de Huffman */ @@ -6,43 +8,60 @@ /* Création d'une feuille */ huffnode *create_huffleaf(int byte, int freq) { - // À écrire - return NULL; + huffnode *h = malloc(sizeof(huffnode)); + h -> freq = 0; + h -> byte = 0; + h -> leftchild = NULL; + h -> rightchild = NULL; + return h; } /* Fusion de deux arbres avec un nouveau noeud racine */ huffnode *merge_hufftree(huffnode *pl, huffnode *pr) { - // À écrire - return NULL; + huffnode *h = create_huffleaf(0, pl->freq + pr->freq); + h->leftchild = pl; + h->rightchild = pr; + return h; } /* Teste si un noeud est une feuille */ bool isleaf_huffnode(huffnode *p) { - // À écrire - return true; + return p->leftchild == NULL && p->rightchild == NULL; } /* Retourne la valeur de l'octet correspondant à un noeud */ int getbyte_huffnode(huffnode *p) { - // À écrire - return 0; + if (isleaf_huffnode(p)) { + ERROR("getbyte_huffnode: noeud feuille attendu\n"); + exit(1); + } + return p->byte; } /* Retournent les fils d'un noeud */ huffnode *getleft_huffnode(huffnode *p) { - // À écrire - return NULL; + if (isleaf_huffnode(p)) { + ERROR("getleft_huffnode: noeud interne attendu\n"); + exit(1); + } + return p->leftchild; } huffnode *getright_huffnode(huffnode *p) { - // À écrire - return NULL; + if (isleaf_huffnode(p)) { + ERROR("getright_huffnode: noeud interne attendu\n"); + exit(1); + } + return p->rightchild; } /* Libération d'un arbre */ void free_hufftree(huffnode *p) { - // À écrire + if (p == NULL) return; + free_hufftree(p -> leftchild); + free_hufftree(p -> rightchild); + free(p); } /**********************************************/ @@ -51,15 +70,20 @@ void free_hufftree(huffnode *p) { /* Comparaison de deux arbres */ bool compare_hufftree(void *p1, void *p2) { - // À écrire - return true; + huffnode *v1 = (huffnode*)p1; + huffnode *v2 = (huffnode*)p2; + if (v1 == NULL || v2 == NULL) { + ERROR("compare_hufftree: NULL pointeur\n"); + exit(1); + } + return v1->freq < v2->freq; } /* Création de l'arbre de Huffman à partir du fichier à compresser */ huffnode *datafile_to_hufftree(FILE *input) { /* Phase 1: création du tableau de fréquences */ - // À écrire + int *tab = malloc(256 * sizeof(int)); /* Phase 2: intialisation de la file de priorité à partir du tableau de * fréquences */ diff --git a/tp-huffman/hufftree.o b/tp-huffman/hufftree.o index b6f41be..3dda154 100644 Binary files a/tp-huffman/hufftree.o and b/tp-huffman/hufftree.o differ diff --git a/tp-huffman/main.o b/tp-huffman/main.o index e060e5a..c1f81d2 100644 Binary files a/tp-huffman/main.o and b/tp-huffman/main.o differ diff --git a/tp-huffman/tp b/tp-huffman/tp index e8f3916..3f68ef2 100755 Binary files a/tp-huffman/tp and b/tp-huffman/tp differ