How would I remove the largest node in a Binary Search tree?
function prototype:
boolean remove_largest(node *& root)
{
}
I know that if there's no right child, then the root node has to be deleted.
If there is a right child, then traverse that path for the largest node.
But how to make this in to code that works?
bool remove_largest(node *& root)
{
if (root == nullptr) returnfalse;
node *temp = root, *prev = nullptr;
while (temp->right != nullptr) {
prev = temp;
temp = temp->right;
}
if (prev == nullptr) //didn't have a right child
root = root->left; //therefore delete root and move the pointer to left child
else prev->right = nullptr; //delete the largest
returntrue;
}