Question: Complete the Compress.java file so that it computes the Huffman com - pression code for WarAndPeace. Your program should use a level - order traversal
Complete the Compress.java file so that it computes the Huffman com
pression code for WarAndPeace. Your program should use a levelorder
traversal of the binary tree constructed by the Huffman algorithm to print
the codes in order of length shorter codes first War and Peace is the novel used, a snippet is attached. Send exactly what to put in the one code file.
HuffmanNode.java
Node for constructing binary tree in Huffman algorithm.
public class HuffmanNode implements Comparable
private char c; The character,
private double f; the character's frequency.
private int f; the character's frequency.
private HuffmanNode left;
private HuffmanNode right;
private String code; This will contain the optimal binary code.
HuffmanNodechar ch int fr HuffmanNode l HuffmanNode r
c ch;
f fr;
left l;
right r;
code ;
public char getCharacter
return c;
public int getFrequency
return f;
public HuffmanNode getLeft
return left;
public HuffmanNode getRight
return right;
public void setCharacterchar ch
c ch;
public void setFrequencyint fr
f fr;
public String getCode
return code;
public void setCodeString c
code c;
public boolean isLeaf
returnleftnull&&rightnull;
public String toString
String s Character.toStringc
Integer.toStringf;
return s;
public int compareToHuffmanNode h
ifthisf hf
return ;
else ifthisf hf
return ;
else
return ;
Compress.java
Implement Huffman code compression for example in notes. Create a
binary tree with nodes defined in HuffmanNode.java class. We need
a MinHeap; cheat by switching the comparator in HuffmanNode.java.
import java.util.Queue;
import java.util.ArrayList;
import java.util.LinkedList;
import java.ioFile;
import java.ioFileReader;
import java.ioIOException;
import java.ioFileInputStream;
public class Compress
public static void mainString args throws IOException
MaxHeap Q new MaxHeap;
Enter data from example in class.
FileReader inputStream null;
ArrayList letters new ArrayList;
ArrayList frequencies new ArrayList;
try
inputStream new FileReaderWarAndPeace;
int c;
while c inputStream.read
Character ch char c;
int i letters.indexOfch;
ifi
frequencies.seti frequencies.geti;
else
letters.addch;
frequencies.add;
finally
if inputStream null
inputStream.close;
forint j; j elts levelorderElementsr;
System.out.printlnNow do levels";
forHuffmanNode nd : elts
ifndisLeaf System.out.printlnndgetCharacterndgetCode;
Recursively descend to leaves, constructing code as we go
public static void printLeavesHuffmanNode r
ifr null return;
ifrgetLeft null &&
rgetRight null
System.out.printlnrgetCoder;
return;
ifrgetLeft null
rgetLeftsetCodergetCode;
printLeavesrgetLeft;
ifrgetRight null
rgetRightsetCodergetCode;
printLeavesrgetRight;
public static ArrayList levelorderElementsHuffmanNode r
return nodes in level order: root, then children of root, etc.
ifrnull
return null;
ArrayList elts new ArrayList;
Fill in
return elts;
The Project Gutenberg EBook of War and Peace, by Leo Tolstoy
This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever. You may copy it give it away or
reuse it under the terms of the Project Gutenberg License included
with this eBook or online at wwwgutenberg.org
Title: War and Peace
Author: Leo Tolstoy
Posting Date: January EBook #
Release Date: April,
Language: English
Character set encoding: ASCII
START OF THIS PROJECT GUTENBERG EBOOK WAR AND PEACE
An Anonymous Volunteer Complete the Compress.java file so that it computes the Huffman compression code for WarAndPeace. Your program should use a levelorder traversal of the binary tree constructed by the Huffman algorithm to print the codes in order of length shorter codes first
You can download the files Compress.java, HuffmanNode.java, and WarAndPeace from Canvas and MaxHeap.java if you have not downloaded it previously
Suppose that you need to "approximately sort" arrays of length n in the sense that if the i th smallest element of the input is at index j in the output, then ijleq k for some fixed number k small compared to n Describe an efficient algorithm and characterize the asymptotic running time state whether your analysis is worstcase or averagecase
The Project Gutenberg EBook of War and Peace, by Leo Tolstoy
This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever. You may copy it give it away or
reuse it under the terms of the Project Gutenberg License included
with this eBook or online at wwwgutenberg.org
Title: War
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
