Save a frequency table to a file. (Huffman compactor)

Asked

Viewed 131 times

0

Hello,

I’m developing a work of Huffman’s algorithm, and I’m having some difficulty working with the frequency table. In short, I need to turn the characters of a text into a frequency table to create the tree of Huffman.

example: pineapple... The table has to have:

 a = 3
 b = 1
 c = 1
 x = 1
 i = 1

I’m using this code:

int[] charFreqs = new int[256];
    for (char c : test.toCharArray())
        charFreqs[c]++;

It creates the table and it is possible to mount the tree in this way:

    public static HuffmanTree buildTree(int[] charFreqs) {
    // Cria uma Fila de Prioridade 
    // A Fila será criado pela ordem de frequência da letra no texto
    PriorityQueue<HuffmanTree> trees = new PriorityQueue<HuffmanTree>();
    // Criar as Folhas da Árvore para cada letra 
    for (int i = 0; i < charFreqs.length; i++){
        if (charFreqs[i] > 0)
            trees.offer(new HuffmanLeaf(charFreqs[i], (char)i)); // Inser os elementos, nó folha, na fila de prioridade
    }
    // Percorre todos os elementos da fila
    // Criando a árvore binária de baixo para cima
    while (trees.size() > 1) {
        // Pega os dois nós com menor frequência
        HuffmanTree a = trees.poll(); // poll - Retorna o próximo nó da Fila ou NULL se não tem mais
        HuffmanTree b = trees.poll(); // poll - Retorna o próximo nó da Fila ou NULL se não tem mais

        // Criar os nós da árvore binária
        trees.offer(new HuffmanNode(a, b)); 
    }
    // Retorna o primeiro nó da árvore
    return trees.poll();

the problem is that for me to unzip a file, you need to read this table again and recreate the tree, then it is necessary to save it in a text file or any other way, I am trying with txt... however when I read the txt and try to assemble the table again, the tree is not reassembled, it is empty...

I used this program as a basis: https://drive.google.com/file/d/0B-J3IzsJPySmd0FzNzBVeGdtdGM/view

Could someone help me? Maybe there’s something I don’t understand...

  • Can you put your txt r/w code here? I can’t access the drive.

  • I don’t understand your problem. What exactly are you failing to do? I took your code and made an example using data from a txt file. The algorithm worked normally.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.