Question

In: Computer Science

This is to done in Java: create the infrastructure for building a word cloud application. We...

This is to done in Java: create the infrastructure for building a word cloud application. We will do so by 1) Reading the content of a text file and creating a binary tree of words in that file. When a duplicate word is encountered. we simply increase the frequency count of that word in its corresponding node. In other words, the nodes in the tree have two parts. One part maintains the word, and the other maintains the frequency count. It should also be noted that words will not be case sensitive, hence three variations of the word (hello, Hello, HELLO), should be stored in the same node in the tree, and it should have a frequency of 3. 2) Once the binary tree of words (including their frequency) has been created, we need to print the words and their frequency count (one word per line). (Use InOrder() traversal to display the words and their frequency count) The data to be read is provided by the instructor in a text file and we are supposed to use FileReader to read character by charcter and use delimiters like spaces,endofline, tab, etc., to figure out when the words begin and end, and store the words in the binary tree nodes. No duplicate nodes, instead increment frequency counter in each node. I have a basic grasp of how this should work in theory but I can't quite figure it out in code.

Solutions

Expert Solution

/******************************************************************************

Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
// required libraries
import java.io.*;
import java.util.*;
// tree node class
class Node{
public String data;
public int frequency;
public Node left;
public Node right;
// constructor
public Node(String data){
this.data=data;
this.frequency=1;
this.left=null;
this.right=null;
}
// inert method
public Node insert(Node root,Node curr){
if(root==null){
return curr;
}else{
Node temp = root;
insertHelper(temp,curr);
}
return root;
}
// insertHelper method
public void insertHelper(Node temp,Node curr){
if(temp.data.compareTo(curr.data)>0){
if(temp.left==null){
temp.left=curr;
}else{
insertHelper(temp.left,curr);
}
}else if(temp.data.compareTo(curr.data)<0){
if(temp.right==null){
temp.right=curr;
}else{
insertHelper(temp.right,curr);
}
}else{
temp.frequency+=1;
}
}
  
}

public class Main
{
// inorder traversal of tree
// left->root->right
public static void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.data+" frequency: "+root.frequency);
inorderRec(root.right);
}
}
// driver method
   public static void main(String[] args) throws IOException{
int ch;
FileReader fr=null;
// ArrayList to store each word
ArrayList<String> hash_Set = new ArrayList<String>();
// reading data
try{
fr = new FileReader("hello.txt");
}catch(FileNotFoundException fe){
System.out.println("File not found");
}
// adding to arraylist
String op="";
while ((ch=fr.read())!=-1){
if(!String.valueOf((char)ch).equals(" ")){
op+=String.valueOf((char)ch);
}else{
hash_Set.add(op);
op="";
}
}
// iterating through each word
Iterator<String> itr = hash_Set.iterator();
Node root=null;
// creating the tree
while (itr.hasNext()){
String data = itr.next();
data = data.toLowerCase();
Node node = new Node(data);
root = node.insert(root,node);
}
// callinf the tree node
inorderRec(root);
// close the file
fr.close();
   }
}

ouput:

a frequency: 2
and frequency: 4
are frequency: 1
be frequency: 2
been frequency: 1
binary frequency: 1
by frequency: 2
character frequency: 1
count frequency: 2
created frequency: 1
data frequency: 1
display frequency: 1
file frequency: 1
filereader frequency: 1
frequency frequency: 4
has frequency: 1
have frequency: 1
hello frequency: 3
in frequency: 3
including frequency: 1
inorder frequency: 1
instructor frequency: 1
is frequency: 1
it frequency: 1
line frequency: 1
need frequency: 1
node frequency: 1
of frequency: 2
once frequency: 1
one frequency: 1
per frequency: 1
print frequency: 1
provided frequency: 1
read frequency: 2
same frequency: 1
should frequency: 2
stored frequency: 1
supposed frequency: 1
text frequency: 1
the frequency: 7
their frequency: 3
to frequency: 5
traversal frequency: 1
tree frequency: 2
use frequency: 2
we frequency: 2
word frequency: 1
words frequency: 3

HELLO.TXT


Related Solutions

Create the infrastructure for building a word cloud application by (1) Reading the content of a...
Create the infrastructure for building a word cloud application by (1) Reading the content of a text file and creating a binary tree of words in that file. When a duplicate word is encountered. we simply increase the frequency count of that word in its corresponding node. In other words, the nodes in the tree have two parts. One part maintains the word, and the other maintains the frequency count. It should also be noted that words will not be...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
What are the three disadvantages of cloud infrastructure?
What are the three disadvantages of cloud infrastructure?
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
Create a Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters.
JAVACreate a Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0.
Create a PowersTable application that displays a table of of powers. ( Java Programing )
Create a PowersTable application that displays a table of of powers. ( Java Programing )
Create an application that uses a constructor and two different methods. JAVA
Create an application that uses a constructor and two different methods. JAVA
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT