Question: Write the proper code IN JAVA and answer both parts. Last time it was wrong. The War and Peace is a text file - -
Write the proper code IN JAVA and answer both parts. Last time it was wrong. The War and Peace is a text file is just the book by Leo Tolstoy
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
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
