Finish up this \'Huffman coding\'. I did almost every codes. -------------------
ID: 3695188 • Letter: F
Question
Finish up this 'Huffman coding'. I did almost every codes.
------------------------------------------------------------------------------------
import java.util.*;
public class HuffmanEncoding {
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
System.out.println("Enter a text: ");
String txt = kbd.nextLine();
int[] n = getCharFreq(txt);
System.out.printf("%14s%15s%15s%15s ",
"ASCII code", "Character", "Frequency", "Code");
//Tree tree = getHuffmanTree(n);
for(int i = 0; i < 256; i++)
{
if(n[i] != 0)
{
System.out.printf("%14d%15c%15d ",
i, (char)i, n[i]);
//System.out.println((char)i + " " + n[i]);
}
}
}
// This is the frequency
public static int[] getCharFreq(String txt)
{
int[] counts = new int[256];
for(int i = 0; i < txt.length(); i++)
{
counts[(int)txt.charAt(i)]++;
}
return counts;
}
}
------------------------------------------------------------------------------------
public class Node {
private char element;
public int weight;
public Node left;
public Node right;
private String code = "";
public Node()
{
}
public Node(int weight, char element)
{
this.weight = weight;
this.element = element;
}
}
-------------------------------------------------------------------------
public class Tree {
private Node root; // The root of the tree
//Create a tree with two subtrees
public Tree(Tree t1, Tree t2)
{
root = new Node();
root.left = t1.root;
root.right = t2.root;
root.weight = t1.root.weight + t2.root.weight;
}
//Create a tree containing a leaf node
public Tree(int weight, char element)
{
root = new Node(weight, element);
}
}
---------------------------------------------------------------------
here is the output I have and I want you to do the 'Code' column
as Huffman coding.
run Enter a text Mississippi river ASCII code 32 Character requency Code 101 105 112 114 115 118 BUILD SUCCESSFUL (total time: 9 seconds)Explanation / Answer
import java.util.Scanner; public class HuffmanCode { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a text: "); String text = input.nextLine(); int[] counts = getCharacterFrequency(text); // Count frequency System.out.printf("%-15s%-15s%-15s%-15s ", "ASCII Code", "Character", "Frequency", "Code"); Tree tree = getHuffmanTree(counts); // Create a Huffman tree String[] codes = getCode(tree.root); // Get codes for (int i = 0; i 1) { Tree t1 = heap.remove(); // Remove the smallest weight tree Tree t2 = heap.remove(); // Remove the next smallest weight heap.add(new Tree(t1, t2)); // Combine two trees } return heap.remove(); // The final tree } /** Get the frequency of the characters */ public static int[] getCharacterFrequency(String text) { int[] counts = new int[256]; // 256 ASCII characters for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.