Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When an author produce an index for his or her book, the first step in this proc

ID: 663599 • Letter: W

Question

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.

---------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------

Lincoln.txt

---------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------

Explanation / Answer

I have written below code in C++ and has defined functions as per the requirement mentioned in the question and I have created a Binary Search Tree (BST) to store all the word object by writing an insertion or increment function.The function name is "void IncrementOrInsert(TreeNode*&, StrType) "for this .Rest of the code is well explained below:

#include <fstream>
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;

const int MAX_CHARS = 200;

enum RelationType {LESS, EQUAL, GREATER};

class StrType
{
public:

StrType();

void GetStringFile(bool skip, std::ifstream& inFile);

void PrintToFile(bool newLine, std::ofstream& outFile);

RelationType ComparedTo(const StrType& )const;
  


int LengthIs();

private:
char letters[MAX_CHARS + 1];
};

struct WordCount
{
StrType word;
int count;
};

struct TreeNode
{
WordCount info;
TreeNode * left;
TreeNode * right;
};

void PrintTree(TreeNode*, std::ofstream&);


void IncrementOrInsert(TreeNode*&, StrType);


int main()
{
using namespace std;
TreeNode* tree = NULL;
ifstream inFile;
ofstream outFile;
StrType string;
inFile.open("history.in");
outFile.open("words.dat");
string.GetStringFile(true, inFile);

while (inFile)
{
if (string.LengthIs() > 2)
IncrementOrInsert(tree, string);
string.GetStringFile(true, inFile);
}
PrintTree(tree, outFile);
return 0;
}

void IncrementOrInsert(TreeNode*& tree, StrType string)
{
if (tree == NULL)
{
tree = new TreeNode;
tree->info.word = string;
tree->info.count = 1;
tree->left = NULL;
tree->right = NULL;
}
else
{
switch (tree->info.word.ComparedTo(string))
{
case EQUAL : tree->info.count++;
break;
case GREATER : IncrementOrInsert(tree->left, string);
break;
case LESS : IncrementOrInsert(tree->right, string);
break;
default : cout << "error";
}
}
}

void PrintTree(TreeNode* tree, std::ofstream& outFile)
{
if ( !(tree == NULL))
{
PrintTree(tree->left, outFile);
tree->info.word.PrintToFile(true, outFile);
outFile << ": " << tree->info.count;
PrintTree(tree->right, outFile);
}
}

int StrType::LengthIs()
{
return std::strlen(letters);
}

void StrType::PrintToFile(bool newLine, std::ofstream& outFile)
{
if (newLine)
outFile << std::endl;
outFile << letters;
}

StrType::StrType()
{
letters[0] = '';
}


void StrType::GetStringFile(bool skip, std::ifstream& inFile)
{

using namespace std;
char letter;
int count = 0;

if (skip)
{
inFile.get(letter);
while (!isalnum(letter) && inFile)
inFile.get(letter);
}
else
inFile.get(letter);
if (!inFile || !isalnum(letter))
letters[0] = '';
else
{
do
{
letters[count] = letter;
count++;
inFile.get(letter);
} while (isalnum(letter) && inFile && (count < MAX_CHARS));
letters[count] = '';

// Skip extra characters if necessary.
if (count == MAX_CHARS)
do
{
inFile.get(letter);
} while (isalnum(letter) && inFile);

}
}


{
int comp;
comp = std::strcmp(letters, local.letters);

if( comp == 0 )

return EQUAL;

else if( comp < 0)

return    LESS;

else
return GREATER;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote