Create a WordCount class that uses the AVL tree class to count words from a file
ID: 3813456 • Letter: C
Question
Create a WordCount class that uses the AVL tree class to count words from a file. The trick here is that you should not have duplicate words in the list. Basically when a word is read from the file the tree should be checked to see if the word already exists in the tree. If it does exists the count for the word should simply be incremented, If it does not exist then the node should be added to the tree and the count for that particular word should be set to 1. You should have functionality that will print each word in the tree and how many times it appears. This is not simply reading a list of words from a file. Your WordCount class should read any text from the file and count the words in it. This is going to require a bit of parsing on your part. Please disgard punctuation characters like comma and period.Explanation / Answer
Explanation of the code is also given in comments:-
import java.util.*;
import java.lang.*;
import java.io.*;
/*
The progarm takes a text file for input and a file
for output. All the words are read from the input file. An alphabetical list of all the words that
were found, without repetition, is written to the output file with inorder scheme .
The words are stored in AVL tree
*/
public class AVLTreeWordList
{
static WordCount root; // Points to the root of the AVL sort
// tree and assume to be null
public static void main(String[] args)
{
TextReader in; // A stream for reading from the input file.
PrintWriter out; // A stream for writing to the output file.
String inputFileName; // Input file name, specified by the user.
String outputFileName; // Output file name, specified by the user.
root = null; // Start with an empty tree. (Not really necessary,
// since null is the default initial value anyway.)
/* Get the input file name from the user and try to create the
input stream. If not found print a message and terminate the program. */
TextIO.put("Specify the file name ");
inputFileName = TextIO.getln().trim();
try
{
in = new TextReader(new FileReader(inputFileName));
}
catch (FileNotFoundException e)
{
TextIO.putln("File Not Found "" + inputFileName + "".");
return;
}
/* Get the output file name from the user and try to create the
output stream. If there is an IOException, print a message
and terminate the program. */
TextIO.put("Specify the output file name");
outputFileName = TextIO.getln().trim();
try
{
out = new PrintWriter(new FileWriter(outputFileName));
}
catch (IOException e)
{
TextIO.putln("Output File Is Not Found"" + outputFileName + "".");
TextIO.putln(e.toString());
return;
}
/* Read all the words from the input stream and insert them into
the array of words*/
try
{
while (true)
{
// Skip non-letters in the input stream. If an
// end-of-stream has been reached, end the loop. Otherwise,
// read a word and insert it into the array of words.
while ( ! in.eof() && ! Character.isLetter(in.peek()) )
in.getAnyChar();
if (in.eof())
break;
insertWord(in.getAlpha());
}
}
catch (TextReader.Error e)
{
TextIO.putln("An error occurred while reading from the input file.");
TextIO.putln(e.toString());
return;
}
/* Write all the words from the AVL tree to the output stream. */
inorderOutput(root, out);
/* Finish up by checking for an error on the output stream and
printing either a warning message or a message that the words
have been output to the output file. */
if (out.checkError() == true)
{
TextIO.putln("Error occurred in writing output.");
}
else
{
TextIO.putln(" " + countNodes(root) + " words from "" + inputFileName +
"" output to "" + outputFileName + "".");
}
} // end main()
//------------- AVL sort tree ----------
static class WordCount
{
// An object of type WordCount represents one node
// in a binary tree of strings.
String item; // The data in this node.
int count;
WordCount left; // Pointer to left subtree.
WordCount right; // Pointer to right subtree.
WordCount(String str)
{
// Constructor. Make a node containing str.
item = str;
count=0;
}
static void inorderOutput(WordCount node, PrintWriter output)
{
// Output the items in the tree to which node points.
// An inorder listing is used, which
// will print the words in the sorted foem in alphabetical order
if (node != null)
{
inorderOutput( node.left, output );
output.println(node.item);
inorderOutput( node.right, output );
}
} // end inorderOutput()
static int countNodes(WordCount node)
{
// Return the number of nodes in the tree to which node points.
if (node == null)
return 0;
else
return 1 + countNodes(node.left) + countNodes(node.right);
}
static void insertWord(String newItem)
{
// Add the word to the binary sort tree to which the
// global variable "root" refers.Ignore Duplicates
if ( root == null )
{
// The tree is empty. Set root to point to a new node
// containing the new item.
root = new WordCount( newItem );
return;
}
WordCount nextplace; // Runs down the tree to find a place for newItem.
nextplace = root; // Start at the root.
while (true)
{
if ( newItem.equals(nextplace.item) )
{
count++;
// The word is already in the tree.
// increment the count variable.
return;
}
if ( newItem.compareTo(nextplace.item) < 0 )
{
// Since the new item is less than the item in nextplace,
// it belongs in the left subtree of nextplace. If there
// is an open space at nextplace.left, add a node there.
// Otherwise, advance nextplace down one level to the left.
if ( nextplace.left == null )
{
nextplace.left = new WordCount( newItem );
return; // New item has been added to the tree.
}
else
nextplace = nextplace.left;
}
else
{
// Since the new item is greater than or equal to the
// item in nextplace it belongs in the right subtree of
// nextplace. If there is an open space at nextplace.right,
// add a new node there.
if ( nextplace.right == null )
{
nextplace.right = new WordCount( newItem );
return; // New item has been added to the tree.
}
else
nextplace = nextplace.right;
}
} // end while
} // end insertWord()
} // end class WordCount
=====================================================================================
Let me know if more information is required.