6、哈弗曼编码算法
2016-07-10 17:12
169 查看
(1)小根堆(优先队列)
(4)运行结果
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)运行结果
相关文章推荐
- 哈弗曼编码
- Q11.3.4 哈弗曼编码
- POJ-1521/ZOJ-1117/Entropy
- 第六周作业1 -- 利用哈夫曼编码英文字母表
- 贪心算法求解哈弗曼编码
- Hdu 2527 Safe Or Unsafe-哈夫曼编码+优先队列
- 哈弗曼编码
- 哈弗曼编码及译码
- 数据结构之哈弗曼编码的(Huffman Coding)加密解密压缩
- 最小堆和建树分别求哈弗曼编码
- [论文解读] Deep Compression
- 几个技术问题,已知前序遍历中序遍历求后序遍历,哈弗曼编码,建立初始堆
- 一块金条切成两半,是需要花费和长度数值一样的铜板的。比如长度为20的 金条,不管切成长度多大的两半,都要花费20个铜板。一群人想整分整块金 条,怎么分最省铜板。
- poj1521
- 数据结构之哈弗曼树与哈弗曼编码
- 《算法导论》学习分享——16. 贪心算法
- PAT Huffman Codes 哈弗曼树,C语言实现
- 算法导论(2)
- microstation level3 13 extrusion with span
- 【PHP面向对象(OOP)编程入门教程】19.抽象方法和抽象类(abstract)