# Inorder Successor in Binary Search Tree

This is the problem that I have to solve:

given a node in a binary search tree, find the node with the smallest key that is greater than the key of the given node..

The algorithm that is often given is this one:

bst_successor(x)
{
if(x == NULL)
return NULL;
if(x.right != NULL)
return abr_min(x.right)
y = x.p;
while(y != NULL && x = y.right)
{
x = y;
y = y.p;
}
return y;
}


(you can find further explanations of the problem here.)

Now, I was wondering if this can be another solution:

bst_successor(x)
{
if(x == NULL)
return NULL;
if(x.right != NULL)
return abr_min(x.right)
y = x.p;
while(y != NULL && y.key < x.key)
{
y = y.p;
}
return y;
}

• Your answer is missing a lot of context. What is the successor function trying to accomplish? What data structure is in the background? May 4 '20 at 21:05
• (Answer should have been question in my preceding comment.) May 4 '20 at 21:48
• This is the problem I have to solve: "given a node in a binary search tree, find the node with the smallest key that is greater than the key of the given node.". [Here][1], there is another explanation of my problem [1]: geeksforgeeks.org/inorder-successor-in-binary-search-tree May 5 '20 at 8:13
• Don't answer in the comments. Instead, update your question to include all necessary information. Don't add an "EDIT" section. Instead, just rewrite your question so that it looks whole, but now contains all the requisite information. May 5 '20 at 8:15