Question

Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp

Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts.

Project description:

The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order in the tree. The program should ignore the case of the words, so that “Branch” and “branch” are considered the same. However, words that are actually spelled differently – such as “tree” and “trees” – are considered to be different, and should be stored in separate nodes. All words will be stored in the tree in their lowercase form. Help is provided on how to accomplish this.

Two forms of queries are to be supported by the tree class:

A query for an individual word should return the number of occurrences of that word in the input file, as retrieved from searching the tree, or should display a message stating that the word was not found in the tree.

A query for words that meet or exceed a threshold number of occurrences, should result in the output of the words (and their counts) that meet that criteria, using inorder tree traversal.

The nodes for the word tree should be a struct with the following members

A string containing the word itself.

One pointer each for the left and right subtrees.

An int containing the count of the number of occurrences of the word.

Requirements:

Your program must be split into 3 files. There will be a class (with the separate interface and implementation files), and a driver file. The requirements for these are specified below:

file must be named WordTree.h and WordTree.cpp

The WordTree class – This class represents a word binary search tree

Class must be named WordTree

You should implement the following member functions in the class

A constructor – creates an empty tree

A destructor – recursive function that explicitly releases all nodes allocated during program execution. You may not rely no program termination to release memory.

insert – Recursive function that adds a word to the tree, if it is not found, or increments its count if it is already in the tree.

findNode – accepts a string argument (a word) and searches for the word in the tree. This function should be public, but should call a private function, so as to not expose the tree’s root, its implementation, etc. If found, outputs the word and its count. Otherwise displays a message stating that the word was not found.

printInOrder – Recursive function that accepts a single integer argument (a threshold value), and traverses that tree in order, outputting the words (and their counts) that meet or exceed the threshold count. The function should also output the number of nodes meeting the criteria (see sample output).

2. A driver, or client, file that

Must be named proj6.cpp

Instantiates the word tree object

Opens and reads the text file named input.txt and builds the word tree from the file by invoking the insert function described above. The input files should contain a single line of alphabetic characters (no numbers or punctuation). It should be split using a single space character via the provided function or one you write. Care should be taken to ensure that multiple sequential spaces and trailing spaces are not present in the file/line to be read. Example file: “This is the whole file and is stored and read as a single line into a string”

IF YOU NEED A HELP USE THIS

-------------------- code to transform string to lowercase
 
//transforms a string to lowercase - must include <algorithm>
// text is a string
std::transform(text.begin(), text.end(), text.begin(), ::tolower);


-------------------- function to split a string with a delimiter and load individual tokens into a string vector

// From book "C++ Cookbook" by Jeff Cogswell, Jonathan Turkanis, Christopher Diggins, D. Ryan Stephens
// Splits a string s, using character c as a delimeter, and places individual words into a vector of strings
void split(const std::string& s, char c, std::vector<std::string>& v) {

   std::string::size_type i = 0;
   std::string::size_type j = s.find(c);

   while (j != std::string::npos) {
      v.push_back(s.substr(i, j-i));
      i = ++j;
      j = s.find(c, j);

      if (j == std::string::npos)
         v.push_back(s.substr(i, s.length()));
   }
}

Invokes queries for individual words, or for words whose counts meet or exceed a threshold number of occurrences, as shown in the sample output below.

