Advertisement
Guest User

Huffman coding

a guest
Jan 24th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.32 KB | None | 0 0
  1. // Лапшева консольный
  2.  
  3. import java.util.*;
  4.  
  5. public class Solution {
  6.   static ArrayList<Character> code = new ArrayList<>();
  7.   static TreeMap<Character, ArrayList<Character>> table = new TreeMap<>();
  8.  
  9.   public static class Token implements Comparable<Token> {
  10.     String information;
  11.     int weight;
  12.     boolean isAtom;
  13.  
  14.     Token() {
  15.       information = new String();
  16.       weight = -1;
  17.       isAtom = true;
  18.     }
  19.  
  20.     Token(Character what, int weight) {
  21.       information = new String(what.toString());
  22.       this.weight = weight;
  23.       isAtom = true;
  24.     }
  25.  
  26.     Token(String what, int weight) {
  27.       information = what;
  28.       this.weight = weight;
  29.       isAtom = false;
  30.     }
  31.  
  32.     Token add(Token other) {
  33.       Token ans = new Token();
  34.  
  35.       ans.information = new String(information + other.information);
  36.       ans.weight = weight + other.weight;
  37.       ans.isAtom = false;
  38.  
  39.       return ans;
  40.     }
  41.  
  42.     @Override
  43.     public int compareTo(Token rhs) {
  44.       if (weight != rhs.weight) {
  45.         return weight - rhs.weight;
  46.       }
  47.  
  48.       if (isAtom != rhs.isAtom) {
  49.         if (isAtom)
  50.           return -1;
  51.  
  52.         return 1;
  53.       }
  54.  
  55.       return information.compareTo(rhs.information);
  56.     }
  57.   }
  58.  
  59.   public static class Tree implements Comparable<Tree> {
  60.     Token token;
  61.  
  62.     Tree left;
  63.     Tree right;
  64.  
  65.     Tree() {
  66.       token = new Token();
  67.       left = null;
  68.       right = null;
  69.     }
  70.  
  71.     Tree(Token token) {
  72.       this.token = token;
  73.       left = null;
  74.       right = null;
  75.     }
  76.  
  77.     Tree(Token token, Tree left, Tree right) {
  78.       this.token = token;
  79.       this.left = left;
  80.       this.right = right;
  81.     }
  82.  
  83.     @Override
  84.     public int compareTo(Tree rhs) {
  85.       return token.compareTo(rhs.token);
  86.     }
  87.   }
  88.  
  89.   public static void buildTable(Tree root) {
  90.     if (root.left != null) {
  91.       code.add('0');
  92.       buildTable(root.left);
  93.     }
  94.  
  95.     if (root.right != null) {
  96.       code.add('1');
  97.       buildTable(root.right);
  98.     }
  99.  
  100.     if (root.token.isAtom) {
  101.       var list = new ArrayList<Character>(code);
  102.  
  103.       table.put(root.token.information.charAt(0), list);
  104.     }
  105.  
  106.     if (!code.isEmpty())
  107.       code.remove(code.size() - 1);
  108.   }
  109.  
  110.   public static void main(String[] args) throws Exception {
  111.     var s = new Scanner(System.in);
  112.  
  113.     String input = s.nextLine();
  114.  
  115.     var chars = new TreeMap<Character, Integer>();
  116.     for (int i = 0; i < input.length(); ++i) {
  117.       var cur = input.charAt(i);
  118.  
  119.       if (chars.containsKey(cur)) {
  120.         chars.put(cur, chars.get(cur) + 1);
  121.       } else {
  122.         chars.put(cur, 1);
  123.       }
  124.     }
  125.  
  126.     var tree = new PriorityQueue<Tree>();
  127.  
  128.     for (var i : chars.entrySet()) {
  129.       var newToken = new Token(i.getKey(), i.getValue());
  130.       var newTree = new Tree(newToken);
  131.  
  132.       tree.add(newTree);
  133.     }
  134.    
  135.     System.out.println("Символы (" + chars.size() + " уникальных):");
  136.    
  137.     for (var i : chars.entrySet()) {
  138.       System.out.println(i.getKey() + "(" + i.getValue() + ")");
  139.     }
  140.    
  141.     System.out.println();
  142.    
  143.     System.out.println("Листья (вес, алфавит, атомарность): ");
  144.    
  145.     var tmp = new PriorityQueue<Tree>(tree);
  146.    
  147.     while (!tmp.isEmpty()) {
  148.       var cur = tmp.poll();
  149.  
  150.       System.out.println(cur.token.information + "(" + cur.token.weight + ")");
  151.     }
  152.    
  153.     System.out.println();
  154.    
  155.     int step = 1;
  156.    
  157.     while (tree.size() > 1) {
  158.       var left = tree.poll();
  159.       var right = tree.poll();
  160.  
  161.       var parentToken = left.token.add(right.token);
  162.       var parentTree = new Tree(parentToken, left, right);
  163.  
  164.       tree.add(parentTree);
  165.      
  166.       System.out.print("Шаг " + step + ": ");
  167.       System.out.print(left.token.information + "(" + left.token.weight + ") + ");
  168.       System.out.print(right.token.information + "(" + right.token.weight + ") = ");
  169.       System.out.println(parentTree.token.information + "(" +
  170.           parentTree.token.weight + ")");
  171.      
  172.       tmp = new PriorityQueue<Tree>(tree);
  173.      
  174.       while (!tmp.isEmpty()) {
  175.         var cur = tmp.poll();
  176.        
  177.         System.out.println(cur.token.information + "(" + cur.token.weight + ")");
  178.       }
  179.      
  180.       System.out.println();
  181.      
  182.       ++step;
  183.     }
  184.    
  185.     System.out.println();
  186.  
  187.     Solution.buildTable(tree.peek());
  188.  
  189.     System.out.print("Длина строки: " + input.length() + "\n");
  190.  
  191.     int power = (int) Math.ceil(Math.log(chars.size()) / Math.log(2));
  192.     int badSize = power * input.length();
  193.    
  194.     System.out.print("i: " + power + "\n");
  195.     System.out.print("Объем по Хартли: " + badSize + "\n");
  196.    
  197.     System.out.print("Объем по Хаффману: ");
  198.  
  199.     Integer goodSize = 0;
  200.    
  201.     for (var i : table.entrySet()) {
  202.       goodSize += chars.get(i.getKey()) * i.getValue().size();
  203.     }
  204.    
  205.     System.out.print(goodSize.toString() + "\n");
  206.    
  207.     double answer = 1.0 * badSize / goodSize;
  208.    
  209.     System.out.print("Ответ (Vхартли / Vхаффман): " + answer + "\n\n");
  210.  
  211.     for (var i : table.entrySet()) {
  212.       System.out.print(i.getKey().toString() + ": ");
  213.  
  214.       for (var j : i.getValue()) {
  215.         System.out.print(j.toString());
  216.       }
  217.  
  218.       System.out.print("\n");
  219.     }
  220.  
  221.     s.close();
  222.   }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement