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)