1.哈夫曼编码是一种可变字长编码
如果一组编码中任一编码都不是其他任何一个编码的前缀,我们称这组编码为前缀编码。哈夫曼树可用于构造最短的不等长编码方案。
2.算法流程
(1)根据给定的n个权值{w1,w2,w3…,wn}构成n棵二叉树的集合F={T1,T2,…Tn},其中每棵二叉树Ti中只有一个带权为wi的根结点,其左右子树均空。
(2)在F中选取两棵根结点的权值最小的树作为左右子树构造一棵新的二叉树,且置新的二叉树的根结点的权值为其左、右子树上根结点的权值之和。
(3)在F中删除这两棵树,同时将新得到的二叉树加人F中。
(4)重复(2)和(3),直到F只含棵树为止。 这棵树便是赫夫曼树。
例如:已知某系统在通信联络中只可能出现8种字符,其概率分别为0.05,0.29,0.07,0.08,0.14,0.23,0.03,0.11,试设计赫夫曼编码。
权w=(5,29,7,8,14,23,3,11),n=8,则m=15,按上述算法可构造一棵赫夫曼树如图所示。
则此时可以根据所得的哈夫曼树求得哈夫曼编码。
哈夫曼树
public class huffNode {
int weight;
int parent,lchild,rchild;
public huffNode() {
weight = 0;parent = 0;
lchild = 0; rchild = 0;
}
}
public class huffmanTree {
private huffNode Tree[] = null;
private String code[] = null;
private int[] Select(huffNode tree[],int end){ //选择两个权值最小的
int ans[] = new int[2];
int min = Integer.MAX_VALUE;
for (int i = 0; i < end; i++) {
if (tree[i].parent!=0) continue;
if (min>tree[i].weight){
min = tree[i].weight;
ans[0] = i;
}
}
min = Integer.MAX_VALUE;
for (int i = 0; i < end; i++) {
if (tree[i].parent!=0||i==ans[0]) continue;
if (min>tree[i].weight){
min = tree[i].weight;
ans[1] = i;
}
}
return ans;
}
public void huffmanCoding(int []w,int n){ //哈夫曼编码
int m = 2 * n - 1 ;
int s1,s2;
Tree = new huffNode[m];
for (int i=0;i<n;i++){ //初始化哈夫曼树
Tree[i] = new huffNode();
Tree[i].weight = w[i];
}
for (int i = n;i<m;i++){
Tree[i] = new huffNode();
}
for (int i = n ;i<m;i++){ //建立哈夫曼树
int ans[] = Select(Tree,i);
s1 = ans[0];s2 = ans[1];
Tree[s1].parent=i;Tree[s2].parent=i;
Tree[i].lchild = s1; Tree[i].rchild = s2;
Tree[i].weight = Tree[s1].weight + Tree[s2].weight;
}
code = new String [n];
for (int i=0;i<n;i++){ //求出哈夫曼编码
StringBuilder sb = new StringBuilder();
for (int c = i,f = Tree[i].parent;f!=0;c=f,f=Tree[f].parent){
if (Tree[f].lchild == c) sb.append(0);
else sb.append(1);
}
code[i] = sb.reverse().toString();
}
}
public void print(int w[]){ //打印哈夫曼编码
for (int i=0;i<w.length;i++){
System.out.println(w[i] +":"+code[i]);
}
}
public static void main(String args[]){
huffmanTree t = new huffmanTree();
int w[] = {7,4,56,165,1,5,416};
t.huffmanCoding(w,w.length);
t.print(w);
}
}