USING C++ VISUAL STUDIO COMPILER PLEASE SOLVE ASSIGNMENT FULLY FOR BEST ANSWER W
ID: 663462 • Letter: U
Question
USING C++ VISUAL STUDIO COMPILER PLEASE SOLVE ASSIGNMENT FULLY FOR BEST ANSWER
When an author produce an index for his or her book, the first step in this process is to decide which words should go into the index; the second is to produce a list of the pages where each word occurs. Instead of trying to choose words out of our heads, we decided to let the computer produce a list of all the unique words used in the manuscript and their frequency of occurrence. We could then go over the list and choose which words to put into the index.
The main object in this problem is a "word" with associated frequency. The tentative definition of "word" here is a string of alphanumeric characters between markers where markers are white space and all punctuation marks; anything non-alphanumeric stops the reading. If we skip all un-allowed characters before getting the string, we should have exactly what we want. Ignoring words of fewer than three letters will remove from consideration such as "a", "is", "to", "do", and "by" that do not belong in an index.
In this project, you are asked to write a program to read any text file and then list all the "words" in alphabetic order with their frequency together appeared in the article. The "word" is defined above and has at least three letters.
Note:
Your result should be printed to an output file named YourUserID.txt.
You need to create a Binary Search Tree (BST) to store all the word object by writing an insertion or increment function. Finally, a proper traversal print function of the BST should be able to output the required results.
The BST class in the text can not be used directly to solve this problem. It is also NOT a good idea to modify the BST class to solve this problem. Instead, the following codes are recommended to start your program.
SHOW ME MAIN FUNCTION AND THE DEFINITIONS OF ABOVE TWO FUNCTIONS
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
struct WordCount{
public:
string word;
int count;
WordCount(){}
};
struct TreeNode{
public:
WordCount* info;
TreeNode* left;
TreeNode* right;
TreeNode(){
left = NULL;
right = NULL;
}
};
struct BST{
public:
TreeNode* Node;
int i;
BST(string wd){
Node = new TreeNode();
WordCount* wc = new WordCount();
i = 0;
wc->word = wd;
wc->count = 1;
Node->info = wc;
TreeNode* Left = new TreeNode();
TreeNode* Right = new TreeNode();
Node->left = Left;
Node->right = Right;
}
bool ext(TreeNode* n){
if (n->left == NULL && n->right == NULL) return true;
return false;
}
void Insert(TreeNode* n, string s){
TreeNode* temp = n;
//cout << temp->left << " " << temp->right << endl;
while (ext(temp) == false){
if (s[0] > temp->info->word[0])
temp = temp->right;
else if (s == temp->info->word){
temp->info->count++;
return;
}
else
temp = temp->left;
}
WordCount* wc = new WordCount();
wc->word = s;
wc->count = 1;
temp->info = wc;
TreeNode* Left = new TreeNode();
TreeNode* Right = new TreeNode();
temp->left = Left;
temp->right = Right;
}
string rem(string rem){
int n = rem.length();
if (rem[n-1] == ',' || rem[n-1] == '.')
return rem.substr(0,n-1);
return rem;
}
void PrintTree(TreeNode* n,ofstream& outfile){
if (i == 0){
outfile.open("outfile.txt");
i++;
}
if (ext(n) == false){
PrintTree(n->left,outfile);
outfile << rem(n->info->word) << " " << n->info->count << endl;
PrintTree(n->right,outfile);
}
}
};
void removeInitSpaces(string& str){
int i=str.length();
int j=0;
while ((j<i) && ((str[j] == ' ') || (str[j] == ' '))){
j++;
}
str.erase(0,j);
}
int main(){
ifstream infile;
ofstream outfile;
infile.open("Lincoln.txt");
string get;
string temp;
int i = 0;
BST* bst;
while (!infile.eof()){
get = "";
getline(infile,get);
removeInitSpaces(get);
if (get.length() == 0)
continue;
stringstream line(get);
while (line >> temp){
if (temp.length() > 3){
if (i == 0){
bst = new BST(temp);
i++;
}
else{
bst->Insert(bst->Node,temp);
}
}
}
}
bst->PrintTree(bst->Node,outfile);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.