Java实现哈夫曼编码

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 import java.util.Map;
  4 import java.util.Scanner;
  5 
  6 
  7 class Node{
  8     char letter;
  9     int power;
 10     Node left_son;
 11     Node right_son;
 12     public Node() { super();}
 13     public Node(int power) {
 14         this.power = power;
 15     }
 16 }
 17 public class CurrDesign {
 18 
 19     static Map<Character, String> map = new HashMap<Character, String>();
 20     public static void main(String[] args) {
 21         List <Node>list = codingTable();//输入
 22         Node tree_root = creatHFMtree(list);
 23         creatTable(tree_root,"");
 24         display();
 25 
 26         String text = inputText();
 27         encode(text);
 28 
 29         String code = inputCode();
 30         decode(code,tree_root);
 31     }
 32 
 33     public static String[] encode(String text) {
 34         String []s = new String[100];
 35         char []key = text.toCharArray();
 36         System.out.println("编码结果为:");
 37         for(int i=0;i<key.length;i++) {
 38             s[i]=map.get(key[i]);
 39             System.out.print(s[i]+" ");
 40         }
 41         System.out.println();
 42         return s;
 43     }
 44 
 45     public static void decode(String s,Node tree_root) {
 46         System.out.println("译码结果为:");
 47         char []num = s.toCharArray();
 48         Node node = tree_root;
 49         for(int i=0;i<num.length;i++) {
 50             node = check(node,num[i]);
 51             if(node==null) {
 52                 node = check(tree_root,num[i]);
 53             }
 54         }
 55         System.out.println(node.letter);
 56     }
 57 
 58     public static Node check(Node node,char c) {
 59         if(node.left_son!=null && c=='0')
 60             return node.left_son;
 61         else if(node.right_son!=null && c=='1'){
 62             return node.right_son;
 63         }
 64         System.out.print(node.letter);
 65         return null;
 66     }
 67 
 68     public static Object getKey(Object value){
 69         for(Object key: map.keySet()){
 70             if(map.get(key).equals(value)){
 71                 return key;
 72             }
 73         }
 74         return null;
 75     }
 76 
 77     public static String inputText() {
 78         Scanner in = new Scanner(System.in);
 79         System.out.println("请输入要编码的文本,只含大写字母:");
 80         String text = in.next();
 81         return text;
 82     }
 83 
 84     public static String inputCode() {
 85         Scanner in = new Scanner(System.in);
 86         System.out.println("请输入编码:");
 87         String code = in.next();
 88         in.close();
 89         return code;
 90     }
 91 
 92     public static  List<Node> codingTable(){
 93         List <Node>list = new ArrayList<Node>();
 94         int []num = {186,64,13,22,32,103,21,15,47,57,1,2,32,20,57,63,15,1,48,51,80,23,8,18,1,16};
 95         Node node = null;
 96         for(int n=0;n<26;n++) {
 97             node = new Node();
 98             node.letter = (char)('A'+n);
 99             node.power = num[n];
100             list.add(node);
101         }
102         return list;
103     }
104 
105     public static void display() {
106         System.out.println("The huffman coding table are:");
107         System.out.println(map.toString());
108     }
109 
110     public static Node creatHFMtree(List<Node> list){
111         Node p = null;
112         int n = 0;
113         while(!list.isEmpty()) {
114             if(n!=0) {
115                 list.add(p);
116             }
117             Node min1 = new Node(1000);
118             Node min2 = new Node(999);
119             for(Node node:list) {
120                 if(node.power < min1.power) {
121                     if(node.power < min2.power) {
122                         min1 = min2;
123                         min2 = node;
124                     }else {
125                         min1 = node;
126                     }
127                 }
128             }
129             p = new Node(min1.power+min2.power);
130             p.left_son  = min2;
131             p.right_son = min1;
132             list.remove(min1);
133             list.remove(min2);
134             n++;
135         }
136         return p;
137     }
138 
139     public static void creatTable(Node node,String coding){
140         if(node.left_son!=null) {
141             creatTable(node.left_son,coding+"0");
142         }
143         if(node.right_son!=null) {
144             creatTable(node.right_son,coding+"1");
145         }
146         if(node.left_son==null && node.right_son==null) {
147             map.put(node.letter, coding);
148         }
149     }
150 }

 

posted @ 2020-01-05 18:07  初寒~修  阅读(1559)  评论(0编辑  收藏  举报