简体   繁体   English

霍夫曼代码说明Java

[英]Huffman code Explanation Java

I'm new in java and I'm trying to understand Huffman coding by using a code online. 我是Java的新手,我想通过在线使用代码来了解Huffman编码。 I'm messing with the code to understand how it works cause I did not find anything on how to implement a Huffman code. 我弄乱了代码以了解它是如何工作的,因为我没有找到有关如何实现霍夫曼代码的任何信息。 I need to understand why in this code the guy used comparable in Huffman tree class and string buffer. 我需要了解为什么在这段代码中,那个家伙在霍夫曼树类和字符串缓冲区中使用了可比性。 If someone knows any good explanation of Huffman coding online or even an algorithm, please. 如果有人对在线霍夫曼编码甚至算法有很好的解释,请。 I really need to understand this code. 我真的需要了解这段代码。 PS: English is not my native language so sorry for any confusion. 附言:英语不是我的母语,对于任何混淆,我们深表歉意。 Thank you 谢谢

import java.util.*;

public class HuffmanCode {
    // input is an array of frequencies, indexed by character code
    public HuffmanTree buildTree(int[] charFreqs) {
        PriorityQueue<HuffmanTree> trees = new PriorityQueue<HuffmanTree>();
        // initially, we have a forest of leaves
        // one for each non-empty character
        for (int i = 0; i < charFreqs.length; i++)
            if (charFreqs[i] > 0)
                trees.offer(new HuffmanLeaf(charFreqs[i], (char)i));

        assert trees.size() > 0;
        // loop until there is only one tree left
        while (trees.size() > 1) {
            // two trees with least frequency
            HuffmanTree a = trees.poll();
            HuffmanTree b = trees.poll();

            // put into new node and re-insert into queue
            trees.offer(new HuffmanNode(a, b));
        }
        return trees.poll();
    }

    public void printCodes(HuffmanTree tree, StringBuffer prefix) {
        assert tree != null;
        if (tree instanceof HuffmanLeaf) {
            HuffmanLeaf leaf = (HuffmanLeaf)tree;

            // print out character, frequency, and code for this leaf (which is just the prefix)
            System.out.println(leaf.value + "\t" + leaf.frequency + "\t" + prefix);

        } else if (tree instanceof HuffmanNode) {
            HuffmanNode node = (HuffmanNode)tree;

            // traverse left
            prefix.append('0');
            //prefix = prefix + "0";
            printCodes(node.left, prefix);
            prefix.deleteCharAt(prefix.length()-1);

            // traverse right
            prefix.append('1');
            printCodes(node.right, prefix);
            prefix.deleteCharAt(prefix.length()-1);
        }
    }
}

Huffman tree class: 霍夫曼树类:

public class HuffmanTree implements Comparable<HuffmanTree> {
    public final int frequency; // the frequency of this tree

    public HuffmanTree(int freq) {
        frequency = freq;
    }

    // compares on the frequency
    public int compareTo(HuffmanTree tree) {
        return frequency - tree.frequency;
    }
}

Huffman leaf: 霍夫曼叶:

class HuffmanLeaf extends HuffmanTree {

    public final char value; // the character this leaf represents

    public HuffmanLeaf(int freq, char val) {
        super(freq);
        value = val;
    }
}

Huffman node: 哈夫曼节点:

class HuffmanNode extends HuffmanTree {

    public final HuffmanTree left, right; // subtrees

    public HuffmanNode(HuffmanTree l, HuffmanTree r) {
        //Calling the super  constructor HuffmanTree 
        super(l.frequency + r.frequency);
        left = l;
        right = r;
    }

}

Main: 主要:

public class Main {

    public static void main(String[] args) {
        String test = "Hello World";
        HuffmanCode newCode = new HuffmanCode();

        // we will assume that all our characters will have
        // code less than 256, for simplicity
        int[] charFreqs = new int[256];
        // read each character and record the frequencies
        for (char c : test.toCharArray())
            charFreqs[c]++;

        // build tree
        ////HuffmanTree tree = buildTree(charFreqs);
        HuffmanTree tree = newCode.buildTree(charFreqs);

        // print out results
        System.out.println("SYMBOL\tWEIGHT\tHUFFMAN CODE");
        newCode.printCodes(tree, new StringBuffer());
    }

}

why did the guy used Stringbuffer 那家伙为什么用Stringbuffer

Because building a string using one is the preferred over string of concatenation. 因为使用一个构建字符串比连接字符串更可取。

StringBuilder vs String concatenation in toString() in Java Java中toString()中的StringBuilder vs String串联

When to use StringBuilder in Java 何时在Java中使用StringBuilder

etc... 等等...

StringBuilder is somewhat different than StringBuffer StringBuilder与StringBuffer有所不同

Why use StringBuilder? 为什么要使用StringBuilder? StringBuffer can work with multiple thread as well as one thread? StringBuffer可以同时使用多个线程吗?

and comparable 和可比的

Because a priority queue is used. 因为使用了优先级队列。 It needs that interface. 它需要该接口。

How do I use a PriorityQueue? 如何使用PriorityQueue?

And, reading the Huffman coding Wikipedia page (which you can do to understand the algorithm), it is mentions that the optimal structure of the encoding is ordered. 并且,阅读霍夫曼编码Wikipedia页面(您可以做以了解算法),提到编码的最佳结构是有序的。 I don't know the algorithm, personally, but I will recommend not copying code off of the internet you don't understand. 我个人不知道该算法,但是我建议不要从您不了解的互联网上复制代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM