//Recursively Remove data from the Tree void BinaryTree::rRemove(string s, TreeNode*& that) { //Error Handling if (that == NULL) return; //Base Case if (s == that->data) { remNode(that); return; } if (s <= that->data) { rRemove(s, that->left); return; } if (s > that->data) { rRemove(s, that->rght); return; } cout << "Recursive Delete has Failed" << endl; return; }
BinaryNode *BinarySearchTree::rRemove(int x, BinaryNode *t) //throw (ItemNotFound) { if(t == NULL) throw runtime_error("ITEM NOT FOUND"); if(x < t->key) t->left = rRemove(x, t->left); else if(x > t->key) t->right = rRemove(x, t->right); else if(t->left != NULL && t->right != NULL)// item x is found; t has two children { t->key = findMin(t->right)->key; t->right = removeMin(t->right); } else { //t has only one child BinaryNode *node = t; t = (t->left != NULL) ? t->left : t->right; delete node; } return t; }
//Remove data from the Tree void BinaryTree::remove(string s) { rRemove(s, root); return; }