Modify HuffmanTreejava and HuffmanNodejava into HuffmanTreeR
Modify HuffmanTree.java and HuffmanNode.java (into HuffmanTreeRary.java and HuffmanNodeRary.java) to allow the user to select the value of r 9 (where r is the radix) to construct an r ary Huffman Tree. Once finished, verify the accuracy of your tree by running it for the English Alphabet for r = 3, 4, 5, 6, 7, 8, 9. For each value of r, compute the average codeword length. Write these in a table (r vs. ACW L(Cr)). Hint: i) instead of the left and right child nodes for the binary tree, we instead need to add an ArrayList of children. ii) The construction of the first Huffman parent node and it’s children requires that we think about (and answer correctly) the question “how many children should the first constructed node have?” The answer depends on n (the number of total codewords), and r, the radix, and will generally lie from 2 to r. But this value must be computed, or your Huffman tree may not be optimal.
/ HuffmanTree.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class HuffmanTree {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print(\"Enter the file name: \");
String fileName = in.nextLine();
ArrayList nodes = new ArrayList<>();
ArrayList temp = new ArrayList<>();
try {
Scanner fin = new Scanner(new FileReader(fileName));
String symbol;
double prob;
while(fin.hasNext()){
symbol = fin.next();
prob = fin.nextDouble();
nodes.add(findInsertIndex(nodes, prob), new HuffmanNode(symbol, prob));
}
fin.close();
temp.addAll(nodes);
HuffmanNode root = createHuffmanTree(temp);
setCodeWords(root);
double avg = 0;
for (HuffmanNode node : nodes) {
System.out.println(node.getS() + \": \" + node.getC() + \": \" + node.getL());
avg += node.getL();
}
if(avg != 0)
avg /= nodes.size();
System.out.println(\"Average code length is: \" + avg);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static HuffmanNode createHuffmanTree(ArrayList nodes){
HuffmanNode root = null;
if(nodes.size() > 0) {
HuffmanNode node0, node1, node;
while (nodes.size() > 1) {
node0 = nodes.get(0);
node1 = nodes.get(1);
node = new HuffmanNode(null, node0.getP() + node1.getP());
node.setLeft(node0);
node.setRight(node1);
nodes.remove(0);
nodes.remove(0);
nodes.add(findInsertIndex(nodes, node.getP()), node);
}
root = nodes.get(0);
}
return root;
}
private static void setCodeWords(HuffmanNode root){
if(root != null){
setCodeWords(root, \"\");
}
}
private static void setCodeWords(HuffmanNode root, String codeWord){
if(root.getS() != null){
root.setC(codeWord);
root.setL(codeWord.length());
}
else{
setCodeWords(root.getLeft(), codeWord + \"0\");
setCodeWords(root.getRight(), codeWord + \"1\");
}
}
private static int findInsertIndex(ArrayList nodes, double prob){
for(int i = 0; i < nodes.size(); ++i){
if(prob < nodes.get(i).getP()){
return i;
}
}
return nodes.size();
}
}
// HuffmanNode.java
public class HuffmanNode {
private String S;
private double P;
private String C;
private int L;
private HuffmanNode left, right;
public HuffmanNode(String s, double p) {
S = s;
P = p;
}
public String getS() {
return S;
}
public void setS(String s) {
S = s;
}
public double getP() {
return P;
}
public void setP(double p) {
P = p;
}
public String getC() {
return C;
}
public void setC(String c) {
C = c;
}
public int getL() {
return L;
}
public void setL(int l) {
L = l;
}
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;
}
}
//EnglishAlphabet.txt (symbols with probability)
Solution
Programming foe Huffman Code: Huffman coding is based on the frequency of occurance of a data item.The principle is to use a lower number of bits to encode the data that occurs more frequently.
//Huffman code
public class HuffmanTree {
public Node root;
public HuffmanTree(){
this.root = new Node();
}
public void add(char data, String sequence){
Node temp = this.root;
int i = 0;
for(i=0;i<sequence.length()-1;i++){
if(sequence.charAt(i)==\'0\'){
if(temp.left == null){
temp.left = new Node();
temp = temp.left;
}
else{
temp = (Node) temp.left;
}
}
else
if(sequence.charAt(i)==\'1\')
{
if(temp.right == null){
temp.right = new Node();
temp = temp.right;
}
else
{
temp = (Node) temp.right;
}
}}
if(sequence.charAt(i)==\'0\')
{
temp.left = new Node(data);
}
else{
temp.right = new Node(data);
}
}
public class HuffmanNode
{
private String S;
private double P;
private String C;
private int L;
private HuffmanNode left, right;
public HuffmanNode(String s, double p)
{
S = s;
P = p;
}
public String getS()
{
return S;
}
public void setS(String s)
{
S = s;
}
public double getP()
{
return P;
}
public void setP(double p)
{
P = p;
}
public String getC()
{
return C;
}
public void setC(String c)
{
C = c;
}
public int getL() {
return L;
}
public void setL(int l)
{
L = l;
}
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;
}
}




