请各位帮帮忙看看问题出在哪里(二叉树)
我修改了好久,也弄不出来,帮忙看看,谢谢谢谢#include <stdio.h>
#include <stdlib.h>
#define MaxSize 30
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *lchild;
struct node *rchild;
}BTNode,*BiTree;
void CreateBTNode(BTNode *&b,char *str)//创建二叉树
{
BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case '(':top++;St[top]=p;k=1;break;
case ')':top--;break;
case ',':k=2;break;
default:p=(BTNode *)malloc(sizeof(MaxSize));
p->data=ch;p->lchild=p->rchild=NULL;
if(b==NULL)
{
b=p;}
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
j++;
ch=str[j];
}
}
}
void PreOrder(BTNode *b)//先序
{
BTNode *St[MaxSize],*p;
int top=-1;
if(b!=NULL)
{
top++;
St[top]=b;
while(top>-1)
{
p=St[top];
top--;
printf("%c",p->data);
if(p->rchild !=NULL)
{
top++;
St[top]=p->rchild;
}
if(p->lchild !=NULL)
{
top++;
St[top]=p->lchild ;
}
}
printf("\n");
}
}
void InOrder(BTNode *b)//中序
{
BTNode *St[MaxSize],*p;
int top=-1;
if(b!=NULL)
{
p=b;
while(top>-1||p!=NULL)
{
while(p!=NULL)
{
top++;
St[top]=p;
p=p->lchild ;
}
if(top>-1)
{
p=St[top];
printf("%c",p->data);
p=p->rchild ;
}
}
printf("\n");
}
}
void CountLeaf(BiTree T,int &count)//统计结点算法
{
if ( T )
{
if ((!T->lchild)&& (!T->rchild))
count++; // 对叶子结点计数
CountLeaf( T->lchild, count);
CountLeaf( T->rchild, count);
}
else return;
}
void DispBTNode(BTNode *b)//输出二叉树
{
if(b!=NULL)
{
printf("%c",b->data);
if(b->lchild!=NULL||b->rchild!=NULL)
{
printf("(");
DispBTNode(b->lchild);
if(b->rchild!=NULL)printf(",");
DispBTNode(b->rchild);
printf(")");
}
}
}
void LevelOrder(BTNode *b)//层次遍历算法
{
BTNode *p;
BTNode *qu[MaxSize];
int front,rear;
front=rear=-1;
rear++;
qu[rear]=b;
while(front!=rear)
{
front=(front+1)%MaxSize;
p=qu[front];
printf("%c",p->data);
if(p->lchild!=NULL)
{
rear=(rear+1)%MaxSize;
qu[rear]=p->lchild;
}
if(p->rchild!=NULL)
{
rear=(rear+1)%MaxSize;
qu[rear]=p->rchild;
}
}
}
int BTNodeHeight(BTNode *b)//求二叉树的高度
{
int lchildh,rchildh;
if(b==NULL)return(0);
else
{
lchildh=BTNodeHeight(b->lchild );
rchildh=BTNodeHeight(b->rchild );
return (lchildh>rchildh)?(lchildh+1):(rchildh+1);
}
}
BTNode *FindNode(BTNode *b,ElemType x)//查找结点
{
BTNode *p;
if(b==NULL)
return NULL;
else if(b->data==x)
return b;
else
{
p=FindNode(b->lchild,x);
if(p!=NULL)
return p;
else
return FindNode(b->rchild,x);
}
}
void main()
{
BTNode *b;
char str[]={'A','(','B','(','(',')',',','C','(','D',',','(',')',')',')',',','E','(','(',')',',','F',')',')'};
CreateBTNode(b,str);
printf("统计结点数:\n");
//CountLeaf(b,2);
printf("前序遍历:\n");
PreOrder(b);
DispBTNode(b);
printf("中序遍历:\n");
InOrder(b);
printf("层次遍历输出结点如下:\n");
LevelOrder(b);
printf("二叉树的高度:\n");
BTNodeHeight(b);
printf("查找二叉树的结点:\n");
BTNode *FindNode();
}