Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

If there's any way you can post screenshots of the code instead of copy and pasting it as the answer, i would really appreciate that!

If there's any way you can post screenshots of the code instead of copy and pasting it as the answer, i would really appreciate that! So I need help with a C++ assignment, which I posted below. Thank you!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

7. It is advisable to do favors for people whenever possible.

Answered: 1 week ago