Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package Révision;
  2.  
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.Map;
  8. import java.util.PriorityQueue;
  9. import java.util.Queue;
  10. import java.util.Stack;
  11. import java.util.TreeSet;
  12. import java.util.Vector;
  13.  
  14. public class Huffman {
  15. public static final int N = 256; // nb car
  16. static class Node implements Comparable<Node> {
  17. public char ch;
  18. public int freq;
  19. private final Node left, right;
  20.  
  21. public Node(char ch, int freq, Node left, Node right) {
  22. this.ch = ch;
  23. this.freq = freq;
  24. this.left = left;
  25. this.right = right;
  26. }
  27.  
  28. public boolean isLeaf() {
  29. return left == null && right == null;
  30. }
  31.  
  32. public int compareTo(Node that) {
  33. return this.freq - that.freq;
  34. }
  35.  
  36. public String toString() {
  37. return this.ch + " " + this.freq;
  38. }
  39. }
  40.  
  41. private static Node buildTrie(int[] freq) {
  42. PriorityQueue<Node> pq = new PriorityQueue<Node>();
  43.  
  44. for (char c = 0; c < N; c++) {
  45. if (freq[c] > 0) {
  46. pq.offer(new Node(c, freq[c], null, null));
  47. }
  48. }
  49. while (pq.size() > 1) {
  50. Node x = pq.poll();
  51. Node y = pq.poll();
  52. System.out.println((x.ch == '\0' ? 'x' : x.ch) + " " + (y.ch == '\0' ? 'x' : y.ch) + " = " + (x.freq + y.freq));
  53. Node parent = new Node('\0', x.freq + y.freq, x, y);
  54. pq.offer(parent);
  55. }
  56.  
  57. return pq.poll();
  58. }
  59.  
  60. public static void buildCode(String[] st, Node x, String s) {
  61. if (x.isLeaf()) {
  62. st[x.ch] = s;
  63. return;
  64. }
  65.  
  66. buildCode(st, x.left, s + '0');
  67. buildCode(st, x.right, s + '1');
  68. }
  69.  
  70. public static void writeTrie(Node x) {
  71. if (x.isLeaf()) {
  72. BinaryStdOut.write(true);
  73. BinaryStdOut.write(x.ch, 8);
  74. return;
  75. }
  76. BinaryStdOut.write(false);
  77. writeTrie(x.left);
  78. writeTrie(x.right);
  79. }
  80.  
  81. public static Node readTrie() {
  82. if (BinaryStdIn.readBoolean())
  83. return new Node(BinaryStdIn.readChar(), 0, null, null);
  84.  
  85. return new Node('\0', 0, readTrie(), readTrie());
  86. }
  87.  
  88. public static void expand() {
  89. Node root = readTrie();
  90. int N = BinaryStdIn.readInt();
  91. for (int i = 0; i < N; i++) {
  92. Node x = root;
  93. while (!x.isLeaf())
  94. if (BinaryStdIn.readBoolean())
  95. x = x.right;
  96. else x = x.left;
  97. BinaryStdOut.write(x.ch, 8);
  98. }
  99. }
  100.  
  101. public static void main(String[] args) {
  102. char[] input = "abteehhwwwiiiissssss ".toCharArray();
  103.  
  104. // étape 0 : remplir un tableau de fréquence des caractères
  105. int[] charFreq = new int[N];
  106. for (char c : input) {
  107. charFreq[c]++;
  108. }
  109.  
  110. // étape 1 : construire l'arbre de trie
  111. Node root = buildTrie(charFreq);
  112.  
  113. // étape 2 : construction des codes
  114. String[] st = new String[N];
  115. buildCode(st, root, "");
  116. for (int i = 0; i < N; i++) {
  117. if (charFreq[i] > 0)
  118. System.out.println((char)i + " -> " + st[i]);
  119. }
  120.  
  121.  
  122. // étape 3 : encodage de l'input avec les codes
  123. // écriture du trie
  124. writeTrie(root);
  125. BinaryStdOut.write(input.length);
  126. // encodage du message
  127. for (int i = 0; i < input.length; i++) {
  128. String code = st[input[i]];
  129. for (int j = 0; j < code.length(); j++)
  130. if (code.charAt(j) == '1')
  131. BinaryStdOut.write(true);
  132. else BinaryStdOut.write(false);
  133. }
  134. // Décodage de la chaine encodée
  135. //expand();
  136. BinaryStdOut.close();
  137.  
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement