Removing largest item in BST tree?

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?
You just described what to do, where are you stuck?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool remove_largest(node *& root)
{
	if (root == nullptr) return false;
	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
	return true;
}


http://en.cppreference.com/w/cpp/language/nullptr
Last edited on
Topic archived. No new replies allowed.