import java.util.*;
import java.io.*;
public class HuffmanCode {
private Queue<HuffmanNode> queue;
private HuffmanNode overallRoot;
public HuffmanCode(int[] frequencies) {
queue = new PriorityQueue<HuffmanNode>();
for (int i = 0; i < frequencies.length; i++) {
if (frequencies[i] > 0) {
HuffmanNode node = new HuffmanNode(frequencies[i]);
node.ascii = i;
queue.add(node);
}
}
overallRoot = buildTree();
}
public HuffmanCode(Scanner input) {
overallRoot = new HuffmanNode(-1);
while (input.hasNext()) {
int asciiValue = Integer.parseInt(input.nextLine());
String code = input.nextLine();
overallRoot = reconstructTree(overallRoot, code, asciiValue);
}
}
private HuffmanNode buildTree() {
if (queue.size() == 1) {
return queue.remove();
} else {
HuffmanNode nodeLeft = queue.remove();
HuffmanNode nodeRight = queue.remove();
HuffmanNode nodeNew = new HuffmanNode(nodeLeft.frequency + nodeRight.frequency);
nodeNew.left = nodeLeft;
nodeNew.right = nodeRight;
queue.add(nodeNew);
return buildTree();
}
}
private HuffmanNode reconstructTree(HuffmanNode root, String code, int asciiValue) {
if (code.isEmpty()) {
root.ascii = asciiValue;
return root;
} else {
char next = code.charAt(0);
if (next == '0') {
if (root.left == null) {
root.left = new HuffmanNode(-1);
}
root.left = reconstructTree(root.left, code.substring(1), asciiValue);
} else if (next == '1') {
if (root.right == null) {
root.right = new HuffmanNode(-1);
}
root.right = reconstructTree(root.right, code.substring(1), asciiValue);
}
}
return root;
}
public void save(PrintStream output) {
save(output, overallRoot, "");
}
private void save(PrintStream output, HuffmanNode root, String code) {
if (root != null) {
if (root.ascii != -1) {
output.println(root.ascii);
output.println(code);
}
save(output, root.left, code + "0");
save(output, root.right, code + "1");
}
}
public void translate(BitInputStream input, PrintStream output) {
while (input.hasNextBit()) {
translate(overallRoot, input, output);
}
}
private void translate(HuffmanNode curr, BitInputStream input, PrintStream output) {
if (curr.left == null && curr.right == null) {
output.write(curr.ascii);
} else {
int token = input.nextBit();
if (token == 0) {
translate(curr.left, input, output);
} else {
translate(curr.right, input, output);
}
}
}
private static class HuffmanNode implements Comparable<HuffmanNode> {
public int frequency;
public int ascii;
public HuffmanNode left;
public HuffmanNode right;
public HuffmanNode(int frequency) {
this.frequency = frequency;
this.ascii = -1;
}
public int compareTo(HuffmanNode other) {
return this.frequency - other.frequency;
}
}
}
*code is provided and the error is in the picture, please help with how to fix the error*
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images
- Implement a circular queue by writing circular.h and circular.c. Here are the contents of circular.h: #define QUEUE_SIZE 100 typedef struct { int head; int tail; int itemCount; int items[QUEUE_SIZE]; } CircularQueue; void CircularInitialize(CircularQueue * q); void CircularEnqueue (CircularQueue * q, int value); int Circular Dequeue (CircularQueue * q, int *pValue); Using an array, implement a circular queue of up to 100 elements of type integer. Implement the functions CircularEnqueue() and Circular Dequeue () to place numbers in the head of the queue and read them from the tail of the queue tail. A circular queue only saves the last n entries (where n is the number of elements in the queue). Overwrite the oldest entries with newest entries once the queue is full.arrow_forwardDraw a UML class diagram for the following code: import java.util.*; public class QueueOperationsDemo { public static void main(String[] args) { Queue<String> linkedListQueue = new LinkedList<>(); linkedListQueue.add("Apple"); linkedListQueue.add("Banana"); linkedListQueue.add("Cherry"); System.out.println("Is linkedListQueue empty? " + linkedListQueue.isEmpty()); System.out.println("Front element: " + linkedListQueue.peek()); System.out.println("Removed element: " + linkedListQueue.remove()); System.out.println("Front element after removal: " + linkedListQueue.peek()); Queue<Integer> arrayDequeQueue = new ArrayDeque<>(); arrayDequeQueue.add(10); arrayDequeQueue.add(20); arrayDequeQueue.add(30); System.out.println("Is arrayDequeQueue empty? " + arrayDequeQueue.isEmpty()); System.out.println("Front element: " + arrayDequeQueue.peek());…arrow_forwardJava Algorithm Programming Question Implement the ADT queue by using a circular linked list. Recall that this list has only an external reference to its last note. Example Output: Create a queue: isEmpty () returns true Add to queue to get Joe Jess Jim Jill Jane Jerry isEmpty () returns false Testing getFront and dequeue: Joe is at the front of the queue. Joe is removed from the front of the queue. Jess is at the front of the queue. Jess is removed from the front of the queue. Jim is at the front of the queue. Jim is removed from the front of the queue. Jill is at the front of the queue. Jill is removed from the front of the queue. Jane is at the front of the queue. Jane is removed from the front of the queue. Jerry is at the front of the queue. Jerry is removed from the front of the queue. The queue should be empty: isEmpty() returns true Add to queue to get Joe Jess Jim Testing clear: isEmpty() returns true Add to queue to get Joe Jess Jim Joe is at the front of the queue. Joe is…arrow_forward
- Change the __str__ method of the Queue class (provided below) so that it prints each object in the queue along with its order in the queue (see sample output below). class Queue(): def __init__(self): self.queue = [] # implement with Python lists! # start of physical Python list == front of a queue # end of physical Python list == back of a queue def enqueue(self, new_obj): self.queue.append(new_obj); def dequeue(self): return self.queue.pop(0) def peek(self): return self.queue[0] def bad_luck(self): return self.queue[-1] def __str__(self): return str(self.queue) # let's try a more fun waySample output: >>> my_queue = Queue()>>> everyone = ["ESC", "ABC", "YOLO", "HTC"]>>> for initials in everyone:>>> my_queue.enqueue(initials)>>> print(my_queue)Output: 1: ESC2: ABC3: YOLO4: HTCarrow_forwarddef has_at_least(queue: Queue, n: int) -> bool:"""Return true iff queue contains at least n items. Precondition: n >= 0 >>> queue = Queue()>>> queue.enqueue(1)>>> queue.enqueue(2)>>> queue.enqueue(3)>>> has_at_least(queue, 3)True"""arrow_forwardjava Suppose queue is defined as LinkedQueue<Integer> queue = new LinkedQueue<>(); Show what is written by the following segment of code. SHOW YOUR WORK.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education