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]...

  • //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...

  • (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...

  • Using the diagram below and the starter code, write Document Processor that will read a file...

    Using the diagram below and the starter code, write Document Processor that will read a file and let the user determine how many words of a certain length there are and get a count of all of the word lengths in the file. Your program should do the following: The constructor should accept the filename to read (word.txt) and then fill the words ArrayList up with the words from the file. The countWords method should accept an integer that represents...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Write a function that, given a word and a set of characters, filters all occurrences of...

    Write a function that, given a word and a set of characters, filters all occurrences of those characters from the word. For e.g. remove_chars("abcdeeeeefmnop", "ebo") returns: acdfmnp i.e all occurrences of e, b and o have been removed from abcdeeeeefmnop You should acquire the word and set of characters both from the user and then pass them to the function. Note: std::cin terminates when it encounters a space, you should take the word and character set as separate inputs: std::cin...

  • Problem 1) (15 points) File Search You are given a file of integers named MyData.dat. We...

    Problem 1) (15 points) File Search You are given a file of integers named MyData.dat. We would like to find the number of integers that are greater than or equal to a given value. Write a C program that searches the file for integers that are greater than or equal to the integer entered by the user. Your main function should call a C function that receives as input a file pointer and a target integer, and returns the number...

  • C++, program that use the built in string class, its constructors, some of its methods and...

    C++, program that use the built in string class, its constructors, some of its methods and operators. It reads an input file of English text, one word at a time. The file should consist of about 500 words. Keep only the words, removing any punctuation or other characters. .Store the words in a built-in STL container, such as vector or map.,Choose a list of about 10 words, for example, {she,, and, he, or, the}., Remove these words from the list.,,...

  • Creat a C Program that Reads words from a file called words, which contains one word...

    Creat a C Program that Reads words from a file called words, which contains one word per line.Each word has maximum length to M.The number of words in the file is equal to N. Incert each word to Binary Search Tree with node name dictionary.Each node of the tree must contain one word.The left child must contain a word that it is lexicographically smaller while the right child contains a lexicographically bigger one. 1) When you finish reading the file,...

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
ADVERTISEMENT