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

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”.
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!
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.
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes...
using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes from user and show how best fit algorithm allocates them.
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum...
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum element in an unsorted list. Second, convert your recursive algorithm to a non-recursive (or iterative) implementation. For your input, populate an "unsorted list" with random elements between 1 and 1,000,000.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT