1) If right subtree of node is not NULL, then succ lies in right subtree. Do following:
Go to right subtree and return the node with minimum key value in right subtree.
2) If right sbtree of node is NULL, then succ is one of the ancestors. Do following:
Travel up using the parent pointer until you see a node which is left child of it’s parent. The parent of such a node is the succ.c
struct
node * inOrderSuccessor(
struct
node *root,
struct
node *n)
{
if
( n->right != NULL )
return
minValue(n->right);
struct
node *succ = NULL;
// Start from root and search for successor down the tree
while
(root != NULL)
{
if
(n->data < root->data)
{
succ = root;
root = root->left;
}
else
if
(n->data > root->data)
root = root->right;
else
break
;
}
return
succ;
}
Resolving technical problems:
Solve your technical problems instantly
We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM
Mail your problem details at [email protected] along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.