Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package is211.huffman;
- import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.Reader;
- import java.io.StringReader;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.PriorityQueue;
- /**
- * A simple Huffman encoder/decoder.
- *
- * @author Even Åby Larsen
- * @version 1.0
- */
- public class HuffmanCoDec
- {
- // declare fields for the necessary data
- // hint you need a Huffman tree, and a structure
- // where you can quickly retrieve the huffman code
- // for a character (when you are encoding the text)
- private HashMap<Integer, HuffmanNode> encodeTable;
- // private PriorityQueue
- private HuffmanNode root;
- int c = 0;
- private static final int ALPHABET_SIZE = 256;
- /**
- * Create Huffman encoder/decoder with optimal encoding for the data
- * provided. This constructor will read the text form a java.io.Reader
- *
- * @param src the text to build the code table for.
- */
- public HuffmanCoDec(Reader src) throws IOException {
- createCodeTable(src);
- }
- /**
- * This constructor will read the text from a String
- *
- * @param src
- * @throws IOException
- */
- public HuffmanCoDec(String src) throws IOException {
- this(new StringReader(src));
- }
- /**
- * And this one will read from a file
- *
- * @param f
- * @throws IOException
- */
- public HuffmanCoDec(File f) throws IOException {
- this(new BufferedReader(new FileReader(f)));
- }
- /**
- * Build the huffman tree and encoding table for the text that can be read
- * from src
- *
- * @param src the text to create the code table for
- */
- public final void createCodeTable(Reader src) throws IOException {
- encodeTable = new HashMap<>();
- //int[] counter = new int[256];
- int c = src.read();
- while (c != -1) {
- countChar(c);
- c = src.read();
- }
- // Phase 1 - count character frequencies // Må kalle på countChar metoden?
- // phase 2 - build the huffman tree
- // assign codes to each character //traverse?
- //print encode table, to enable checking of output
- for (int ch : encodeTable.keySet())
- System.out.println("encodeTable = " + encodeTable.get(ch));
- }
- public int traverse(HuffmanNode n, int[] code, int level) {
- if (n.isLeaf()) {
- return n.c;
- }
- else
- code[level] = 1;
- traverse(n.one, code, level+1);
- code[level] = 0;
- traverse(n.zero, code, level+1);
- //System.out.println(n);
- return n.c;
- }
- /**
- * Traverses the tree to find the huffman code for each character. When you
- * go left, add a zero to the code, and a one when moving right
- *
- * @param c
- */
- public void countChar(int c) {
- HuffmanNode value = encodeTable.get(c);
- if (value == null) {
- HuffmanNode node = new HuffmanNode(c);
- node.count++;
- encodeTable.put(c, node);
- } else {
- value.count++;
- }
- for (int hmkey : encodeTable.keySet()) {
- // Integer key = hmkey;
- String character = encodeTable.get(hmkey).toString();
- // System.out.println("HashMap encodeTable:");
- // System.out.println( "Character =" + character); //"Key = " + key + " " +
- }
- }
- /**
- * Encode a string
- *
- * @param input the text to encode
- * @param output the destination for the encoded text
- */
- public void encode(Reader input, BitSink output) throws IOException {
- // read characters from input, find the character codes in the
- // table and write the code to output
- /*
- int c = input.read();
- while (c != -1) {
- output.writeBit(c);
- c = input.read();*/
- HuffmanNode root = buildTree();
- //traverse(root);
- // output.writeBit(root);
- }
- /**
- * Decode a huffman encoded text. See assignment text for details
- *
- * @param input the encoded text
- * @param output the destination for the decoded text.
- */
- public void decode(BitSource input, Writer output) throws IOException {
- // repeat the following until all bits are read
- // read bits from input, use the bit value to select a child
- // when you have reached a leaf node, write the character from
- // the leaf node to output
- }
- private HuffmanNode buildTree() {
- PriorityQueue<HuffmanNode> pq = new PriorityQueue<>();
- for(int ch : encodeTable.keySet()) {
- HuffmanNode newNode = new HuffmanNode(c);
- newNode.zero = null;
- newNode.one = null;
- pq.add(encodeTable.get(ch));
- }
- while(pq.size() > 1) {
- HuffmanNode zero = pq.remove();
- HuffmanNode one = pq.remove();
- HuffmanNode parent = new HuffmanNode('\0');
- parent.count = zero.count + one.count;
- pq.add(parent);
- }
- while(!pq.isEmpty()) {
- // System.out.println("TEST2 :: " + pq);
- System.out.println("PQ = " + pq.remove());
- }
- return pq.poll();
- }
- /**
- * The node class. It must implement comparable, so the nodes can be
- * compared by the priority queue.
- *
- */
- private class HuffmanNode implements Comparable<HuffmanNode>
- {
- int c; // the character that this node represents
- int count; // the number of occurences of the character in the txt
- int[] code; // the huffman code for the character
- HuffmanNode zero, one; // the children of the node
- /**
- * Constructor for leaf nodes
- *
- * @param c
- */
- public HuffmanNode(int c) {
- this.c = c;
- count = 0;
- zero = one = null;
- }
- boolean isLeaf() {
- return this.zero == null && this.one == null;
- }
- /**
- * Constructor for creating a parent for two nodes
- *
- * @param zero
- * @param one
- */
- public HuffmanNode(HuffmanNode zero, HuffmanNode one) {
- this.zero = zero;
- this.one = one;
- this.c = 0;
- count = zero.count + one.count;
- }
- /**
- * ToString is useful for debugging...
- *
- * @return
- */
- public String toString() {
- StringWriter out1 = new StringWriter();
- PrintWriter out2 = new PrintWriter(out1);
- out2.format("%3c %5d ", c, count);
- //for (int i : code) MÅ FINNE CODE FOR Å BRUKE DENNE.
- // out2.print(i);
- out2.flush();
- out1.flush();
- return out1.toString();
- }
- /**
- * compareTo is defined in the Comparable interface. A priority queue
- * will call this method to determine which of two nodes go first.
- *
- * @param that the node to compare this to.
- * @return this.count - that.count
- */
- public int compareTo(HuffmanNode that) {
- return - (that.count - this.count);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement