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.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileInputStream;
public class Compress {
public static void main(String[] 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 FileReader("WarAndPeace");
int c;
while ((c = inputStream.read())!=-1){
Character ch =(char) c;
int i = letters.indexOf(ch);
if(i>-1)
frequencies.set(i, frequencies.get(i)+1);
else
{
letters.add(ch);
frequencies.add(1);
}
}
} finally {
if (inputStream != null){
inputStream.close();
}
}
for(int j=0; j elts = levelorderElements(r);
System.out.println("Now do levels");
for(HuffmanNode nd : elts)
if(nd.isLeaf()) System.out.println(nd.getCharacter()+""+nd.getCode());
}
/* Recursively descend to leaves, constructing code as we go.*/
public static void printLeaves(HuffmanNode r){
if(r == null) return;
if((r.getLeft()== null) &&
(r.getRight()== null)){
System.out.println(r.getCode()+""+r);
return;
}
if(r.getLeft()!= null){
r.getLeft().setCode(r.getCode()+'0');
printLeaves(r.getLeft());
}
if(r.getRight()!= null){
r.getRight().setCode(r.getCode()+'1');
printLeaves(r.getRight());
}
}
public static ArrayList levelorderElements(HuffmanNode r){
// return nodes in level order: root, then children of root, etc.
if(r==null)
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
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.org
Title: War and Peace
Author: Leo Tolstoy
Posting Date: January 10,2009[EBook #2600]
Release Date: April, 2001
Language: English
Character set encoding: ASCII
*** START OF THIS PROJECT GUTENBERG EBOOK WAR AND PEACE ***
```
An Anonymous Volunteer
Write the proper code IN JAVA and answer both

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 Programming Questions!