Created
August 1, 2018 10:19
-
-
Save vinitshahdeo/589beeed65940da177b3c63ebea314e5 to your computer and use it in GitHub Desktop.
Inorder Successor - BST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Node * inOrderSuccessor(Node *root, Node *x) | |
{ | |
if(root==NULL) | |
return root; | |
if(x->right) | |
{ | |
Node* temp=x->right; | |
while(temp->left!=NULL) | |
temp=temp->left; | |
return temp; | |
} | |
else | |
{ | |
Node* successor=NULL; | |
Node* ancestor=root; | |
while(ancestor!=x) | |
{ | |
if(x->data<ancestor->data) | |
{ | |
successor=ancestor; | |
ancestor=ancestor->left; | |
} | |
else | |
{ | |
ancestor=ancestor->right; | |
} | |
} | |
return successor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment