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 : 1541
Catégorie(s) : Compression & Split, Maths, Algorithme, Chaîne de caractères
Langage sélectionné : Java
Langages dispo pour ce code :
- Java
- ObjectiveCaml
- C
- Voir tous les langages pour ce code snippet



Langage : Java
Date ajout : 15/03/2008
Posté par coucou747 [Liste]

class Lettre{
    public Lettre(char l, int f){ this.l=l; this.f=f; }
    public char getL(){ return l; }
    public int getF(){ return f; }
    public String toString(){
        return this.l+" => "+this.f;
    }
    public void add(int i){ this.f+=i; }
    private char l;
    private int f; // le nombre d'apparitions
}
class HuffArbre /* un arbre binaire */{
    public HuffArbre(Lettre l){this.feuille=l;}
    public HuffArbre(HuffArbre droite, HuffArbre gauche){
        this.droite=droite; this.gauche=gauche;
    }
    public String toString(){
        if (this.feuille!=null){
            return "Feuille("+this.feuille+")";
        }else{
            return "Branche("+this.droite+", "+this.gauche+")\n";
        }
    }
    public void add(HuffArbre a){
        if (this.feuille!=null){
            this.feuille.add(a.count());
        }else{
            System.out.println("tey null");
        }
    }
    public char getL(){
        if (this.feuille!=null){
            return this.feuille.getL();
        }else{
            return this.droite.getL();
        }
    }
    public int count(){
        if (this.feuille!=null){
            return this.feuille.getF();
        }else{
            return this.droite.count()+this.gauche.count();
        }
    }
    public String getSeq(char c){
        if (this.feuille != null){
            if (this.feuille.getL() == c){
                return "";
            }else{
                return "ERR";
            }
        }else{
            String outb, outa = this.droite.getSeq(c);
            if (outa.equals("ERR")){
                outb=this.gauche.getSeq(c);
                if (outb.equals("ERR")){
                    return "ERR";
                }else{
                    return outb.concat("1");
                }
            }else
                return outa.concat("0");
        }
    }
    private Lettre feuille;
    private HuffArbre droite;
    private HuffArbre gauche;
}
class HuffArbreList extends java.util.LinkedList<HuffArbre>{
    public HuffArbreList(){
    }
    public HuffArbreList(String str){
        for (int i=0;i<str.length();i++){
            this.insertSorted(new HuffArbre(new Lettre(str.charAt(i), 1)));
        }
    }
    public void insertSorted(HuffArbre e){
        int i=0, s=this.size();
        while (i<s && cmp(this.get(i), e)) i++;
        this.add(i, e);
    }
    public boolean cmp(HuffArbre a, HuffArbre b){
        if (this.sortLetter){
            return a.getL() <= b.getL();
        }else{
            return a.count() <= b.count();
        }
    }
    public HuffArbreList grouper(){
        HuffArbre e1, e2;
        HuffArbreList l=new HuffArbreList();
        l.sortLetter=false;
        e1=this.removeFirst();
        int s=this.size();
        for (int i=0;i<s;i++){
            e2=this.removeFirst();
            if (e1.getL()==e2.getL()){
                e1.add(e2);
            }else{
                l.insertSorted(e1);
                e1=e2;
            }
        }
        l.add(e1);
        return l;
    }
    public void faireArbre(){
      if (this.size()==1) return;
      HuffArbre e1, e2;
      e1=this.removeFirst();
      e2=this.removeFirst();
      this.insertSorted(new HuffArbre(e1, e2));
      faireArbre();
    }
    public boolean sortLetter=true;
}
public class Main {
    public static void main(String[] args) {
        HuffArbreList liste=new HuffArbreList("test ok arbre test");
        liste=liste.grouper();
        liste.faireArbre();
        HuffArbre arbre=liste.removeFirst();
        System.out.println(arbre);
        System.out.println(arbre.getSeq('t'));
    }
}


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...