Question: Create a program called AnalyzeText.java that computes and prints some statistics on the words in the file data/donut.txt . Specifically: Print the word that occurs
Create a program called AnalyzeText.java that computes and prints some statistics on the words in the file data/donut.txt. Specifically:
- Print the word that occurs most often in the file
- Print a count of the words that occur exactly once
- Compute and print the average length of a word
Use one of the tree implementations for storing words and their counts as they read in from the input text file. Use one of BST or BSTEssential.
Do not use try-catch method, Scanner, BufferedReader, or StringBuilder. Do something like this. No complex code.
StdIn.fromFile("data/donut.txt");
String[] words = StdIn.readAllStrings();
The files:
If you want to see how the BST it is here under section 3.3 .
https://algs4.cs.princeton.edu/code/
BSTEssential looks like this:
package myalgs4;
import java.awt.Font;
import algs4.Queue; import algs4.StdDraw;
/* * This is a simplified version of the BST class * */
public class BSTEssential
private Node
private static class Node
public Node(Key key, Value val) { this.key = key; this.val = val; } }
public BSTEssential() { this.root = null; this.size = 0; }
/* ********************************************************************* * Return the number of key-values pairs in the BST. ***********************************************************************/ public int size() { return this.size; } /* ********************************************************************* * Does there exist a key-value pair with given key? ***********************************************************************/ public boolean contains(Key key) { return get(key) != null; }
/* ********************************************************************* * Search BST for given key, and return associated value if found, * return null if not found (recursive version) ***********************************************************************/
public Value get(Key key) { return get(root, key); }
private Value get(Node
private Node
/* ********************************************************************* * Delete a key-value pair, given the key. ***********************************************************************/
public void delete(Key key) { root = delete(root, key); }
private Node
// Determine how many children x has. if (x.right == null && x.left == null){ // This is a leaf node. Change its // reference from its parent to null. return null; } else if (x.right == null) { // One child, to the left. Make that // the new child of x's parent. return x.left; } else if (x.left == null) { // One child, to the right. Make that // the new child of x's parent. return x.right; } else { // Node x has two children. // Find the node in x's left subtree with // the maximum key. Node
private Node
public Iterable
// This is an in-order traversal: visit left, visit x, visit right // Pre-order: visit x, visit left, visit right // Post-order: visit left, visit right, visit x private void inOrder(Node
}
public int height() { return height(root); }
private int height(Node
/* *************************************************************************** * Visualization *****************************************************************************/
public void drawTree() { if (root != null) { StdDraw.setPenColor (StdDraw.BLACK); StdDraw.setCanvasSize(1500,1000); drawTree(root, .5, 0.95, .35, 0); } } private void drawTree (Node
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
