求各位大神帮我看看,程序有一个段错误,但我实在是找不到原因,求指导。
#include<iostream>
#include<string>
using namespace std;
struct tnode
{
string word;
int cout;
tnode *left;
tnode *right;
} ;
struct tnode ** tra(struct tnode *r,string s)
{
if( s<=r->word)
{
if(r->left==NULL)
return &(r->left);
else
tra(r->left,s);
}
if(s>r->word)
{
if(r->right==NULL)
return &(r->right);
tra(r->right,s);
}
}
void T_Insert(struct tnode *r)
{
int i=0;
struct tnode *a,**b;
if(r==NULL)
{
a=new (struct tnode);
cin>>a->word;
a->left=a->right=NULL;
a->cout=i++;
r=a;
}
a=new (struct tnode);
cin>>a->word;
do
{
a->left=a->right=NULL;
a->cout=i++;
b=tra(r,a->word);//返回应当把a插入位置的指针
*b=a;
a=new (struct tnode);
}while(cin >>a->word);//当输入EOF时,结束结点的插入
}
int Tranfer(struct tnode *r)
{
if(r==NULL)
return 0;
else
{
Tranfer(r->left);
cout<<r->word<<' ';
Tranfer(r->right);
}
}
int main()//按字典序建二叉树
{
struct tnode * r=NULL;
T_Insert(r);//建树
Tranfer(r);//遍历
return 0;
}