Question: Topic: HuffmanCode Policy: 1 ) Each lab is due at the end of each allocated Lab Section. The TA will be available to help you
Topic: HuffmanCode
Policy: Each lab is due at the end of each allocated Lab Section. The TA will be available to
help you for the labhomework
Exception: if you plan to do the lab in advance, you must submit your lab answer before the
start of your allocated lab section.
Instruction:
Finish public HuffmanNode encode char charArray, int charfreq;
Finish public String decodeFileHuffmanNode root, String simport java.util.Comparator;
import java.util.PriorityQueue;
public class LabCS
public static void mainString args
number of characters.
int n ;
char charArray ABIMSXZ;
int charfreq ;
HuffmanCode hc new HuffmanCode;
HuffmanNode root hcencodecharArraycharfreq;
print the codes by traversing the tree
hcprintCoderoot;
System.out.printlnhcdecodeFileroot;
System.out.printlnhcdecodeFileroot;
System.out.printlnhcdecodeFileroot;
System.out.printlnhcdecodeFileroot;
class HuffmanCode
recursive function to print the
huffmancode through the tree traversal.
Here s is the huffman code generated.
public void printCodeHuffmanNode root, String s
base case; if the left and right are null
then its a leaf node and we print
the code s generated by traversing the tree.
if rootleft null && root.right null
&& Character.isLetterrootc
c is the character in the node
System.out.printlnrootc : s;
return;
if we go to left then add to the code.
if we go to the right add" to the code.
recursive calls for left and
right subtree of the generated tree.
printCoderootleft, s ;
printCoderootright, s ;
public HuffmanNode encode char charArray, int charfreq
HuffmanNode root null; create a root node
creating a priority queue q
makes a minpriority queueminheap
creating a Huffman node object
and add it to the priority queue.
Here we will extract the two minimum value
from the heap each time until
its size reduces to extract until
all the nodes are extracted.
return root; replace it with root
public String decodeFileHuffmanNode root, String s
String ans ;
HuffmanNode curr root;
finish your code
return ans ;
node class is the basic structure
of each node present in the Huffman tree.
class HuffmanNode
int data; store frequency
char c; store character
HuffmanNode left;
HuffmanNode right;
comparator class helps to compare the node
on the basis of one of its attribute.
Here we will be compared
on the basis of data values of the nodes.
class MyComparator implements Comparator
public int compareHuffmanNode x HuffmanNode y
return xdata ydata;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
