数据结构,二叉树,我想用队列实现层次遍历,然后就报如图的错,怎么改才好?

#include<stdio.h>#include<malloc.h>#include"BTree.h" //包含二叉树的存储结构声明#define MaxSize 50typedef char ElemType ;typedef struct node{ ElemType data; //数据元素 struct node *lchild;//指向左孩子结点 struct node *rchild;//指向右孩子结点}BTNode;void CreateBTree(BTNode *&b,char *str) //创建二叉树{ BTNode *St[MaxSize] , *p ;//St数组作为顺序栈 int top=-1,k,j=0;//top作为栈顶指针 char ch; b=NULL;//初始时二叉链为空 ch=str[j]; while(ch!='\0') //循环扫描str中每个字符 { switch(ch) { case'(':top++;St[top]=p;k=1;break;//开始处理左孩子节点 case')':top--;break;//栈顶结点的子树处理完毕 case',':k=2;break;//开始处理右孩子结点 default:p=(BTNode *)malloc(sizeof(BTNode));//创建一个右结点,由p指向它 p->data=ch;//存放节点值 p->lchild=p->rchild=NULL;//左、右指针都设置为空 if(b==NULL)//若尚未建立根节点 b=p;//p指向的结点就为根节点 else { switch(k) { case 1:St[top]->lchild=p;break;//新建结点作为栈顶结点的左孩子 case 2:St[top]->rchild=p;break;//新建结点作为栈顶结点的右孩子 } } } j++;//继续扫描str ch=str[j]; }}typedef struct{ BTNode *data[MaxSize];//存放队中元素 int front,rear;//队头和队尾指针}SqQueue;//顺序队类型void InitQueue(SqQueue *&q) //初始化队列{ q=(SqQueue *)malloc(sizeof(SqQueue )); q->front=q->rear=0;}bool enQueue(SqQueue *&q,ElemType e[])//进队列{ if((q->rear+2)%MaxSize==q->front)//队满上溢出 return false; q->rear=(q->rear+1)%MaxSize; q->data[q->rear]=e; return true;}bool QueueEmpty(SqQueue *q){ return(q->front==q->rear);}bool deQueue(SqQueue *&q,ElemType e[])//进队列{ if(q->front==q->rear)//队空下溢出 return false; q->front=(q->front+1)%MaxSize; e=q->data[q->front]; return true;}void LevelOrder(BTNode *b) //层次遍历法{ BTNode *p; SqQueue *qu;//定义环形队列指针 InitQueue (qu);//初始化队列; enQueue(qu,b);//根结点指针进入队列 while(!QueueEmpty(qu))//连队不为空 { deQueue(qu,p);//出队结点p printf("%c",p->data);//访问结点p if(p->lchild!=NULL)//有左孩子时将其进队 enQueue(qu,p->lchild); if(p->rchild!=NULL)//有右孩子时将其进队 enQueue(qu,p->rchild); }} void main(){ BTNode *b; CreateBTree(b,"A(B(D,E(H(J,K(L,M(N))))),C(F,G(I))"); LevelOrder(b);}

#include<stdio.h>
#include<malloc.h>
//#include"BTree.h"
//包含二叉树的存储结构声明
#define MaxSize 50
typedef char ElemType ;
typedef struct node {
    ElemType data; //数据元素
    struct node *lchild;//指向左孩子结点
    struct node *rchild;//指向右孩子结点
} BTNode;
void CreateBTree(BTNode *&b,char *str) //创建二叉树
{
    BTNode *St[MaxSize] , *p ;//St数组作为顺序栈
    int top=-1,k,j=0;//top作为栈顶指针
    char ch;
    b=NULL;//初始时二叉链为空
    ch=str[j];
    while(ch!='\0') //循环扫描str中每个字符
    {   switch(ch) {
        case'(':
            top++;
            St[top]=p;
            k=1;
            break;//开始处理左孩子节点
        case')':
            top--;
            break;//栈顶结点的子树处理完毕
        case',':
            k=2;
            break;//开始处理右孩子结点
        default:
            p=(BTNode *)malloc(sizeof(BTNode));//创建一个右结点,由p指向它
            p->data=ch;//存放节点值
            p->lchild=p->rchild=NULL;//左、右指针都设置为空
            if(b==NULL)//若尚未建立根节点
                b=p;//p指向的结点就为根节点
            else {
                switch(k) {
                case 1:
                    St[top]->lchild=p;
                    break;
                //新建结点作为栈顶结点的左孩子
                case 2:
                    St[top]->rchild=p;
                    break;//新建结点作为栈顶结点的右孩子
                }
            }
        }
        j++;//继续扫描str
        ch=str[j];
    }
}
typedef struct {
    BTNode *data[MaxSize];//存放队中元素
    int front,rear;//队头和队尾指针
} SqQueue;//顺序队类型
void InitQueue(SqQueue *&q) //初始化队列
{
    q=(SqQueue *)malloc(sizeof(SqQueue ));
    q->front=q->rear=0;
}
bool enQueue(SqQueue *&q,BTNode *e) //进队列
{
    if((q->rear+2)%MaxSize==q->front)//队满上溢出
        return false;
    q->rear=(q->rear+1)%MaxSize;
    q->data[q->rear]=e;
    return true;
}
bool QueueEmpty(SqQueue *q) {
    return(q->front==q->rear);
}
bool deQueue(SqQueue *&q,BTNode *&e)//进队列
{
    if(q->front==q->rear)//队空下溢出
        return false;
    q->front=(q->front+1)%MaxSize;
    e=q->data[q->front];
    return true;
}
void LevelOrder(BTNode *b) //层次遍历法
{
    BTNode *p;
    SqQueue *qu;//定义环形队列指针
    InitQueue (qu);//初始化队列;
    enQueue(qu,b);//根结点指针进入队列
    while(!QueueEmpty(qu))//连队不为空
    {
        deQueue(qu,p);//出队结点p
        printf("%c",p->data);//访问结点p
        if(p->lchild!=NULL)//有左孩子时将其进队
            enQueue(qu,p->lchild);
        if(p->rchild!=NULL)//有右孩子时将其进队
            enQueue(qu,p->rchild);
    }
}
int main() {
    BTNode *b;
    CreateBTree(b,"A(B(D,E(H(J,K(L,M(N))))),C(F,G(I))");
    LevelOrder(b);
    return 0;
}

追问

emm 你运行过吗

我之前好像也有这样改过 好像不行

追答

当然运行了呀

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-04-26
楼主现在解决了没,我现在也遇到这个问题了,能否共享一下代码。谢谢