huffman(greedy)

Posted Wujunde

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了huffman(greedy)相关的知识,希望对你有一定的参考价值。

present a file by binary character code,let the less characters can be presented simplier.

 1 package greedy;
 2 
 3 import java.util.Iterator;
 4 import java.util.LinkedList;
 5 
 6 public class huffman {
 7     private static class Node{
 8         private Node left ;
 9         private Node right ;
10         private int freq;
11         public Node(Node left,Node right,int freq){
12             this.left = left;this.right = right; this.freq = freq;
13         }
14         }
15         
16     
17     public Node Huffman(LinkedList<Node> list){
18         int n = list.size();
19         for(int i = 1;i <= n-1;i++){
20             Node x = list.removeFirst();
21             Node y = list.removeFirst();
22             Node node = new Node(x,y,x.freq + y.freq);
23             for (int j = 0;j < list.size() - 1;j++){
24                 if(node.freq >= list.get(j).freq && node.freq <= list.get(j+1).freq){
25                     list.add(j+1,node);
26                 }
27                 else if( j+1 == list.size() - 1){
28                     list.addLast(node);
29                 }
30             
31             }
32             
33             
34         }
35         return list.getFirst();
36         
37     }
38     
39 }

 

技术分享

技术分享

 

以上是关于huffman(greedy)的主要内容,如果未能解决你的问题,请参考以下文章

DS二叉树——Huffman编码与解码(不含代码框架)

搬家] 关于 Huffman 编码

Huffman树 建树方法代码实现

优化算法Greedy Randomized Adaptive Search算法 超详细解析,附代码实现TSP问题求解

题目1453:Greedy Tino(dp题目)

C++ class实现Huffman树(完整代码)