In: Computer Science
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.
/******************************************************************************
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