Please help me complete this assignment. or comment so I can assist in any way I
ID: 3540388 • Letter: P
Question
Please help me complete this assignment. or comment so I can assist in any way I can in case you have questions. Thank you in advance. - We%u2019re building a binary tree, where the %u201Cdata%u201D is a count of the frequency of each character.
public class HuffmanNode implements Comparable<HuffmanNode> {
public int frequency; public char character; public HuffmanNode left; public HuffmanNode right;
The Huffman Node class will also need a boolean isLeaf() method, plus a static method to actually provide a count of the
characters in an input file, and place those counts into a Map, with character as the unique key mapped into the integer
counts of that character: public static Map<Character, Integer> getCounts(FileInputStream input)
Your Huffman Tree class must have the following defined: public HuffmanTree(Map<Character, Integer> counts) // Constructor
public StringBuilder compress(InputStream inputFile) // inputFile is a text file public StringBuilder decompress(StringBuilder inputString) //inputString 1%u2019s & 0%u2019s public String printSideways() (HERE IS WHAT I HAVE SO FAR) PRETTY MUCH NEED A LOT - THAT IS WHY IT IS 3000 points.
import java.io.FileInputStream;
import java.util.Map;
HUFFMANTREE.JAVA
public class HuffmanTree {
public HuffmanTree(Map<Character, Integer> counts) {
// TODO Auto-generated constructor stub
}
public String printSideways() {
String sideways = "";
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;
}
}
HUFFMANNODE.JAVA
import java.io.FileInputStream;
import java.util.*;
public class HuffmanNode implements Comparable<HuffmanNode> {
public int frequency;
public char character;
public HuffmanNode left;
public HuffmanNode right;
public HuffmanNode (int frequency, char character) {
this.frequency = frequency;
this.character = character;
left = null;
right = null;
}
public HuffmanNode (HuffmanNode left, HuffmanNode right) {
this.left = left;
this.right = right;
this.frequency = left.frequency + right.frequency;
}
public boolean isLeaf() {
return (left == null & right == null);
}
public static Map<Character, Integer> getCounts(FileInputStream input) {
Map<Character, Integer> charCounts = new TreeMap<Character, Integer>();
Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
String word = scanner.nextLine();
char ch;
for(int i = 0; i < word.length(); i++) {
ch = word.charAt(i);
if (charCounts.containsKey(ch)) {
int count = charCounts.get(ch);
charCounts.put(ch, count+1);
} else {
charCounts.put(ch, 1);
}
}
}
return charCounts;
}
public int getFrequency() {
return this.frequency;
}
public char getCharacter() {
return this.character;
}
public HuffmanNode getLeft() {
return left;
}
public HuffmanNode getRight() {
return right;
}
public int compareTo(HuffmanNode other) {
if (frequency < other.frequency) {
return -1;
} else if (frequency > other.frequency) {
return 1;
} else {
return 0;
}
}
}
Explanation / Answer
/**
* A node class used for constructing a Huffman encoding tree
*
* !!!!! DO NOT MODIFY THIS FILE !!!!!
*/
public class HuffmanNode implements Comparable<HuffmanNode>
{
private byte value;
private int freq;
private HuffmanNode left, right, parent;
public HuffmanNode(byte value, int freq)
{
if (freq < 0)
throw new IllegalArgumentException("Bad frequency");
this.value = value;
this.freq = freq;
this.left = null;
this.right = null;
this.parent = null;
}
public HuffmanNode(HuffmanNode left, HuffmanNode right)
{
if (left == null || right == null)
throw new IllegalArgumentException("Bad children nodes");
this.value = 0;
this.freq = left.getFreq() + right.getFreq();
this.left = left;
this.right = right;
this.parent = null;
left.parent = this;
right.parent = this;
}
public byte getValue()
{
return value;
}
public int getFreq()
{
return freq;
}
public HuffmanNode getLeft()
{
return left;
}
public HuffmanNode getRight()
{
return right;
}
public HuffmanNode getParent()
{
return parent;
}
public boolean isLeaf()
{
return (left == null);
}
public int compareTo(HuffmanNode n)
{
return (freq - n.getFreq());
}
public String toString()
{
if (isLeaf())
return "(" + (char)value + " -> " + freq + ")";
else
return "[" + left + ", " + right + "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.