I have the following binary search tree, with root node 20. The question I'm trying to answer is, if we apply the function t = deleteRoot(t)
, what will be the value of the new root node, as well as its immediate left and right children (eg the current root node is 20 with immediate left child 11 and immediate right child 32). I scribbled at least 10 pages over the last 2 hours trying to solve this, but the recursion is killing me. Can someone please help me visualize this - ie some way of thinking which allows me to deal with recursion. I'm not very good at visualizing how recursion works but I can somewhat implement it. Thanks very much. By the way, the answer is root node: 21 with left child 11 and right child 32.
My attempt:
The question tells us to start with t=deleteRoot(t)
.
parent = node containing 25
succ = node containing 21
succval = 21
then we get to
t = TreeDelete(t, succval)
then I get a bit lost as to how the TreeDelete function works
t->left = TreeDelete(t->left, key) ...etc
deleteRoot(Tree t) function
// delete root of tree
Tree deleteRoot(Tree t)
{
Link newRoot;
// if no subtrees, tree empty after delete
if (t->left == NULL && t->right == NULL) {
free(t);
return NULL;
}
// if only right subtree, make it the new root
else if (t->left == NULL && t->right != NULL) {
newRoot = t->right;
free(t);
return newRoot;
}
// if only left subtree, make it the new root
else if (t->left != NULL && t->right == NULL) {
newRoot = t->left;
free(t);
return newRoot;
}
// else (t->left != NULL && t->right != NULL)
// so has two subtrees
// - find inorder successor (grab value)
// - delete inorder successor node
// - move its value to root
Link parent = t;
Link succ = t->right; // not null!
while (succ->left != NULL) {
parent = succ;
succ = succ->left;
}
int succVal = succ->value;
t = TreeDelete(t,succVal);
t->value = succVal;
return t;
}
Tree TreeDelete(Tree t, Key k) function:
Tree TreeDelete(Tree t, Key k)
{
Tree deleteRoot(Tree);
if (t == NULL)
return NULL;
int diff = cmp(k,key(t->value));
if (diff == 0)
t = deleteRoot(t);
else if (diff < 0)
t->left = TreeDelete(t->left, k);
else if (diff > 0)
t->right = TreeDelete(t->right, k);
return t;
}
By the way, the answer is root node: 21 with left child 11 and right child 32.
Well, it is one solution but not the only solution.
18 with left child 11 and right child 32 will also be a solution.
When deleting the root, the new root can be selected as
or
So copy the value of the selected node to the current root. And then delete the selected node. If the selected node has children, you can repeat the process recursively.
In the case of deletion, if the node to be deleted has two children:
The successor is the highest element on the right subtree while the predecessor is the lowest element on the left subtree. In your case, the successor would be 21 and the predecessor would be 18.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.