您的位置:首页 > 其它

6、哈弗曼编码算法

2016-07-10 17:12 169 查看
(1)小根堆(优先队列)

package com.tree;

public class MinHeap {

private static final int DEFAULT_CAPACITY = 100;

private int currentSize;

private Comparable[] array;

public MinHeap() {
this(DEFAULT_CAPACITY);
}

public MinHeap(int capacity) {
currentSize = 0;
array = new Comparable[capacity + 1];
}

public MinHeap(Comparable[] items){
currentSize = items.length +1;
int i = 1;
for (Comparable item : items) {
array[i++] = item;
}
buildHeap();
}

public void insert(Comparable x) {
if (isFull()) {
throw new RuntimeException("堆已满!");
}
int hole = ++currentSize;
for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2) {
array[hole] = array[hole / 2];
}
array[hole] = x;
}

public void buildHeap() {
for (int i = currentSize / 2; i > 0; i--) {
percolateDown(i);
}
}

public Comparable findMin() {
if (isEmpty()) {
return null;
}
return array[1];
}

public Comparable deleteMin() {
if (isEmpty()) {
return null;
}
Comparable minItem = findMin();
array[1] = array[currentSize--];
percolateDown(1);
return minItem;
}

/**
* 下滤
*
* @param hole 要下滤节点的索引
*
*/
private void percolateDown(int hole) {
int child;
Comparable temp = array[hole];
for (; hole * 2 <= currentSize; hole = child) {
child = hole * 2;
if ( child != currentSize &&array[child + 1].compareTo(array[child]) < 0) {
child++;
}
if (array[child].compareTo(array[hole]) < 0) {
array[hole] = array[child];
} else {
break;
}
}
array[hole] = temp;
}

public boolean isEmpty() {
return currentSize == 0;
}

public boolean isFull() {
return currentSize == array.length - 1;
}

public void makeEmpty() {
currentSize = 0;
}

}
(2)二叉树

package com.tree;

public class BinaryTree implements Comparable<BinaryTree>{

BinaryTree leftChild, rightChild, parent;
Data data;

@Override
public int compareTo(BinaryTree o) {
return data.weight - o.data.weight;
}

}

class Data  {

char c;
int weight;
String code="";

}
(3)测试类

package com.tree;

public class Test {

public static void main(String[] args) {

//初始化
char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
int[] weights = { 45, 13, 12, 16, 9, 5 };

MinHeap minHeap = new MinHeap(6);
BinaryTree[] trees = new BinaryTree[chars.length];
for (int i = 0; i < weights.length; i++) {
Data data = new Data();
BinaryTree tree = new BinaryTree();

data.c = chars[i];
data.weight = weights[i];
tree.data = data;

trees[i] = tree;
minHeap.insert(tree);
}

//建立关系
for (int i = 1; i < trees.length; i++) {
BinaryTree tree1 = (BinaryTree) minHeap.deleteMin();
BinaryTree tree2 = (BinaryTree) minHeap.deleteMin();

BinaryTree tree = new BinaryTree();

tree.leftChild = tree1;
tree.rightChild = tree2;

tree.data = new Data();
tree.data.weight = tree1.data.weight + tree2.data.weight;

tree1.parent = tree;
tree2.parent = tree;

minHeap.insert(tree);
}
// 编码
for (int i = 0; i < trees.length; i++) {
BinaryTree leafTree = trees[i];

Data data = leafTree.data;

BinaryTree parent = leafTree.parent;
BinaryTree current = leafTree;
while (parent != null) {
if (parent.leftChild == current) {
data.code = "0" + data.code;
} else if (parent.rightChild == current) {
data.code = "1" + data.code;
}
current = parent;
parent = current.parent;
}
}

for (int i = 0; i < trees.length; i++) {
System.out.println("字符:"+trees[i].data.c + " 权重:"+trees[i].data.weight+" 编码:" + trees[i].data.code);
}

}

}


(4)运行结果



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  哈弗曼编码