#include<bits/stdc++.h>
using
namespace
std;
struct
Node
{
int
data;
struct
Node *left, *right;
};
Node* temp =
new
Node;
Node* newNode(
int
data)
{
Node *temp =
new
Node;
temp->data = data;
temp->left = temp->right = NULL;
return
temp;
}
Node* leftMostNode(Node* node)
{
while
(node != NULL && node->left != NULL)
node = node->left;
return
node;
}
Node* rightMostNode(Node* node)
{
while
(node != NULL && node->right != NULL)
node = node->right;
return
node;
}
Node* findInorderRecursive(Node* root, Node* x )
{
if
(!root)
return
NULL;
if
(root==x || (temp = findInorderRecursive(root->left,x)) ||
(temp = findInorderRecursive(root->right,x)))
{
if
(temp)
{
if
(root->left == temp)
{
cout <<
"Inorder Successor of "
<< x->data;
cout <<
" is "
<< root->data <<
"\n"
;
return
NULL;
}
}
return
root;
}
return
NULL;
}
void
inorderSuccessor(Node* root, Node* x)
{
if
(x->right != NULL)
{
Node* inorderSucc = leftMostNode(x->right);
cout<<
"Inorder Successor of "
<<x->data<<
" is "
;
cout<<inorderSucc->data<<
"\n"
;
}
if
(x->right == NULL)
{
Node* rightMost = rightMostNode(root);
if
(rightMost == x)
cout <<
"No inorder successor! Right most node.\n"
;
else
findInorderRecursive(root, x);
}
}
int
main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
inorderSuccessor(root, root);
inorderSuccessor(root, root->left->left);
inorderSuccessor(root, root->right->right);
return
0;
}