I need the below two methods called COMPRESS and DECOMPRESS to return the items
ID: 3540736 • Letter: I
Question
I need the below two methods called COMPRESS and DECOMPRESS to return the items indicated in their method headers. If I have made an error anywhere else please let me know
import java.io.FileInputStream;
import java.util.*;
public class HuffmanTree {
public HuffmanNode overallRoot;
public HuffmanTree(Map<Character, Integer> counts) {
PriorityQueue<HuffmanNode> pq = new PriorityQueue<HuffmanNode>();
Set<Character> characters = counts.keySet();
for (Character c : characters) {
pq.add(new HuffmanNode(c, c.charValue()));
}
System.out.println(pq.toString());
}
public String printSideways() {
return printSideways(overallRoot, 0);
}
private String printSideways(HuffmanNode root, int level) {
String sideways = "";
if (root != null) {
printSideways(root.right, level + 1);
for (int i = 0; i < level; i++) {
sideways += (" ");
}
sideways += (root.frequency);
printSideways(root.left, level + 1);
}
return sideways;
}
public StringBuilder compress(FileInputStream inputFile) {
// TODO Auto-generated method stub
return null;
}
public String decompress(StringBuilder inputString) {
// TODO Auto-generated method stub
return null;
}
}
Explanation / Answer
import java.util.*; import java.io.*; public class HuffmanTree { private HuffNode root; private ArrayList ListOfChars; public HuffmanTree(Map counts) { /* This constructor constructs the huffman tree and fills all the characters in an ArrayList */ if(counts==null) { throw new IllegalArgumentException(); } FillSet(counts); PriorityQueue q=new PriorityQueue(counts.size()); for(char c:counts.keySet()) { q.add(new HuffNode(c,counts.get(c))); } add(q); } public Map createEncodings() {//This method creates encodings for all characters Map map=new TreeMap(); for(int i=0;i1) { HuffNode temp1=q.remove(); HuffNode temp2=q.remove(); HuffNode total=new HuffNode(temp1.count+temp2.count); total.left=temp1; total.right=temp2; q.add(total); } root=q.remove(); } private class HuffNode implements Comparable { // Class for the HuffNode private static final char default_char='~'; private char c; private int count; private HuffNode left; private HuffNode right; // Constructor taking a character and an integer private HuffNode(char c,int count) { this.c=c; this.count=count; this.left=null; this.right=null; } // Constructor taking an integer private HuffNode(int count) { this.c=default_char; this.count=count; this.left=null; this.right=null; } public int compareTo(HuffNode node) { return this.count-node.count; } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.