Question: In this exercise you'll complete a remove function for the OrderedSet class below. Your code must be identical to the code provided with the exception
In this exercise you'll complete a remove function for the OrderedSet class below. Your code must be identical to the code provided with the exception the code for remove and any helper functions you want to write. (For the case where a node has two children, use the rule that we replace the node's value by the smallest value v in the right subtree and delete the original node containing v.) You may also call the findParent function, if you want. _________________________________________________________________________ #includeusing namespace std; class Node { public: int value; Node* left, * right; Node(int v = 0, Node* l = nullptr, Node* r = nullptr) { value = v; left = l; right = r; } }; class OrderedSet { private: Node* root; // root of a BST containing items in the set int size; // size of the set static void drawTree(Node* r, int offset) { char c = r == nullptr ? '.' : '-'; for (int i = 0; i < offset; i++) cout << ' '; for (int i = 0; i < 4; i++) cout << c; if (r != nullptr) { cout << r->value << endl; offset += 4; drawTree(r->right, offset); drawTree(r->left, offset); } else cout << '.' << endl; } // If target is found, return parent of Node containing target // and set child to the node containing target. // If target is not found, return parent of location where target "should go". // In that case, child will be set to nullptr. // r is assumed to be non null. Node* findParent(Node* r, int target, Node*& child) const { if (r->value == target) // r already contains the target value { child = r; return nullptr; } if (r->value > target) { child = r->left; if (child == nullptr || child->value == target) { return r; } return findParent(r->left, target, child); } child = r->right; if (child == nullptr || child->value == target) { return r; } return findParent(r->right, target, child); } static void preorder(Node* r) { if (r == nullptr) return; cout << r->value << " "; preorder(r->left); preorder(r->right); } static void postorder(Node* r) { if (r == nullptr) return; postorder(r->left); postorder(r->right); cout << r->value << " "; } static void inorder(Node* r) { if (r == nullptr) return; inorder(r->left); cout << r->value << " "; inorder(r->right); } static Node* makeDegenerateTreeLeft(int a[], int length) { return length == 0 ? nullptr : new Node(a[length - 1], makeDegenerateTreeLeft(a, length - 1)); } static Node* makeDegenerateTreeRight(int a[], int length) { return length == 0 ? nullptr : new Node(a[0], nullptr, makeDegenerateTreeRight(a + 1, length - 1)); } static Node* makeDegenerateTreeAlternating(int a[], int length) { return length == 0 ? nullptr : length % 2 == 0 ? new Node(a[length - 1], makeDegenerateTreeAlternating(a, length - 1)) : new Node(a[0], nullptr, makeDegenerateTreeAlternating(a + 1, length - 1)); } public: static Node* makeBalancedTree(int a[], int length) { if (length == 0) return nullptr; int leftLen = length / 2, rightLen = (length - 1) / 2, mid = leftLen; return new Node(a[mid], makeBalancedTree(a, leftLen), makeBalancedTree(a + leftLen + 1, rightLen)); } OrderedSet() { root = nullptr; size = 0; } // presumes that a is sorted OrderedSet(int a[], int length, int type = 2) { root = type == -1 ? makeDegenerateTreeLeft(a, length) : type == 1 ? makeDegenerateTreeRight(a, length) : type == 0 ? makeDegenerateTreeAlternating(a, length) : makeBalancedTree(a, length); // type == 2 size = length; } void printInfo() const { cout << size << endl; levelorder(); preorder(); inorder(); postorder(); } void preorder() const { preorder(root); cout << endl; } void postorder() const { postorder(root); cout << endl; } void inorder() const { inorder(root); cout << endl; } void levelorder() const { if (root != nullptr) { queue q; q.push(root); while (!q.empty()) { Node* temp = q.front(); cout << temp->value << " "; q.pop(); if (temp->left != nullptr) q.push(temp->left); if (temp->right != nullptr) q.push(temp->right); } } cout << endl; } bool remove(int v) { // fill in code return false; // case where nothing was removed } void drawTree() const { drawTree(root, 0); } }; int main() { char operation; int value; int values[1000]; int numValues; int treeType; cin >> treeType; cin >> numValues; for (int i = 0; i < numValues; i++) values[i] = 2 * i + 1; OrderedSet o(values, numValues, treeType); int v; // o.drawTree(); // for debugging only do { cin >> v; if (v >= 0) { o.remove(v); // o.drawTree(); // for debugging only } } while (v >= 0); o.printInfo(); // o.drawTree(); // for debugging only return 0; }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
