Question: Read through the My Java code and create an understanding of it . / / / My Code / / / import java.util. * ;

Read through the My Java code and create an understanding of it.
///My Code///
import java.util.*;
public class Huffman {
public static void main(String[] args){
String message = "Hello world";
Scanner scanner = new Scanner(System.in);
while (!message.equalsIgnoreCase("quit")){
System.out.println("Enter a message: ");
message = scanner.nextLine();
new Huffman().process(message);
}
System.out.println("BYE!!!");
scanner.close();
}
private void process(String phrase){
System.out.println("Original: "+ phrase);
PriorityQueue priorityQueue = new PriorityQueue(phrase.length());
for (int i =0; i < phrase.length(); i++){
char c = phrase.charAt(i);
priorityQueue.insert(c);
}
while (priorityQueue.size()>1){
Node n1= priorityQueue.remove();
Node n2= priorityQueue.remove();
Node p = createParent(n1, n2);
priorityQueue.insert(p);
}
Tree huffmanTree = new Tree();
huffmanTree.setRoot(priorityQueue.remove());
String code = huffmanTree.encode(phrase);
System.out.println("Coded: "+ code);
String message = huffmanTree.decode(code);
System.out.println("Decoded: "+ message);
}
private Node createParent(Node n1, Node n2){
Node nn = new Node('\0', n1.frequency + n2.frequency);
if (n1.frequency < n2.frequency){
nn.leftChild = n1;
nn.rightChild = n2;
} else {
nn.leftChild = n2;
nn.rightChild = n1;
}
return nn;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!