彗星爱酿酒 2023-04-18 00:50 采纳率: 60.7%
浏览 15
已结题

有关二叉树的层次遍历问题

和二叉树层次遍历(C语言)有关,想问一下这样写为什么不对?我应该怎么改才行呢?如果可以的话,能具体讲讲吗?(请就用我这种方法)谢谢了(^ω^)
函数接口定义:

void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );

BinTree结构定义:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

BinTree CreatBinTree(); /* 实现细节忽略 */
void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );

int main()
{
    BinTree BT = CreatBinTree();
    printf("Inorder:");    InorderTraversal(BT);    printf("\n");
    printf("Preorder:");   PreorderTraversal(BT);   printf("\n");
    printf("Postorder:");  PostorderTraversal(BT);  printf("\n");
    printf("Levelorder:"); LevelorderTraversal(BT); printf("\n");
    return 0;
}
/* 你的代码将被嵌在这里 */

我的代码:

#define Max 1000
typedef enum {false,true} bool;
typedef struct Node *pr;
struct Node{
        char *Data;
        int f;
        int r;
        int Max;
};
typedef pr Queue;
void InorderTraversal( BinTree BT ){
    if(BT){
        PreorderTraversal(BT->Left);
        printf("%d",BT->Data);
        PreorderTraversal(BT->Right);
    }
}
void PreorderTraversal( BinTree BT ){
    if(BT){
        printf("%d",BT->Data);
        PreorderTraversal(BT->Left);
        PreorderTraversal(BT->Right);
    }
}
void PostorderTraversal( BinTree BT ){
    if(BT){
        PreorderTraversal(BT->Left);
        PreorderTraversal(BT->Right);
        printf("%d",BT->Data);
    }
}
Queue CreatQueue(int Max){
    Queue Q=(Queue)malloc(sizeof(struct QNode));
    Q->Data=(char *)malloc(Max*sizeof(char));
    Q->f=Q->r=0;
    Q->Max=Max;
    return Q;
}
bool AddQ(Queue Q,int X){
    if((Q->r+1)%Q->Max==Q->f)
        return false;
    else{
        Q->r=(Q->r+1)%Q->Max;
        Q->Data[Q->r]=X;
        return true;
    }

}
bool DeleteQ(Queue Q){
    if(Q->r == Q->f){
        //printf("invalid\n");
        return false;
    }else{
         Q->f=(Q->f+1)%Q->Max;
         return true;
    }
}
void LevelorderTraversal( BinTree BT ){
    Queue Q=CreatQueue();
    BinTree T;
    AddQ(Q,BT);
    while(Q){
        T=define(Q);
        printf("%d",T->Data);
        if(T->Left){
            AddQ(Q,T->Left);
        }
        if(T->Right){
            AddQ(Q,T->Right);
        }
    }
}

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-04-18 01:08
    关注

    T=define(Q);
    define???

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月21日
  • 创建了问题 4月18日

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上