185中国男明星:我编了一个二叉树的程序,但在运行时出现了一些小问题!

来源:百度文库 编辑:高考问答 时间:2024/04/27 23:14:05
程序如下:
#define max 30
#define NULL 0
#include <iostream.h>
typedef struct btnode
{
char data;
struct btnode *lchild,*rchild;
}bttree;
bttree *cre_tree(char *str,int i,int m)
{
bttree *p;
if(i>=m)
return NULL;
p=new bttree;
p->data=str[i];
p->lchild=cre_tree(str,2*i,m);
p->rchild=cre_tree(str,2*i+1,m);
return p;
}
void main()
{
char x,y;
int i,n;
char str[max];
bttree *root;
cout<<"please input a bbttee node num:\n";
cin>>n;
cout<<"please input a string which length is:"<<n<<endl;
for(i=0;i<n;i++)
{
cout<<"请输入第"<<i<<"个数字:";
cin>>str[i];
}
cout<<endl;
root=cre_tree(str,0,n);
cout<<"the tree is already created\n";
x=root->rchild->data;
y=root->lchild->rchild->data;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
以上程序的问题是:在输入完数据时,该程序直接直接结束,不执行以下的语句,不知道什么原因?希望哪位高手帮我解决一下,并改进一下我这个程序。小生在此谢过了!!!

不能从0开始输入 for(i=1;i<n;i++)
应该 root=cre_tree(str,1,n);
cre_tree(str,0,n)参数取0时 一直在嵌套运行