Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a remove method to our OrderedSet class. bool remove (int target); // remove the node with target in it, if found, and return true

Implement a remove method to our OrderedSet class.

bool remove (int target);

// remove the node with target in it, if found, and return true (to signify that the removal succeeded). If target is not found, just return false.

// findParent is useful here, since in some cases we can just delete the child (the one that contains target) and update a pointer.

// Unfortunately, there is a special case where the node containing target has two children, so we can't just splice it out.

// Instead identify the parent of the node n with the smallest value v in the right subtree of the node containing the target.

// Then overwrite the value we want to remove with v and remove n. To remove n, update n's parent p so p->left = n->right.

// Also, avoid memory leak be deleting n after saving it's address to a temp.

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.   _________________________________________________________________________ #include  using 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

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_2

Step: 3

blur-text-image_3

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

Database Theory And Application Bio Science And Bio Technology International Conferences DTA And BSBT 2011 Held As Part Of The Future Generation In Computer And Information Science 258

Authors: Tai-hoon Kim ,Hojjat Adeli ,Alfredo Cuzzocrea ,Tughrul Arslan ,Yanchun Zhang ,Jianhua Ma ,Kyo-il Chung ,Siti Mariyam ,Xiaofeng Song

2011th Edition

3642271561, 978-3642271564

More Books

Students also viewed these Databases questions

Question

Why do sediments in rift and pull-apart basins lack maturity?

Answered: 1 week ago

Question

Explain the factors affecting dividend policy in detail.

Answered: 1 week ago

Question

Explain walter's model of dividend policy.

Answered: 1 week ago