In: Computer Science
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 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)
I have additional files,
if I have the code I can try link the files
1). ANSWER :
GIVENTHAT :
Points to consider:
Code
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
// Java program to demonstrate insert operation in
binary search tree
public class BinarySearchTree {
/* Class containing left and right child of
current node and key value*/
class Node {
String key;
int count;
Node left, right;
public Node(String item)
{
key =
item;
count = 0;
left = right =
null;
}
}
// Root of BST
Node root;
// Constructor
BinarySearchTree() {
root = null;
}
// This method mainly calls
insertRec()
void insert(String key) {
key = key.toLowerCase();
root = insertRec(root, key);
}
/* A recursive function to insert a new key in BST
*/
Node insertRec(Node root, String key) {
/* If the tree is empty,
return a new node */
if (root == null) {
root = new
Node(key);
root.count =
1;
return
root;
}
/* Otherwise, recur down
the tree */
if (key.compareTo(root.key) <
0)
root.left =
insertRec(root.left, key);
else if (key.compareTo(root.key)
> 0)
root.right =
insertRec(root.right, key);
else
root.count +=
1;
/* return the
(unchanged) node pointer */
return root;
}
// This method mainly calls
InorderRec()
void inorder() {
inorderRec(root);
}
// A utility function to do inorder
traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key + " " + root.count);
inorderRec(root.right);
}
}
// Driver Program to test above
functions
public static void main(String[] args) throws
FileNotFoundException {
BinarySearchTree tree = new
BinarySearchTree();
File file = new
File("BSTData.txt");
Scanner scan = new
Scanner(file);
while(scan.hasNext()) {
String key =
scan.next();
tree.insert(key);
}
// print inorder
traversal of the BST
tree.inorder();
}
}
Sample File: BSTData.txt
Sample Output: