Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ problem The code below is the data structure of a tree that our professor built in class. #include typedef long int myint; class Frac

C++ problem

The code below is the data structure of a tree that our professor built in class.

#include

typedef long int myint; class Frac { private: myint num; myint den; public: Frac(const myint& = 0, const myint& = 1); Frac(const Frac&); void operator=(const Frac&); myint getNum() const; myint getDen() const; void setNum(const myint &); void setDen(const myint &); myint operator(const Frac &) const;

}; Frac::Frac(const myint& _num, const myint &_den) { num = _num; den = _den; if (den == 0) { num = 0; den = 1; } if (den(const Frac &_cW) const { if (num * _cW.getDen() > den * _cW.getNum()) { return 1; } return 0; }

class TNode { public: Frac value; TNode * lChild; TNode * rChild; TNode(); }; TNode::TNode() { lChild = nullptr; rChild = nullptr; } void insert(TNode*& root, const Frac& fr) { if (root == nullptr) { root = new TNode; root->value = fr; } else { if (frvalue) { insert(root->lChild, fr); } if (fr>root->value) { insert(root->rChild, fr); } } } void insert(TNode*& root, const int& b, const int&c = 1) { Frac fr(b, c); insert(root, fr); } void deleteNode(TNode*& root) { if (root != nullptr) { if (root->lChild != nullptr) { TNode *helper = root->lChild; if (helper->rChild != nullptr) { TNode *h2 = helper->rChild; while (h2->rChild != nullptr) { helper = helper->rChild; h2 = h2->rChild; } root->value = h2->value; if (h2->lChild == nullptr) { delete h2; helper->rChild = nullptr; } else { deleteNode(h2); } } else { root->value = helper->value; if (helper->lChild == nullptr) {

delete helper; root->lChild = nullptr; } else { deleteNode(helper); } }

} else { if (root->rChild != nullptr) { TNode *helper = root->rChild; if (helper->lChild != nullptr) { TNode *h2 = helper->lChild; while (h2->lChild != nullptr) { helper = helper->lChild; h2 = h2->lChild; } root->value = h2->value; if (h2->rChild == nullptr) { delete h2; helper->lChild = nullptr; } else { deleteNode(h2); } } else { root->value = helper->value; if (helper->rChild == nullptr) {

delete helper; root->rChild = nullptr; } else { deleteNode(helper); } }

} else { delete root; root = nullptr; } } } }

myint deleteValue(TNode *&root, const Frac &fr) { if (root == nullptr) { return 0; } if (frvalue) { return deleteValue(root->lChild, fr); } if (fr>root->value) { return deleteValue(root->rChild, fr); } deleteNode(root); return 1; }

void printAll(TNode * root) { if (root != nullptr) { printAll(root->lChild); std::cout value).getNum(); std::cout value).getDen(); std::cout rChild); } }

Using the data structure of a tree that we built in class, create a class SetOfFractions with the following declaration.

class SetOfFractions { private: TNode* root; public: SetOfFractions(); SetOfFractions(const SetOfFractions &); SetOfFractions(SetOfFractions &&); void operator=(const SetOfFractions &); void operator=(SetOfFractions &&); myint isElement(const Frac &) const; myint insertInS(const Frac &); void printAllFractions() const; ~SetOfFractions(); };

image text in transcribed

int main() { myint a, b, c; Frac tempFr; SetOfFractions setF; a = 1; while (a != 0) { std::cout > a; if (a == 1) { std::cout > b >> c; tempFr.setNum(b); tempFr.setDen(c); setF.insertInS(tempFr); } if (a == 2) { std::cout > b >> c; tempFr.setNum(b); tempFr.setDen(c); std::cout (a) The default constructor Setoffractions O should initialize an empty tree. The copy constructor SetofFractions(const Setof Fractions ©From) should create the set of fractions with the same elements as the set copyFrom. Make sure that the elements are not shared so if later elements are changed in copyFrom the changes do not affect the objects created using copy constructor. (b) Copy assignment operator void operator (const SetofFractions & copySet) copies the content of the set over the existing elements. Make sure you properly deallocate the memory before copying to prevent memory leak. Cc The methods Setoffractions SetofFractions && From) and void operator (Setoffractions && move move From) are move constructor and move assignment operator. Cd The method myint isElementCconst Frac &el) should return 1 ifel is an element of the set and 0 otherwise. (e) The method myint insertInSCconst Frac&fr should insert the element fr in the set, if the element is not in the set already. If it is in the set, then no insertion should be performed. The method void printAllFractionsO should print all the fractions in the set Of Og The method -SetofFractionsO should be the destructor that deallocates the memory occupied by the tree structure. After creating the described class, use the following function mainO to test the implementation of the class. (a) The default constructor Setoffractions O should initialize an empty tree. The copy constructor SetofFractions(const Setof Fractions ©From) should create the set of fractions with the same elements as the set copyFrom. Make sure that the elements are not shared so if later elements are changed in copyFrom the changes do not affect the objects created using copy constructor. (b) Copy assignment operator void operator (const SetofFractions & copySet) copies the content of the set over the existing elements. Make sure you properly deallocate the memory before copying to prevent memory leak. Cc The methods Setoffractions SetofFractions && From) and void operator (Setoffractions && move move From) are move constructor and move assignment operator. Cd The method myint isElementCconst Frac &el) should return 1 ifel is an element of the set and 0 otherwise. (e) The method myint insertInSCconst Frac&fr should insert the element fr in the set, if the element is not in the set already. If it is in the set, then no insertion should be performed. The method void printAllFractionsO should print all the fractions in the set Of Og The method -SetofFractionsO should be the destructor that deallocates the memory occupied by the tree structure. After creating the described class, use the following function mainO to test the implementation of the class

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

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

More Books

Students also viewed these Databases questions

Question

3. Give examples of four fair disciplinary practices.

Answered: 1 week ago

Question

2. Enrolling employees in courses and programs.

Answered: 1 week ago

Question

1. Communicating courses and programs to employees.

Answered: 1 week ago