ord tree ilt an oade Finding all words with 4 or more occurrences: a(20 and 16 are<5) be K11) class 6 count4 counts (4 file <6 orc?) found 4 function<5> in(13 meet 4> nust 4> named 5 nodes 4) ot<4) number(5> occurrences(4) of <12) or(7 program<5) should 9> that 13 the (45 threshold<4> to 5 tree <17 word14) ords (10 30 nodes had words with 4 or more occurrence<s> Searchin for occurrences of the word query The word query occurs 2 time> in the text. Searching for occurrences of the word stack

TESTING FILE:

Project description The program should open and read input file named input.txt in turn building up a binary search tree of words and counts as it progresses The words will be stored in alphabetical order in the tree The program should ignore the case of the words so that Branch and branch are considered the same However words that are actually spelled differently such as tree and trees are considered to be different and should be stored in separate nodes All words will be stored in the tree in their lowercase form Two forms of queries are to be supported by the tree class A query for an individual word should return the number of occurrences of that word in the input file as retrieved from searching the tree or should display a message stating that the word was not found in the tree A query for words that meet or exceed a threshold number of occurrences should result in the output of the words and their counts that meet that criteria using inorder tree traversal The nodes for the word tree should be a struct with the following members One pointer each to the left and right subtrees An int containing the count of the number of occurrences of the word A string containing the word itself Requirements Your program must be split into files There will be a class with separate interface and implementation files and a driver file The requirements for these are specified below The WordTree class This class represents a Word Binary Tree Files must be named WordTree.h and WordTree.cpp Class must be named WordTree You should implement the following member functions in the class A constructor Creates an empty tree A destructor Recursive function that explicitly releases all nodes allocated during program execution You may not rely on program termination to release memory insert Recursive function that adds a word to the tree if it is not found or increments its count if it is already in the tree findNode accepts a string argument a word and searches for the word in the tree If found outputs the word and its count Otherwise displays a message stating that the word was not found printInOrder Recursive function that accepts a single integer argument a threshold value and traverses the tree in order outputting the words and their counts that meet or exceed the threshold count The function should also output the number of nodes meeting the criteria A driver or client file that Must be named proj6.cpp Declares the word tree object Opens and reads the text file named input.txt and builds the word tree from the file by invoking the insert function described above Invokes queries for individual words or for words whose counts meet or exceed a threshold number of occurrences

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

struct TreeNode {

std::string word;
int test;

TreeNode* left;
TreeNode* right;

TreeNode(const std::string& word)
: word(word), test(0), left(nullptr), right(nullptr) { }
};

class Tree {

public:

Tree();
Tree(const std::string& word);

void add(const std::string& word);
void traverse();
int nodecount() const;

~Tree();

private:

TreeNode* add_recursive(TreeNode* n, const std::string& word);
void function1(TreeNode* n);
void function2(TreeNode* n);

TreeNode * root;
}
Tree::Tree()
: root(nullptr) { }

Tree::Tree(const std::string& word)
: root(new TreeNode(word)) { }

void Tree::add(const std::string& word) {
root = add_recursive(root, word);
}

void Tree::traverse() {
function1(root);
}

int Tree::nodecount() const {
return root->test + 1;
}

Tree::~Tree() {
function2(root);
}

TreeNode* Tree::add_recursive(TreeNode* n, const std::string& word) {
if (n == nullptr) {
n = new TreeNode(word);
} else if (word < n->word) {
n->left = add_recursive(n->left, word);
n->test++;
} else if (word > n->word) {
n->right = add_recursive(n->right, word);
n->test++;
} else {
n->word = word;
}

return n;
}

void Tree::function1(TreeNode* n) {
if (n == nullptr) {
return;
}
std::cout << "Node => " << n->word << ", test: " << n->test;
if (n->left != nullptr) {
std::cout << ", left: " << n->left->word;
}
if (n->right != nullptr) {
std::cout << ", right: " << n->right->word;
}
std::cout << "\n";

function1(n->left);
function1(n->right);
}

void Tree::function2(TreeNode* n) {
if (n == nullptr) {
return;
}
function2(n->left);
function2(n->right);
delete n;
}
int main()
{
Tree tree;

tree.add("hello");
tree.add("C++ ");
tree.add("world");
tree.add("foo ");
tree.add("bar ");
tree.add("baz ");

tree.traverse();

std::cout << "num nodes: " << tree.nodecount() << std::end;
}

Add a comment
Know the answer?
Add Answer to:
Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :)...

    C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :) Use FILE I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each group of adjacent vowels (a, e, i, o, u,...

  • C++ Write a program that reads from the external file input.txt, counts the letters in every...

    C++ Write a program that reads from the external file input.txt, counts the letters in every word, replaces the word by that number, and then writes the numbers to an external file output.txt (Note: Do not forget to copy the blanks. You may wish to use infile.get and outfile.put in your program.) Also you may wish to use the strlen( ) function. Output: After the program generates output.txt, the code should display the contents of the file on the screen...

  • Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

    Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Please Only use C language In this assignment, you have to read a text file (in.txt)...

    Please Only use C language In this assignment, you have to read a text file (in.txt) that contains a set of words. The first line of the file contains 3 numbers (N, S, D). These numbers represent the sequence of input words in your file. N: represents the number of words to read to build a binary search tree. You have to write a recursive insert code to create and insert these words into the binary search tree. After inserting...

  • Hello! i need a program in C that uses FILE*! The instructions are below! Please dont...

    Hello! i need a program in C that uses FILE*! The instructions are below! Please dont copy paste another chegg post! Thank you! Will leave positive review! I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Write a Python program stored in a file q6.py that takes a text (without punctuation) as...

    Write a Python program stored in a file q6.py that takes a text (without punctuation) as input and prints the number of occurrences of each word. Your program is case-insensitive, meaning that uppercase and lowercase of the same letter is considered the same. Printing should be done in decreasing order of the number of occurrences. If several words have the same number of occurrences, they should be printed in alphabetical order. A new line must be printed between the words...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT