Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, If I could get some help on this question, and maybe some comments showing what was done and why that'd be great: Start with

Hello,

If I could get some help on this question, and maybe some comments showing what was done and why that'd be great:

Start with your binaryTree class from the last assignment and add the big-3 to the class. First add private static functions named "copy()" and "clear()". Because these functions are being called from within the class (instead of being called by the client), you won't need separate recursive "copy_aux()" and "clear_aux()" functions. Instead, "copy()" and "clear()" will themselves be recursive. If you write these two functions correctly, your big-3 will be trivial. "copy()" should be a static void function with two treenode* parameters; it assigns a deep copy of the second parameter to the first parameter. It will be called from your copy constructor and from your assignment operator. "clear()" should be a static void function that deallocates the entire binary tree pointed to by its single treenode* argument. It will be called from your destructor and from your assignment operator.

---------------------------------SYNTAX------------------------------------------------------------------

binarytree.h

#ifndef BINARYTREE_H #define BINARYTREE_H #include "LL.cpp"

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); int numPrimes() const; LL toLL(); static int mSize; 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); static int numPrimes_aux(const treenode* root, int& max); static bool checkPrimeNumber(int n); static void toLL_aux(const treenode* root, LL& ll); }; #endif

***********************************************

binarytree.cpp

#include #include "binarytree.h" using namespace std;

int binarytree::mSize = 0; binarytree::binarytree() { root = nullptr; mSize = 0; }

void binarytree::print() const { print_aux(root); }

void binarytree::insert(int item) { insert_aux(root, item); }

binarytree::size_type binarytree::size() const { return mSize; }

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; mSize--; } else { int max; remove_max(root -> left, max); root -> data = max; found = true; mSize--; } }

// 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; mSize++; } 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); } }

int binarytree::numPrimes() const { int max = 0; numPrimes_aux(root, max); return max; }

int binarytree::numPrimes_aux(const treenode* root, int& max) { if (root != nullptr) { numPrimes_aux(root -> left, max); if(!checkPrimeNumber(root -> data)) max++; numPrimes_aux(root -> right, max); } }

bool binarytree::checkPrimeNumber(int n) { bool flag = false; if(n == 2) return true; for(int i = 2; i < n; i++) { if(n%i == 0) { flag = true; break; } } return flag; }

LL binarytree::toLL() { LL ll; toLL_aux(root, ll); return ll; }

void binarytree::toLL_aux(const treenode* root, LL& ll) { if (root != nullptr) { toLL_aux(root -> left, ll); ll.push_back(root -> data); toLL_aux(root -> right, ll); } }

***********************************************

LL.cpp

#include using namespace std;

template class LL { class Node; public: LL() noexcept { m_spRoot = nullptr; } class Iterator; Iterator begin() { return Iterator(m_spRoot); } Iterator end() { return Iterator(nullptr); } void push_back(T data); void Traverse(); class Iterator { public: Iterator() noexcept : m_pCurrentNode (m_spRoot) { } Iterator(const Node* pNode) noexcept : m_pCurrentNode (pNode) { } Iterator& operator=(Node* pNode) { this->m_pCurrentNode = pNode; return *this; } Iterator& operator++() { if (m_pCurrentNode) m_pCurrentNode = m_pCurrentNode->pNext; return *this; } Iterator operator++(int) { Iterator iterator = *this; ++*this; return iterator; } bool operator!=(const Iterator& iterator) { return m_pCurrentNode != iterator.m_pCurrentNode; } int operator*() { return m_pCurrentNode->data; } private: const Node* m_pCurrentNode; }; private: class Node { T data; Node* pNext; friend class LL; }; Node* GetNode(T data) { Node* pNewNode = new Node; pNewNode->data = data; pNewNode->pNext = nullptr; return pNewNode; } Node*& GetRootNode() { return m_spRoot; } static Node* m_spRoot; }; template /*static*/ typename LL::Node* LL::m_spRoot = nullptr; template void LL::push_back(T data) { Node* pTemp = GetNode(data); if (!GetRootNode()) { GetRootNode() = pTemp; } else { Node* pCrawler = GetRootNode(); while (pCrawler->pNext) { pCrawler = pCrawler->pNext; } pCrawler->pNext = pTemp; } } template void LL::Traverse() { Node* pCrawler = GetRootNode(); while (pCrawler) { cout << pCrawler->data << " "; pCrawler = pCrawler->pNext; } cout << endl; }

****************************************

sample.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 << "Number of prime numbers in tree are " << list.numPrimes() << 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

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

4. What are the current trends in computer software platforms?

Answered: 1 week ago