Given the code for outputing the postorder traversal of a tree when I have the preorder and the inorder traversal in an interger array. How do I similarily get the preorder with the inorder and postorder array given?
void postorder( int preorder[], int prestart, int inorder[], int inostart, int length)
{
if(length==0) return; //terminating condition
int i;
for(i=inostart; i<inostart+length; i++)
if(preorder[prestart]==inorder[i])//break when found root in inorder array
break;
postorder(preorder, prestart+1, inorder, inostart, i-inostart);
postorder(preorder, prestart+i-inostart+1, inorder, i+1, length-i+inostart-1);
cout<<preorder[prestart]<<" ";
}
Here is the prototype for preorder()
void preorder( int inorderorder[], int inostart, int postorder[], int poststart, int length)
to use postorder() it will be
int preorder[6]={6,4,1,5,8,9};
int inorder[6]={1,4,5,6,8,9};
postorder( preorder,0,inorder,0,6);
out put will be
1 5 4 9 8 6
below is the incorrect code for print_preorder(), still not working below
void print_preorder( int inorder[], int inostart, int postorder[], int poststart, int length)
{
if(length==0) return; //terminating condition
int i;
for(i=inostart; i<inostart+length; i++)
if(postorder[poststart+length-1]==inorder[i])
break;
cout<<postorder[poststart+length-1]<<" ";
print_preorder(inorder, inostart , postorder, poststart, i-inostart);
print_preorder(inorder, inostart+i-poststart+1, postorder, i+1, length-i+inostart-1);
}