Les Snippets

Connexion

huffman : arbre et chaine de binaire

Niveau requis pour utiliser/comprendre cette source : 2 ( Initié )
Créé le 15/03/2008 18:45:04 et initié par coucou747 [Liste]
Vue : 3234
Catégorie(s) : Compression & Split, Maths, Algorithme, Chaîne de caractères
Langages dispo pour ce code :
- Java
- ObjectiveCaml
- C
- Voir tous les langages pour ce code snippet



Langage : C
Date ajout : 07/04/2008
Posté par coucou747 [Liste]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct huffarbre {
    char l;
    int n;
    struct huffarbre *g, *d;
};
struct hal {
    struct huffarbre *ha;
    struct hal *next;
};
void sort_chaine(char * liste, int l){
    int i, j; char a;
    for (i=0;i<l;i++)
        for (j=i+1;j<l;j++)
            if (liste[i] > liste[j]){
                a=liste[i]; liste[i]=liste[j]; liste[j]=a;
            }
}
void ajouter(struct huffarbre * ha, struct hal * li, struct hal **pred){
    if (li->ha == NULL){
        li->ha = ha;
    }else{
        if (ha->n < li->ha->n){
            struct hal *li2 = malloc(sizeof(struct hal));
            li2->ha=ha;
            li2->next = li;
            if (*pred) (*pred)->next=li2;
            else *pred = li2;
        }else if (li->next == NULL){
            li->next = malloc(sizeof(struct hal));
            li->next->next=NULL;
            li->next->ha=ha;
        }else{
            ajouter(ha, li->next, &li);
        }
    }
}
void arbreliste ( char * chaine, int l, struct hal ** li){
    struct hal *first=NULL;
    int i, j;
    struct huffarbre *ha;
    for (i=0;i<l;i++){
        j=i;
        while (chaine[i]==chaine[j] && j<l) j++;
        ha = malloc(sizeof (struct huffarbre));
        ha->l=chaine[i]; ha->n=j-i; ha->g=NULL; ha->d=NULL;
        ajouter(ha, *li, &first);
        if (first) (*li)=first;
        i=j-1;
    }
}
void reduire_arbre(struct hal *first, struct huffarbre **arbre){
    struct hal *a, *b, *of;
    struct huffarbre *ha;
    of = first; // original first
    while (1){
        a=first;
        b=first->next; first=b->next;
        ha = malloc(sizeof (struct huffarbre));
        ha->l=0;
        ha->n=a->ha->n + b->ha->n;
        ha->g = a->ha; ha->d = b->ha;
        if (first==NULL) break;
        ajouter(ha, first, &first);
    }
    while (of){
        a=of;
        of=of->next;
        free(a);
    }
    *arbre = ha;
}
// affichage de la liste
void affliste(struct hal *li){
    struct hal *it_li;
    for (it_li = li; it_li!=NULL; it_li=it_li->next){
        printf("%c, %d\n", it_li->ha->l, it_li->ha->n);
    }
}
// affichage de l'arbre
void affarbre(struct huffarbre *ha, int t){
    int i;
    for (i=0;i<t-1;i++) printf("  |");
    if (t!=0) printf("---");
    if (ha->l){
        printf("%c, %d\n", ha->l, ha->n);
    }else{
        printf("\n");
        t++;
        affarbre(ha->d, t);
        affarbre(ha->g, t);
    }
}
// liberation de la memoire
void freearbre(struct huffarbre *ha){
    if (!ha->l){
        freearbre(ha->g);
        freearbre(ha->d);
    }
    free(ha);
}
// on recupere le code du caractere c, dans *chaine a partir du bit b, au format binaire. renvoie le nombre de bits utilises
int getbinarychainefor(struct huffarbre *ha, char c, char * chaine, int b){
    int l;
    if (ha->l){
        if (c==ha->l){
            return b;
        }else{
            return 0;
        }
    }else{
        if (b%8==0) *(chaine + b/8) = 0;
        if (l=getbinarychainefor(ha->g, c, chaine, b+1)){
            *(chaine + b/8)|=1 << (b%8);
            return l;
        }else if (l=getbinarychainefor(ha->d, c, chaine, b+1)){
            return l;
        }else{
            return 0;
        }
    }
}
// on recupere le code de la chaine *chaine, dans *out, au format binaire. renvoie le nombre de bits utilises
int getbinarychainefromString(struct huffarbre *ha, char * chaine, char *out){
    int l=strlen(chaine);
    int i, j=0;
    for (i=0;i<l;i++){
        j=getbinarychainefor(ha, chaine[i], out, j);
    }
    return j;
}
int main(){
    char chaine[]="test ok arbre test";
    int l=strlen(chaine);
    struct hal *li=malloc(sizeof(struct hal));
    struct huffarbre *ha;
    li->next=NULL; li->ha=NULL;
    sort_chaine(chaine, l);
    arbreliste(chaine, l, &li);
    //affliste(li);
    reduire_arbre(li, &ha);
    //affarbre(ha, 0);
// l'affichage des resultats du test
    l=getbinarychainefromString(ha, "ttb", chaine);
    printf("taille : %d\n", l);
    for (;l>0;l-=8){
        printf("%hhx", *(chaine+(l-1)/8));
    }
    printf("\n");
    freearbre(ha);
    return 0;
}


Snippets en rapport avec : Compression, Binaire, Huffman, Arbre



Codes sources en rapport avec : Compression, Binaire, Huffman, Arbre

{PHP} PHP5 CLASSE ARBRE INVERSÉ (HUFFMAN) COMPRESSION DECOMPRESSION
Ce script de compression-décompression créé une arborescence (treeItems)en partant des feuilles et n...

{JAVA / J2EE} CODAGE ET ARBRE DE HUFFMAN (COMPRESSION, AFFICHAGE DE L'ARBRE, INTERFACE GRAPHIQUE...)
Ce code pemert d'encoder les données sous la forme d'une chaîne de caractères. Le principe...

{C / C++ / C++.NET} LZZ HUFFMAN COMPRESSION
facile a utiliser pour compresser les fichier ou un buffer on ini au debut CLzhCompress *G; ...

{C / C++ / C++.NET} [C / WIN32] COMPRESSION HUFFMAN
Je cherchais depuis un moment un algo huffman facile a utiliser et j'ai fini par trouver mon bonheur...

{Delphi} HUFFMAN ADAPTATIF
Exemple de Compression / Decompression avec l'algorythme Huffman Adaptatif en Trubo Pascal......

{Delphi} HUFFMAN
Exemple de Compression / Decompression avec l'algorythme Huffman en Trubo Pascal......

{PHP} EVALUER UNE EXPRESSION MATHEMATIQUE, UTILISATION D'UN ARBRE BINAIRE
Suite a de nombreuses sources sur ce sujet, j'ai ete attriste de voir sur sur phpcs, on ne trouvait ...

{C# / C#.NET} PRIORITY QUEUE
Priority Queue C# J'imagine que tout le monde connait la class Queue du framework qui permet de f...

{Visual Basic, VB6, VB.NET, VB 2005} .NET ALGO HUFFMAN RLE MTF
Il y a 1 ans j'ai essayé de me lancer dans la compression des données mais sans succès, aujourd'hui...

{JAVA / J2EE} TRI DE LIST EN UTILISANT LES ABR
Bonne classe BinaryTree pour la manip des arbres binaires de recherches(fonctionnalités de base). c...