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 }

原文地址:https://www.cnblogs.com/wujunde/p/7075482.html