2016-12-03 102 views
-1

我是新來的java,我想通過在線使用代碼來理解哈夫曼編碼。我搞亂了代碼來理解它是如何工作的,因爲我沒有發現如何實現霍夫曼代碼。 我需要理解爲什麼在這段代碼中,這個人在霍夫曼樹類和字符串緩衝區中使用可比。 如果有人知道在線霍夫曼編碼甚至算法的任何好解釋,請。 我真的需要了解這段代碼。 PS:英語不是我的母語,所以很抱歉有任何混淆。 謝謝霍夫曼代碼解釋Java

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); 
     } 
    } 
} 

哈夫曼樹類:

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; 
    } 
} 

霍夫曼葉:

class HuffmanLeaf extends HuffmanTree { 

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

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

霍夫曼節點:

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; 
    } 

} 

主營:

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()); 
    } 

} 
+0

將打印語句添加到代碼或使用調試器並逐行運行,直到您認爲您明白 –

+0

有人可以解釋我爲什麼這個人使用Stringbuffer和可比較的嗎? –

+0

不應該使用Stackoverflow作爲社區努力來理解您發現的一些隨機代碼,對不起。請嘗試運行代碼並找出它 –

回答

2

何必當初使用StringBuffer的

由於建設使用一個字符串是串聯的優於串的傢伙。

StringBuilder vs String concatenation in toString() in Java

When to use StringBuilder in Java

等等

的StringBuilder比StringBuffer的

Why use StringBuilder? StringBuffer can work with multiple thread as well as one thread?

有所不同,可比

因爲使用了優先級隊列。它需要這個接口。

How do I use a PriorityQueue?

而且,讀Huffman編碼維基百科頁面(你可以做,以瞭解該算法),它被提到,編碼的最佳結構是有序的。我個人並不知道算法,但我建議不要將代碼從您不明白的互聯網上覆制下來。