c语言层次遍历二叉树遇到的问题

**c语言层次遍历二叉树,无法正常执行,搞了半天也不知道出了啥问题,望解答,感谢!

下面是完整代码**





#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

//树
typedef char DataType;
typedef struct tnode {
    DataType data;
    struct tnode* lchild, * rchild;
}BT, * BTT;



//循环队列     队列类型要写在树类型的后面,因为要用到树类型 
#define MaxSize 50
typedef struct node {
    BTT data[MaxSize]; // 
    int rear, front;
}SqQueue;


BT* Create_(); //这种先序建树可行
//void Create_(BT* T); //先序创建二叉树(这种传参的不知道哪出了问题)
void cen_bianli_(BT* T); //层次遍历二叉树
void visit_(BT* p); //输出函数
void visit2(BT* T); //中序遍历二叉树


void initQueue_(SqQueue* Q);// 初始化循环队列
void EnQueue_(SqQueue* Q, BT* T); //入队
void DeQueue_(SqQueue* Q, BT* p); //出队
int EmptyQueue_(SqQueue* Q); //判队空


int main()
{
    //SqQueue Q;

    BT* T; //不用初始化也可以传参

    T = Create_();
    //Create_(T); 
    visit2(T);
    cen_bianli_(T);

    return 0;
}

//中序遍历 
void visit2(BT* T)
{
    //printf("1");
    if (T != NULL)
    {
        visit2(T->lchild);
        printf("%c ", T->data);//cout << T->data << " ";
        visit2(T->rchild);
    }
}


//先序创建二叉树 
BT* Create_()
{
    BT* t;
    char ch;
    cin >> ch;//scanf("%c", &ch);
    getchar();

    if (ch == '#')
    {
        t = NULL;
    }
    else
    {
        t = (BT*)malloc(sizeof(BT));
        t->data = ch;
        printf("请输入%c节点的lchild节点值:", t->data);
        t->lchild = Create_();
        printf("请输入%c节点的rchild节点值:", t->data);
        t->rchild = Create_();
    }
    return t;
}




/*
void Create_(BT* T)  //这种建立二叉树的方式不知道哪里出了问题
{
    //getchar();
    char ch;
    scanf("%c",&ch);//cin >> ch;
    getchar();
    if (ch == '#')
        T = NULL;
    else
    {
        T = (BT*)malloc(sizeof(BT));
        T->data = ch;
        printf("请输入结点%c的左孩子结点:", T->data);
        Create_(T->lchild);
        printf("请输入结点%c的右孩子结点:", T->data);
        Create_(T->rchild);
    }
}

*/

// 队列初始化 
void initQueue_(SqQueue* Q)
{
    Q->front = Q->rear == 0;
}

//入队 
void EnQueue_(SqQueue* Q, BT* T) //
{
    if ((Q->rear + 1) % MaxSize == Q->front)
        printf("队列已满!");//cout << "队列已满!" << endl;
    Q->data[Q->rear] = T; //
    Q->rear = (Q->rear + 1) % MaxSize;
}

//出队 
void DeQueue_(SqQueue* Q, BT* p)
{
    if (EmptyQueue_(Q) == 1)
        printf("队列已空!");//cout << "队列已空!";
    else
    {
        p = Q->data[Q->front++]; //
        //Q->front = (Q->front + 1) % MaxSize; 
    }
}

//队列判空 
int EmptyQueue_(SqQueue* Q)
{
    if (Q->rear == Q->front)
        return 1;
    else
        return 0;    // 非空的情况下返回false
}

//纯输出函数 
void visit_(BT* p)
{
    printf("%c,", p->data);//cout << p->data << " " << endl;
}


//层次遍历 
void cen_bianli_(BT* T) 
{
    SqQueue* Q = NULL;
    initQueue_(Q); //初始化队列
    BT* p = NULL;
    //printf("提示"); // **检查得知,下面的根节点入队出了问题!** 
    EnQueue_(Q, T);//将根结点入队
    //printf("提示"); // 执行不到这一步 
    while (!EmptyQueue_(Q))
    {
        DeQueue_(Q, p); //出队  
        visit_(p); //访问出队结点(输出)
        if (p->lchild != NULL)
            EnQueue_(Q, p->lchild); //左子树不空,则左子树根节点入队
        if (p->rchild != NULL)
            EnQueue_(Q, p->rchild); //右子树不空,则右子树根节点入队
    }
}


运行的异常结果: 只中序遍历能正常输出,到层次遍历时出现异常

img

应该是遇到空指针了,调试下呗
应该分配内存,如下

//层次遍历 

void cen_bianli_(BT* T) 

{

    SqQueue* Q = NULL;
    Q =(SqQueue* )malloc(sizeof(SqQueue));

    initQueue_(Q); //初始化队列

    BT* p = NULL;

    //printf("提示"); // **检查得知,下面的根节点入队出了问题!** 

    EnQueue_(Q, T);//将根结点入队

    //printf("提示"); // 执行不到这一步 

    while (!EmptyQueue_(Q))

    {

        DeQueue_(Q, p); //出队  

        visit_(p); //访问出队结点(输出)

        if (p->lchild != NULL)

            EnQueue_(Q, p->lchild); //左子树不空,则左子树根节点入队

        if (p->rchild != NULL)

            EnQueue_(Q, p->rchild); //右子树不空,则右子树根节点入队

    }

}