# algorithm – Finding inorder successor of a BST in C

I have written a function to find the successor of BST in c. It takes three arguments, i.e root pointer to the binary search tree, node value of which we have to find a successor and parent pointer (for first call it is also root pointer. It will return an integer that is the successor of the node value give.

``````/**
* find Successor of BST, complex: o(h)
* @param rootPtr root pointer of BST
* @param parent_node parent node of the subtree (root node should be passed by default)
* @param nodeValue node val to find successor
* @return succsor
*/
int find_successor_h(BSTNode * rootPtr, int data, BSTNode * parent_node) {
if (rootPtr == NULL) return data;
int rt;

if (data < rootPtr -> data)
rt = find_successor_h(rootPtr -> left_child, data, rootPtr);
else if (data > rootPtr -> data)
rt = find_successor_h(rootPtr -> right_child, data, rootPtr);
else {
if (rootPtr -> right_child != NULL)
return minBST(rootPtr -> right_child);
else if (rootPtr != parent_node)
return INT_MIN;
else
return data;
}

if (rt == INT_MIN)
return INT_MAX;

if (rt == INT_MAX)
return parent_node -> data;
}
``````