Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

All i need is analysis of this question and code below public class HuffmanNode { private String S; private double P; private String C; private

image text in transcribed

All i need is analysis of this question and code below

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;

}

}

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){

if(args.length > 0){

String fileName = args[0];

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

if(prob

return i;

}

}

return nodes.size();

}

}

Output:

B: 100: 3

D: 101: 3

A: 00: 2

E: 01: 2

C: 11: 2

Average code length is: 2.4

5. Design, code, and test a program that implements the optimal Huffman algorithm discussed in class (with a time complexity of O(n logan), n being the number of code symbols). 5. Design, code, and test a program that implements the optimal Huffman algorithm discussed in class (with a time complexity of O(n logan), n being the number of code symbols)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions