Inorder predecessor and successor for a given key in BST

PROBLEM :

Inorder predecessor and successor for a given key in BST
You need to find the inorder successor and predecessor of a given key.



Example(for above tree)

Input
4  //(no of test cases)

4  // (elements whose Inorder predecessor and successor needed to be printed)
8
20
22

Output
no inorder Predecessor         //for 4
inorder Successor is 8

inorder Predecessor is 4       //for 8
inorder Successor is 10

inorder Predecessor is 14      //for 20
inorder Successor is 22

inorder Predecessor is 20      //for 22
no inorder Successor

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*  Structure of a Binary Tree
struct Node
{
    int data;
    struct Node* left, *right;
}; */

/* Function to get the maximum width of a binary tree*/

void inorder_array_fill(tree* ,int [],int *) ;
int size_tree(tree* ) ;

void inorder_predecessor_successor(tree *root,tree **pre,tree **succ,int ele)
{
if(root==NULL)
return  ;

int h,i=0;
h=size_tree(root) ;

int array[h] ;

inorder_array_fill(root,array,&i) ;

for(i=0;i<h;i++)
cout<<array[i]<<" " ;

for(i=0;i<h;i++)
{
if(array[i]==ele)
{
if(i==0)
{
cout<<"\n no inorder Predecessor " ;
cout<<"\n inorder Successor is "<<array[i+1] ;
break ;
}
else if(i==h-1)
{
cout<<"\n inorder Predecessor is "<<array[i-1] ;
cout<<"\n no inorder Successor" ;
break ;
}
else
{
cout<<"\n inorder Predecessor is "<<array[i-1] ;
cout<<"\n inorder Successor is "<<array[i+1] ;
break ;
}
}
}
}

void inorder_array_fill(tree* root,int a[],int *s)
{
    if(root==NULL)
        return  ;
     
    inorder_array_fill(root->left,a,&(*s)) ;
    a[(*s)++]=root->info ;
    inorder_array_fill(root->right,a,&(*s)) ;
 
}

int size_tree(tree* root)
{
if(root==NULL)
return 0 ;

return ((size_tree(root->left))+(size_tree(root->right))+1) ;
}              

-------------------------------------------------------------------------------
ALTERNATE SOLUTION :
-------------------------------------------------------------------------------

/*  Structure of a Binary Tree
struct Node
{
    int data;
    struct Node* left, *right;
}; */

/* Function to get the maximum width of a binary tree*/

void inorder_predecessor_successor(tree *root,tree **pre,tree **succ,int ele)
{
if(root==NULL)
{
cout<<"\n No inorder predecessor and successor" ;
return ;
}
if(root->info==ele)
{
if(root->left)
{
tree* temp ;
temp=root->left ;
while(temp->right!=NULL)
temp=temp->right ;
(*pre)=temp ;
}

if(root->right)
{
tree *temp ;
temp=root->right ;
while(temp->left!=NULL)
temp=temp->left ;
(*succ)=temp ;
}
}
else
{
if(root->info>ele)
{
(*succ)=root ;
inorder_predecessor_successor(root->left,pre,succ,ele) ;
}
else
{
(*pre)=root ;
inorder_predecessor_successor(root->right,pre,succ,ele) ;
}
}
}                


---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )