-
April 17th, 2009, 02:34 PM
#1
consturcting tree from its preorder,inorder..
i have two arrays which represent the preorder
and inorder traversals in two rows.
i cant see why i get an empty tree?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node node;
struct node{
int key;
node* left;
node* right;
};
node* t(char* pre,char* in,int st_pre,int en_pre,int st_in,int en_in);
int main ()
{
char pre[13]={'a','b','c','d','e','f','g','h','i','j','k','l','\0'};
char in[13]={'c','b','e','d','f','a','h','g','j','i','l','k','\0'};
node* root=t(pre,in,0,0,0,0);
return 0;
}
node* t(char* pre,char* in,int st_pre,int en_pre,int st_in,int en_in)
{
int i;
node* root=(node*)malloc(sizeof(node));
if (st_pre==en_pre)
return NULL;
root->key=pre[st_pre];
for(i=0;((i<(int)strlen(pre))&&(pre[st_pre]!=in[i]));i++)
{
}
root->left=t(pre,in,st_pre+1,st_pre+i+1,st_in,i-1);
root->right=t(pre,in,st_pre+i+1,(int)(strlen(pre)-1),i+1,(int)(strlen(pre)-1));
return root;
}
-
April 17th, 2009, 03:47 PM
#2
Re: consturcting tree from its preorder,inorder..
Code:
if (st_pre==en_pre)
return NULL;
That's why
Kurt
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|