Question

In: Computer Science

Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters...

Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters and frequencies as listed below. The total number of nodes is n = 6.

Solutions

Expert Solution

Dear User no character and frequency is found listed below, so here I am using 6 random characters and frequencies

Total number of nodes = 6

Coding Language used: Java NetBeans.

import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Comparator;
  
class HuffmanNode {
  
int data;
char c;
  
HuffmanNode left;
HuffmanNode right;
}
class MyComparator implements Comparator<HuffmanNode> {
public int compare(HuffmanNode x, HuffmanNode y)
{
  
return x.data - y.data;
}
}
  
public class Huffman {
  
public static void printCode(HuffmanNode root, String s)
{
  
if (root.left
== null
&& root.right
== null
&& Character.isLetter(root.c)) {
  
System.out.println(root.c + ":" + s);
  
return;
}
  
printCode(root.left, s + "0");
printCode(root.right, s + "1");
}
  
// main function
public static void main(String[] args)
{
  
Scanner s = new Scanner(System.in);
  
int n = 6;
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f' };
int[] charfreq = { 5, 9, 12, 13, 16, 45 };
  
PriorityQueue<HuffmanNode> q
= new PriorityQueue<HuffmanNode>(n, new MyComparator());
  
for (int i = 0; i < n; i++) {
  
HuffmanNode hn = new HuffmanNode();
  
hn.c = charArray[i];
hn.data = charfreq[i];
  
hn.left = null;
hn.right = null;
  
q.add(hn);
}
  
HuffmanNode root = null;
  
while (q.size() > 1) {
  
HuffmanNode x = q.peek();
q.poll();
  
HuffmanNode y = q.peek();
q.poll();
  
HuffmanNode f = new HuffmanNode();
  
f.data = x.data + y.data;
f.c = '-';
  
f.left = x;
  
f.right = y;

root = f;
  
q.add(f);
}
  
printCode(root, "");
}
}
  

// Happy Coding


Related Solutions

Huffman Coding Huffman coding is a lossless data compression algorithm. The idea is to assign variable-...
Huffman Coding Huffman coding is a lossless data compression algorithm. The idea is to assign variable- length codes to input characters; lengths of the assigned codes are based on the frequencies of corresponding characters. The most frequent character gets the smallest code and the least frequent character gets the largest code. The variable-length codes assigned to input characters are Prefix Codes, means the codes (bit sequences) are assigned in such a way that the code assigned to one character is...
Prove / Disprove the following 2 properties using Huffman Coding: a) If some characters occur with...
Prove / Disprove the following 2 properties using Huffman Coding: a) If some characters occur with a frequency of more than 2/5, then there is a codeword that is guaranteed to be a length of 1. b) If all characters occur with a frequency of less than 1/3, then there are no codewords guaranteed to be a length of 1.
Encode “GOOGLE” using Huffman coding algorithm. Show that the entropy of “GOOGLE” specifies the lower bound...
Encode “GOOGLE” using Huffman coding algorithm. Show that the entropy of “GOOGLE” specifies the lower bound for the average number of bits to code each symbol in “GOOGLE”.
Language: Java(Netbeans) Implement a simple calculator.
Language: Java(Netbeans) Implement a simple calculator.
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in...
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in the "How it works" section), where you take some user-defined number of steps that randomly step in W and I and are scaled by a step_size. This means you don't want to take steps of a discrete size, but instead use a random distribution of step sizes that are scaled by your step_size variable. You'll want to take steps that have an equal chance...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT