Friday, March 1, 2013

Find the next node given parent node in BST


#include"tree_test.h"
#include<iostream>

using namespace std;

Node* InorderSuccessor(Node* node)
{
if(node->R)
{
Node* pre=node;
node=node->R;
while(node)
{
pre=node;
node=node->L;
}
return pre;
}
else if(node->parent)
{
Node* pre=node;
node=node->parent;
while(node && node->R==pre)
{
pre=node;
node=node->parent;
}
return node;
}
else
return NULL;
}

No comments:

Post a Comment