Zijeak 2019-01-02 16:28 采纳率: 33.3%
浏览 1356

数据结构:二叉树层次遍历问题

该代码无法输出任何内容就结束,请问是怎么回事

#include <stdio.h>
//二叉树的类型定义 
typedef struct BiNode{
    char data;
    struct BiNode* LChild;
    struct BiNode* RChild;
}BiNode,*BiTree;
//队列的类型定义 
typedef struct Node{
    char data;
    struct Node* next;
}LinkQueueNode;

typedef struct{
    LinkQueueNode *front;
    LinkQueueNode *rear;
}LinkQueue;

//初始化队
int InitQueue(LinkQueue *q)
{
    q->front=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));
    if(q->front!=NULL)
    {
        q->rear=q->front;
        q->front->next=NULL;
        return(1);
    }
    else return(0);
} 
//入队操作
int EnterQueue(LinkQueue *q,char x)
{
    LinkQueueNode *NewNode;
    NewNode=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));
    if(NewNode!=NULL)
    {
        NewNode->data=x;
        NewNode->next=NULL;
        q->rear->next=NewNode;
        q->rear=NewNode;
        return(1);
    }
    else return(0);
} 

//出队操作
int DeleteQueue(LinkQueue *q,char *x)
{
    LinkQueueNode *p;
    if(q->front==q->rear)
     return(0);
    p=q->front->next;
    q->front->next=p->next;
    if(q->rear==p)
     q->rear=q->front;
    *x=p->data;
    free(q);
    return(1);
} 
int isEmpty(LinkQueue *q)
{
    if(q->front==NULL)
        return 1;
    return 0;
}
//按层序打印二叉树 
void printInLines(BiTree T)
{
    LinkQueue q;
    InitQueue(&q);
    if(T==NULL)
     return;
    EnterQueue(&q,T->data);//根节点进队
    while(!isEmpty(&q))
    {
        //出对保存队头并访问 
        BiNode *s;
        DeleteQueue(&q,s);
        printf("%c",s->data);
        //将出队结点的左子树根入队
        EnterQueue(&q,(s->LChild)->data);
        //将出队结点的右子树根入队
        EnterQueue(&q,(s->RChild)->data); 
    } 

}
//建立二叉树 
void createBiTree(BiTree* T)
{
    char ch;
    scanf("%c",&ch);
    if(ch='.')
     *T=NULL;
    else
    {
        *T=(BiTree)malloc(sizeof(BiNode));
        (*T)->data=ch;
        createBiTree((*T)->LChild);
        createBiTree((*T)->RChild);
    }
}
void main()
{
    BiTree T;
    createBiTree(&T);
    printInLines(&T); 
}

  • 写回答

1条回答 默认 最新

  • wgf098 2019-01-02 13:41
    关注

    createBitree函数中,if(ch = '.'),应该写成if (ch == '.')吧。还有,这个函数中,应该写成createBiTree(&(*T)->LChild)吧,createBiTree要求传的是二级指针。

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?