Question: Can someone please help me with the numprimes() function. All of the details are listed below No documentation is required on this assignment. Start with

Can someone please help me with the numprimes() function. All of the details are listed below

No documentation is required on this assignment.

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. (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 << endl; } 

the class and implementation file are listed below:

#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; friend void insert_aux(treenode*& root, int item); friend void print_aux(treenode* root); friend size_type size_aux(treenode* root); friend int find_aux(treenode* root, int target, bool& found); friend void del_aux(treenode*& root, int target, bool& found); friend void remove_max(treenode*& root, int& max); }; #endif

//implementation

#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 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 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 find_aux(binarytree::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 size_aux(binarytree::treenode* root) { if (root == nullptr) { return 0; } else { return size_aux(root->left) + size_aux(root->right) + 1; } }

void insert_aux(binarytree::treenode*& root, int item) { if (root == nullptr) { // root = new treenode(item, nullptr, 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 print_aux(binarytree::treenode* root) { if (root != nullptr) { print_aux(root->left); cout << root->data << " "; print_aux(root->right); } }

//client file

// 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;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!