Hi, I need help with a C++ assignment. The final code should be in two files- binarytree.h & binarytree.cpp
Start with the binaryTree class provided in lesson 23 and make the following changes. Don't make any changes other than the additions listed here. Do not add the big-3 yet. We'll work on that next week. (Adding private helper functions is also ok.)
mSize: Add a data member to store the size of the tree. Call it "mSize" to distinguish it from the "size()" function. You'll need to update this new data member in all of the appropriate places in the class implementation. Change the size() function so that it returns this data member instead of calculating the size.
numPrimes(): Add a "numPrimes()" function to the class that returns the number of nodes in the tree that contain prime integers. Good decomposition dictates that you will want a helper function that determines whether its parameter is prime. Don't worry about making this efficient. Just test all of the numbers less than the parameter and if any of them divide evenly into the parameter, then it's not prime.
toLL(): Add a "toLL()" function to the class that converts the calling binary tree object into an LL object. The items in the LL object should be in increasing order. The created LL object should be returned. Here's a sample client to illustrate how this function might be used:
#include #include "LL.h" #include "binarytree.h" using namespace std; int main() { binarytree t; for (int i = 0; i < 20; i++) { t.insert(rand() % 50); } cout << "The binary tree: "; t.print(); cout << endl; LL l; l = t.toLL(); cout << "The linked list: "; for (LL::iterator i = l.begin(); i != l.end(); i++) { cout << *i << " "; } cout << endl; cout << "The original binary tree still intact: "; t.print(); cout << endl; }
Here is the toLL_aux() function comment copied from the solution, which I hope will serve as a hint for you:
// Calls linkedList.push_front(). With push_front(), the last item pushed becomes the // first item in list, so traverse tree from greatest value to lowest value (right // subtree before left), in order for list to be in ascending order.
binaryTree class from lesson 23:
// Here is the file binarytree.h #ifndef BINARYTREE_H #define BINARYTREE_H #include // for size_t class binarytree { public: typedef std::size_t size_type; binarytree(); void insert(int item); void print() const; size_type size() const; int find(int target, bool& found) const; void del(int target, bool& found); private: struct treenode { int data; treenode* left; treenode* right; }; treenode* root; static void insert_aux(treenode*& root, int item); static void print_aux(const treenode* root); static size_type size_aux(const treenode* root); static int find_aux(const treenode* root, int target, bool& found); static void del_aux(treenode*& root, int target, bool& found); static void remove_max(treenode*& root, int& max); }; #endif
// Here is the file binarytree.cpp #include #include "binarytree.h" using namespace std; binarytree::binarytree() { root = nullptr; } void binarytree::print() const { print_aux(root); } void binarytree::insert(int item) { insert_aux(root, item); } binarytree::size_type binarytree::size() const { return size_aux(root); } int binarytree::find(int target, bool& found) const { return find_aux(root, target, found); } void binarytree::del(int target, bool& found) { del_aux(root, target, found); } void binarytree::del_aux(binarytree::treenode*& root, int target, bool& found) { if (root == nullptr) { found = false; } else if (target < root -> data) { del_aux(root -> left, target, found); } else if (target > root -> data) { del_aux(root -> right, target, found); } else if (root -> left == nullptr) { binarytree::treenode* tempptr = root; root = root -> right; delete tempptr; found = true; } else { int max; remove_max(root -> left, max); root -> data = max; found = true; } } // pre: root != nullptr void binarytree::remove_max(binarytree::treenode*& root, int& max) { if (root -> right == nullptr) { max = root -> data; binarytree::treenode* tempptr = root; root = root -> left; delete tempptr; } else { remove_max(root -> right, max); } } int binarytree::find_aux(const treenode* root, int target, bool& found) { if (root == nullptr) { found = false; return 0; } else if (target == root -> data) { found = true; return root -> data; } else if (target < root -> data) { return find_aux(root -> left, target, found); } else { return find_aux(root -> right, target, found); } } binarytree::size_type binarytree::size_aux(const treenode* root){ if (root == nullptr) { return 0; } else { return size_aux(root -> left) + size_aux(root -> right) + 1; } } void binarytree::insert_aux(treenode*& root, int item) { if (root == nullptr) { root = new binarytree::treenode; root -> data = item; root -> left = nullptr; root -> right = nullptr; } else if (item <= root -> data) { insert_aux(root -> left, item); } else { insert_aux(root -> right, item); } } void binarytree::print_aux(const treenode* root) { if (root != nullptr) { print_aux(root -> left); cout << root -> data << " "; print_aux(root -> right); } } // Here is the file binarytree-client.cpp #include #include "binarytree.h" using namespace std; int main() { binarytree list; int num; bool found; cout << "enter number to insert (negative to quit): "; cin >> num; while (num >= 0){ list.insert(num); cout << "enter number to insert (negative to quit): "; cin >> num; } list.print(); cout << endl; binarytree::size_type numItems; numItems = list.size(); cout << "There are " << numItems << " items." << endl; cout << "enter a number to find (negative to quit): "; cin >> num; while (num >= 0) { int result = list.find(num, found); if (!found) { cout << "not found" << endl; } else { cout << "found. The data is " << result << endl; } cout << "enter a number to find (negative to quit): "; cin >> num; } cout << "enter a number to delete (negative to quit): "; cin >> num; while (num >= 0) { list.del(num, found); if (found) { cout << "the list is now "; list.print(); cout << endl; } else { cout << num << " is not in the list." << endl; } cout << "enter a number to delete (negative to quit): "; cin >> num; } binarytree list2(list); cout << "Now list 2 should be a copy of list. Here it is: "; list2.print(); cout << endl; list2.del(3, found); cout << "After deleting a 3 from list2, list2 is now: "; list2.print(); cout << endl << "list should be unchanged. Here it is: "; list.print(); cout << endl; list = list2; cout << "Now list has been assigned list2 so it should match list2. Here it is: "; list.print(); cout << endl; list.del(4, found); cout << "After deleting a 4 from list, list is now: "; list.print(); cout << endl << "list2 should be unchanged. Here it is: "; list2.print(); cout << endl;