Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. package is211.huffman;
  2.  
  3.  
  4. import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.io.Reader;
  12. import java.io.StringReader;
  13. import java.io.StringWriter;
  14. import java.io.Writer;
  15. import java.util.HashMap;
  16. import java.util.Iterator;
  17. import java.util.PriorityQueue;
  18.  
  19.  
  20.  
  21. /**
  22. * A simple Huffman encoder/decoder.
  23. *
  24. * @author Even Åby Larsen
  25. * @version 1.0
  26. */
  27. public class HuffmanCoDec
  28. {
  29. // declare fields for the necessary data
  30. // hint you need a Huffman tree, and a structure
  31. // where you can quickly retrieve the huffman code
  32. // for a character (when you are encoding the text)
  33. private HashMap<Integer, HuffmanNode> encodeTable;
  34. // private PriorityQueue
  35. private HuffmanNode root;
  36. int c = 0;
  37. private static final int ALPHABET_SIZE = 256;
  38.  
  39. /**
  40. * Create Huffman encoder/decoder with optimal encoding for the data
  41. * provided. This constructor will read the text form a java.io.Reader
  42. *
  43. * @param src the text to build the code table for.
  44. */
  45. public HuffmanCoDec(Reader src) throws IOException {
  46. createCodeTable(src);
  47. }
  48.  
  49. /**
  50. * This constructor will read the text from a String
  51. *
  52. * @param src
  53. * @throws IOException
  54. */
  55. public HuffmanCoDec(String src) throws IOException {
  56. this(new StringReader(src));
  57. }
  58.  
  59. /**
  60. * And this one will read from a file
  61. *
  62. * @param f
  63. * @throws IOException
  64. */
  65. public HuffmanCoDec(File f) throws IOException {
  66. this(new BufferedReader(new FileReader(f)));
  67. }
  68.  
  69. /**
  70. * Build the huffman tree and encoding table for the text that can be read
  71. * from src
  72. *
  73. * @param src the text to create the code table for
  74. */
  75. public final void createCodeTable(Reader src) throws IOException {
  76. encodeTable = new HashMap<>();
  77.  
  78. //int[] counter = new int[256];
  79. int c = src.read();
  80. while (c != -1) {
  81. countChar(c);
  82. c = src.read();
  83.  
  84. }
  85.  
  86. // Phase 1 - count character frequencies // Må kalle på countChar metoden?
  87. // phase 2 - build the huffman tree
  88. // assign codes to each character //traverse?
  89.  
  90. //print encode table, to enable checking of output
  91. for (int ch : encodeTable.keySet())
  92. System.out.println("encodeTable = " + encodeTable.get(ch));
  93. }
  94.  
  95.  
  96.  
  97. public int traverse(HuffmanNode n, int[] code, int level) {
  98.  
  99. if (n.isLeaf()) {
  100. return n.c;
  101.  
  102. }
  103.  
  104. else
  105. code[level] = 1;
  106. traverse(n.one, code, level+1);
  107.  
  108. code[level] = 0;
  109. traverse(n.zero, code, level+1);
  110.  
  111. //System.out.println(n);
  112. return n.c;
  113. }
  114.  
  115. /**
  116. * Traverses the tree to find the huffman code for each character. When you
  117. * go left, add a zero to the code, and a one when moving right
  118. *
  119. * @param c
  120. */
  121.  
  122.  
  123.  
  124. public void countChar(int c) {
  125. HuffmanNode value = encodeTable.get(c);
  126. if (value == null) {
  127. HuffmanNode node = new HuffmanNode(c);
  128. node.count++;
  129. encodeTable.put(c, node);
  130. } else {
  131. value.count++;
  132. }
  133.  
  134.  
  135. for (int hmkey : encodeTable.keySet()) {
  136.  
  137. // Integer key = hmkey;
  138. String character = encodeTable.get(hmkey).toString();
  139.  
  140. // System.out.println("HashMap encodeTable:");
  141. // System.out.println( "Character =" + character); //"Key = " + key + " " +
  142.  
  143. }
  144.  
  145. }
  146.  
  147.  
  148.  
  149. /**
  150. * Encode a string
  151. *
  152. * @param input the text to encode
  153. * @param output the destination for the encoded text
  154. */
  155. public void encode(Reader input, BitSink output) throws IOException {
  156. // read characters from input, find the character codes in the
  157. // table and write the code to output
  158.  
  159. /*
  160. int c = input.read();
  161. while (c != -1) {
  162. output.writeBit(c);
  163. c = input.read();*/
  164.  
  165. HuffmanNode root = buildTree();
  166. //traverse(root);
  167. // output.writeBit(root);
  168. }
  169.  
  170.  
  171. /**
  172. * Decode a huffman encoded text. See assignment text for details
  173. *
  174. * @param input the encoded text
  175. * @param output the destination for the decoded text.
  176. */
  177. public void decode(BitSource input, Writer output) throws IOException {
  178. // repeat the following until all bits are read
  179. // read bits from input, use the bit value to select a child
  180. // when you have reached a leaf node, write the character from
  181. // the leaf node to output
  182. }
  183.  
  184. private HuffmanNode buildTree() {
  185.  
  186. PriorityQueue<HuffmanNode> pq = new PriorityQueue<>();
  187.  
  188. for(int ch : encodeTable.keySet()) {
  189.  
  190. HuffmanNode newNode = new HuffmanNode(c);
  191.  
  192. newNode.zero = null;
  193. newNode.one = null;
  194.  
  195. pq.add(encodeTable.get(ch));
  196. }
  197.  
  198.  
  199. while(pq.size() > 1) {
  200. HuffmanNode zero = pq.remove();
  201. HuffmanNode one = pq.remove();
  202. HuffmanNode parent = new HuffmanNode('\0');
  203. parent.count = zero.count + one.count;
  204. pq.add(parent);
  205.  
  206.  
  207. }
  208.  
  209. while(!pq.isEmpty()) {
  210. // System.out.println("TEST2 :: " + pq);
  211. System.out.println("PQ = " + pq.remove());
  212. }
  213. return pq.poll();
  214. }
  215.  
  216. /**
  217. * The node class. It must implement comparable, so the nodes can be
  218. * compared by the priority queue.
  219. *
  220. */
  221. private class HuffmanNode implements Comparable<HuffmanNode>
  222. {
  223. int c; // the character that this node represents
  224. int count; // the number of occurences of the character in the txt
  225. int[] code; // the huffman code for the character
  226. HuffmanNode zero, one; // the children of the node
  227.  
  228. /**
  229. * Constructor for leaf nodes
  230. *
  231. * @param c
  232. */
  233. public HuffmanNode(int c) {
  234. this.c = c;
  235. count = 0;
  236. zero = one = null;
  237. }
  238.  
  239. boolean isLeaf() {
  240. return this.zero == null && this.one == null;
  241. }
  242.  
  243.  
  244. /**
  245. * Constructor for creating a parent for two nodes
  246. *
  247. * @param zero
  248. * @param one
  249. */
  250. public HuffmanNode(HuffmanNode zero, HuffmanNode one) {
  251. this.zero = zero;
  252. this.one = one;
  253. this.c = 0;
  254. count = zero.count + one.count;
  255. }
  256.  
  257. /**
  258. * ToString is useful for debugging...
  259. *
  260. * @return
  261. */
  262. public String toString() {
  263. StringWriter out1 = new StringWriter();
  264. PrintWriter out2 = new PrintWriter(out1);
  265.  
  266. out2.format("%3c %5d ", c, count);
  267. //for (int i : code) MÅ FINNE CODE FOR Å BRUKE DENNE.
  268. // out2.print(i);
  269. out2.flush();
  270. out1.flush();
  271. return out1.toString();
  272. }
  273.  
  274. /**
  275. * compareTo is defined in the Comparable interface. A priority queue
  276. * will call this method to determine which of two nodes go first.
  277. *
  278. * @param that the node to compare this to.
  279. * @return this.count - that.count
  280. */
  281.  
  282. public int compareTo(HuffmanNode that) {
  283.  
  284. return - (that.count - this.count);
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement