https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/
character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
* HuffmanNode.java
package org.geeksforgeeks.www.huffman;
public class HuffmanNode {
protected int data;
protected char c;
protected HuffmanNode left;
protected HuffmanNode right;
public HuffmanNode(char c, int freq) {
this.c = c;
this.data = freq;
this.left = null;
this.right = null;
}
public int getData() {
return this.data;
}
public char getChar() {
return this.c;
}
public void setLeft(HuffmanNode left) {
this.left = left;
}
public void setRight(HuffmanNode right) {
this.right = right;
}
public boolean isLeaf() {
return this.left == null && this.right == null;
}
public static String decodeFromBitString(HuffmanNode root, String bs) {
String text = "";
HuffmanNode node = root;
for (int i = 0; i < bs.length(); i++) {
if (node.isLeaf()) {
text += node.c;
node = root; // rewind
}
char c = bs.charAt(i);
if (c == '0') {
node = node.left;
} else if (c == '1') {
node = node.right;
} else {
throw new IllegalArgumentException("must be 0 or 1");
}
}
if (node.isLeaf()) {
text += node.c;
}
return text;
}
}
* Huffman.java
package org.geeksforgeeks.www.huffman;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
/**
* https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/
* @author Mch
*/
public class Huffman {
protected HuffmanNode root = null;
private int length = 0;
public Huffman(char[] charArray, int[] charFreq) throws IllegalArgumentException {
int n = charArray.length;
if (n != charFreq.length) {
throw new IllegalArgumentException("charArray.length != charFreq.length");
}
PriorityQueue<HuffmanNode> q = new PriorityQueue<HuffmanNode>(n,
new Comparator<HuffmanNode>() {
@Override
public int compare(HuffmanNode o1, HuffmanNode o2) {
return o1.data - o2.data;
}
});
for (int i = 0; i < n; i++) {
q.add( new HuffmanNode(charArray[i], charFreq[i]) );
}
while (q.size() > 1) {
HuffmanNode x = q.poll(), y = q.poll();
HuffmanNode f = new HuffmanNode('-', x.getData() + y.getData());
f.setLeft(x);
f.setRight(y);
this.root = f;
q.add(f);
}
this.length = n;
}
public int size() {return this.length;}
public HashMap<String, String> getEncodeMap() {
HashMap<String, String> pairs = new HashMap<String, String>(this.size());
addPair(pairs, this.root, "");
return pairs;
}
private static void addPair(HashMap<String, String> pairs, HuffmanNode root, String s) {
if (root.left == null && root.right == null && Character.isLetter(root.c)) {
pairs.put(root.c + "", s);
return;
}
addPair(pairs, root.left, s + "0");
addPair(pairs, root.right, s + "1");
}
public String decodeFromBitString(String bs) {
return HuffmanNode.decodeFromBitString(this.root, bs);
}
public static void main(String []args) {
char[] charArray = {'a', 'b', 'c', 'd', 'e', 'f'};
int[] charFreq = {5, 9, 12, 13, 16, 45};
try {
Huffman tree = new Huffman(charArray, charFreq);
HashMap<String, String> map = tree.getEncodeMap();
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue() );
}
String text = "cabaddfabdef", bs = "";
for (int i = 0; i < text.length(); i++) {
bs += map.get("" + text.charAt(i));
}
System.out.println(bs);
System.out.println(tree.decodeFromBitString(bs));
} catch (IllegalArgumentException e) {
e.getMessage();
}
}
}
* test:
a: 1100
b: 1101
c: 100
d: 101
e: 111
f: 0
1001100110111001011010110011011011110
cabaddfabdef
参照: