Question
Someone already answered this question but I need another answer, so please don't copy and paste their answer! Thank you! I need help with a
Someone already answered this question but I need another answer, so please don't copy and paste their answer! Thank you!
I need help with a code, please!
Here is a C++ class definition for an abstract data type WordTree of strings. You must store the words and the counts of the words in a single binary search tree. Each word occurring in the text can only be stored once in the tree. Implement each member function in the class below. The WordTree class may have only one member variable, root, and it must be private. You may add additional private members functions to the WordTree class. Some of the functions we may have already done in lecture, that's fine, try to do those first without looking at your notes. Remember to provide an appropriate copy constructor, destructor and assignment operator for the WordTree class as well.
#include#include using namespace std; typedef string ItemType; struct WordNode { ItemType m_data; WordNode *m_left; WordNode *m_right; // You may add additional data members and member functions in WordNode }; class WordTree { private: WordNode *root; public: // default constructor WordTree() : root(nullptr) { }; // copy constructor WordTree(const WordTree& rhs); // assignment operator const WordTree& operator=(const WordTree& rhs); // Inserts v into the WordTree void add(ItemType v); // Returns the number of distince words / nodes int distinctWords() const; // Returns the total number of words inserted, including duplicate // values int totalWords() const; // Prints the LinkedList friend ostream& operator<<(ostream &out, const WordTree& rhs); // Destroys all the dynamically allocated memory // in the tree. ~WordTree(); };
The add function enables a client to insert elements into the WordTree. If an element has already been added, repeated calls to add will not add a new WordNode. Instead the count of the occurrences of that word will increase.
WordTree t; t.add("Skyler"); t.add("Walter"); t.add("Walter"); t.add("Walter"); assert(t.distinctWords() == 2); assert(t.totalWords() == 4);
The output operator << enables a client to print elements of a WordTree. The key and the number of occurrences of the key are printed. The output should be sorted according to the key.
WordTree t; t.add("Skyler"); t.add("Walter"); t.add("Walter"); t.add("Hank"); t.add("Gus"); t.add("Walter"); t.add("Gus"); cout << t;
must write (including the order)
Gus 2 Hank 1 Skyler 1 Walter 3
Here is a simple example of overloading the output operator in a class. You must implement it as a non member friend function.
When comparing items, just use the == or != operators provided for the string type by the library. These do case-sensitive comparisons. In otherwords, the The THE will count as different words for this assignment, that's fine.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started