注册 登录
编程论坛 C语言论坛

问题关于中序遍历

魂之子 发布于 2020-07-15 20:14, 2049 次点击
#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;
}
/* 你的代码将被嵌在这里 */

以下是中序遍历的答案,我没太明白他的具体操作,求大佬帮解释一下。
尤其是 BinTree binTree[100];Bintree 定义的明明是指针数组,那么此时不久相当于定义一个二维数组吗,然后后面的操作就更疑惑了。



void LevelorderTraversal(BinTree BT)
{
    if (BT == NULL)return;
   
    BinTree binTree[100];
    int head = 0, last = 0;
    binTree[last++] = BT;
   
    while (head < last){
        BinTree temp = binTree[head++];
        printf(" %c", temp->Data);

        if (temp->Left)
            binTree[last++] = temp->Left;
        if (temp->Right)
            binTree[last++] = temp->Right;        
    }
}
原题网址
https://
3 回复
#2
八画小子2020-07-16 01:33
没有二维数组这种说法
#3
fulltimelink2020-07-16 16:04
指针数组 和二维数组是有区别的
binTree的元素是指向TNode的指针
#4
rjsp2020-07-17 08:33
以下是中序遍历的答案,……
void LevelorderTraversal(BinTree BT)

说个题外话,“中序遍历”难道不是“InorderTraversal”?“LevelorderTraversal”难道不是“层序遍历”?
1