中序递归创建二叉树有门道么?

来源:百度知道 编辑:UC知道 时间:2024/06/07 23:52:06
先序的递归按照书上的方法写出来了,中序和后续的怎么想怎么不对。以下是我的先序的函数
bitnode* createbitree1(bitnode *t,int i,int *j,char *ch)
{
if(*j>i||ch[*j]==' '||ch[*j]=='#')
t=NULL;
else
{
t=(bitnode *)malloc(sizeof(bitnode)); t->data=ch[*j];
(*j)++;
t->lchild=createbitree1(t->lchild,i,j,ch); (*j)++;
t->rchild=createbitree1(t->rchild,i,j,ch);
}
return(t);
}
有什么办法实现中序和后续么?

中序bitnode* createbitree1(bitnode *t,int i,int *j,char *ch)
{
if(*j>i||ch[*j]==' '||ch[*j]=='#')
t=NULL;
else
{
t->lchild=createbitree1(t->lchild,i,j,ch); (*j)++;
t=(bitnode *)malloc(sizeof(bitnode)); t->data=ch[*j];
(*j)++;

t->rchild=createbitree1(t->rchild,i,j,ch);
}
return(t);
}
后序
bitnode* createbitree1(bitnode *t,int i,int *j,char *ch)
{
if(*j>i||ch[*j]==' '||ch[*j]=='#')
t=NULL;
else
{
t->lchild=createbitree1(t->lchild,i,j,ch); (*j)++;
t->rchild=createbitree1(t->rchild,i,j,ch);
t=(bitnode *)malloc(sizeof(bitnode)); t->data=ch[*j];
(*j)++;

}
return(t);
}
看出来没中序就是访问根给点或者父节点在中间
同理后序就是访问根给点或者父节点在后面