/*
* 1. Idea is to use code of inorder traversal.
* 2. Keep track of last processed node in lastNode pointer variable.
* 3. While processing the current node print last process Node. Current node will be inorder successor of last node.
* 4. Update lastNode to current processed node and repat.
*
* The code can be modified with little changes to return inorder successor node of a specific node.
*/
void inorderSuccessor(node *ptr)
{
if(ptr)
{
inorderSuccessor(ptr->getLeftChild());
if(lastNode)
cout<<"\nSuccessor of "<<lastNode->getData();
else
cout<<"\nNo predecessor of ";
lastNode = ptr;
cout<<"\t"<<ptr->getData();
inorderSuccessor(ptr->getRightChild());
}
}
No comments:
Post a Comment