Have multiple Java files for huffman code: Huff.java HuffC.java FileIO.java File
ID: 3740333 • Letter: H
Question
Have multiple Java files for huffman code:
Huff.java
HuffC.java
FileIO.java
FileIOC.java
Bit.java
BitC.java
etc....
How do I utilize the code written in FileIO/IOC that compresses the file into huff code in the huff model to solve:
Create a new (empty) symbol table.
Open the input text file. For each unique character (or symbol) in the input file, create an entry in the symbol table for that symbol. Use the symbol table entry to count the frequency of the character in the input file. Close the input file when done.
Create a new priority queue for Huffman trees. Recall that the compareTo function for Huffman trees compares trees by their weights. For each key (i.e., symbol) K in the symbol table, look up it's frequency f and create a Huffman tree leaf node with weight f. Insert the leaf node into the priority queue.
While the priority queue has more than one element, remove two trees t1 and t2 from the priority queue. Construct a new tree t with t1 and t2 as left and right children (resp.) and with weight = t1.weight() + t2.weight(). Insert the new tree t into the priority queue.
The priority queue now contains exactly one element: the Huffman coding tree for the input text. Remove the tree from the priority queue. Recursively walk the coding tree recording the binary bit path P at each step. When the recursive walk arrives at a leaf with symbol A, update A's binary bit pattern entry in the symbol table to record the binary path that led from the root to leaf A.
The symbol table now has the information required to write the variable length codes to the binary output file.
Open the binary output file.
Write the signature bits to identify the file as a zip file.
Write out the 32-bit length of the symbol table.
Next write out the symbol frequency information. For each key in the symbol table, write the key (i.e., the character) using one byte and write its integer frequency using 4 bytes.
Reopen the input file.
For each occurrence of a character in the input file, look up it's binary bit pattern in the symbol table and write it to the output file.
Close the output file.
Explanation / Answer
Although a little more information would have helped. See if the java code snippet helps. public class HuffmanNode { int weight; char c; HuffmanNode left; HuffmanNode right; public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public char getC() { return c; } public void setC(char c) { this.c = c; } public HuffmanNode getLeft() { return left; } public void setLeft(HuffmanNode left) { this.left = left; } public HuffmanNode getRight() { return right; } public void setRight(HuffmanNode right) { this.right = right; } } class HuffmanComparator implements Comparator{ @Override public int compare(HuffmanNode o1, HuffmanNode o2) { return o1.weight > o2.weight ? o1.weight : o2.weight; } class Huff { public void readFromText(){ Map symbolTable= new HashMap(); //declare a pattern for the symbols in the txt Pattern p= Pattern.compile("[^a-z0-9]", Pattern.CASE_INSENSITIVE); String line=""; File sampleFile= new File(""); try(BufferedReader sampleBuffer = new BufferedReader(new FileReader(sampleFile))) { while ((line = sampleBuffer.readLine()) != null) { //1. read all symbols from the file and its frequency and make an entry in the table Matcher m = p.matcher(line); while (m.find()){ symbolTable.merge(line.charAt(m.start()), 1, (x,y) -> x + y); } } PriorityQueue q = new PriorityQueue(2, new HuffmanComparator()); //create a new HuffmanNode with the frequency as the weight of the node symbolTable.keySet().forEach( key -> { if(q.size() > 1){ HuffmanNode treee= new HuffmanNode(); treee.setWeight(q.peek().getWeight()); treee.setLeft(q.poll()); treee.setWeight(treee.getWeight() + q.peek().getWeight()); treee.setRight(q.poll()); q.add(treee); }else if(q.size() == 1){ binaryOutput(q.poll(), "0", symbolTable); } else { HuffmanNode node = new HuffmanNode(); node.setWeight(symbolTable.get(key)); node.c = key; q.add(node); } } ); } catch (Exception e) { e.printStackTrace(); } } public void binaryOutput(HuffmanNode node, String s, Map symbolTable) { // base case; if the left and right are null // then its a leaf node update the table // the code s generated by traversing the tree. FileWriter writer= null; if (node.left == null && node.right == null) { // c is the character in the node symbolTable.merge(symbolTable.get(node.getC()), s, (x,y) -> y=x ); } try { writer= new FileWriter("/Users/anupamrakshit/Documents/study/demo/src/main/java/com/example/demo/demo/resource/Bit.java"); writer.write(node.c + ":" +s); } catch (IOException e) { e.printStackTrace(); } finally{ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } // if we go to left then add "0" to the code. // if we go to the right add"1" to the code. // recursive calls for left and // right sub-tree of the generated tree. binaryOutput(node.left, s + "0", symbolTable); binaryOutput(node.right, s + "1", symbolTable); } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.