请大家帮忙看看我的问题出在哪
#include<iostream>using namespace std;
typedef char ElemType;
#define MaxSize 100
typedef struct node //定义二叉树
{
ElemType data;
struct node *lchild;
struct node *rchild;
}BTNode;
void CreateBTNode(BTNode *&h,char *str) //建造二叉树
{
BTNode *zhan[MaxSize],*p=NULL;
int top=-1,k,j=0;
char c;
c=str[j];
while(c!='#')
{
switch(c)
{
case '(':top++;zhan[top]=p;k=1;break;
case ')':top--;break;
case ',':k=2;break;
default:p=new BTNode;
p->data=c;p->lchild=NULL;p->rchild=NULL;
if(h==NULL)
h=p;
else
switch(k)
{
case 1:zhan[top]->lchild=p;break;
case 2:zhan[top]->rchild=p;break;
}
}
j++;
c=str[j];
}
}
void display(BTNode *h) //输出root的左右的孩子
{
cout<<"左孩子结点值:"<<h->lchild->data<<endl;
cout<<"右孩子结点值:"<<h->rchild->data<<endl;
}
int high(BTNode *h) //求高度
{
int i,lift,right;
if(h==NULL)
return 0;
else
{
if(h->lchild!=NULL)
i=1;
else
i=2;
switch(i)
{
case 1:lift=high(h->lchild)+1;break;
case 2:right=high(h->rchild)+1;break;
}
return (lift>right)? lift:right;
}
}
void wide(BTNode *h) //求宽度
{
BTNode *std[MaxSize];
int top=1,i=0,j,n,k=1,m,f=0,l=1,max;
int a[MaxSize];
std[top]=h;
i++;
a[f]=i;
if(std[1]->lchild!=NULL)
{
i++;
top++;
std[top]=std[1]->lchild;
}
if(std[1]->rchild!=NULL)
{
i++;
top;
std[top]=std[1]->lchild;
}
while(l<=MaxSize)
{
i=0;
m=i;
n=top;
for(j=k;j<=n;j++)
{
if(std[j]->lchild!=NULL)
{
i++;
top++;
std[top]=std[j]->lchild;
}
if(std[j]->rchild!=NULL)
{
i++;
top;
std[top]=std[j]->lchild;
}
}
k=k+m;
f++;
a[f]=i;
l++;
}
max=a[0];
for(i=1;i<=f;i++)
if(max<a[i])
max=a[i];
cout<<"宽度为:"<<max<<endl;
}
int main()
{
BTNode *h;
char str[MaxSize];
int i=0;
cin>>str[i];
while(str[i]!='#')
{
i++;
cin>>str[i];
}
CreateBTNode(h,str);
display(h);
wide(h);
}
输入是以括号表示法的二叉树 然后应该以#号结束
但是以#号结束时 程序就出错了 无法运行