In C++, create a class to hold a set of strings called setTree. setTrees contain copies...

70.2K

Verified Solution

Question

Programming

In C++, create a class to hold a set of strings called setTree.setTrees contain copies of the strings inserted so that the stringscannot be changed due to the fact that changing the strings insidethe set could break the binary search tree. The strings are casesensitive.

TreeSet implements the following:

bool add(const string& s) -- add s to the set, if it's notalready there. Return true if the set changed, false otherwise.

void clear() -- remove all elements from the set

bool contains(const string& s) const -- test whether or nots is in the set.

bool isEmpty() const -- test whether or not the set is empty

bool remove(const string& s) -- remove s from the set, if itwas there. Return true if the set changed, false otherwise.

int size() const -- return the number of strings in set.

int toArray(string sa[]) const -- put the elements of the setinto the array sa. Assume that sa has sufficient room (it's theclient's responsibility to check). You can place the strings in anyorder you choose.

The class also includes a zero-argument constructor to create anempty set, a constructor that takes in an array of strings and itssize (creating a set containing each element of the array), copyconstructor, assignment operator, and destructor. The copyconstructor and assignment operator make deep copies.

setTree implements the set using a binary search tree. The treedoes not need to be balanced. The implementation of the tree nodeclass is hidden from the client. There are no memory leaks.

Answer & Explanation Solved by verified expert
3.7 Ratings (551 Votes)
Classes TreeSetNode It represents the node inside the Binary Search Tree BST class TreeSetNode public string s TreeSetNode left left child of the BST TreeSetNode right right child of the BST TreeSetNodeconst string s s s left nullptr right nullptr class TreeSet public head points to the root of the BST TreeSetNode head size represents the current number of nodes of the BST int size TreeSet head nullptr size 0 TreeSetstring s int size head nullptr forint i 0 i size i addsi size size TreeSetTreeSet sTree size sTreesize head buildTreesTreehead TreeSet clear TreeSetNode buildTreeTreeSetNode toCopyFrom iftoCopyFrom nullptr return nullptr TreeSetNode root new TreeSetNodetoCopyFroms rootleft buildTreetoCopyFromleft rootright    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students