数据结构与算法题目集(中文) 6-9 二叉树的遍历 (25分)
// #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;
// }
//space + char
//中序
void InorderTraversal( BinTree BT )
{
if (BT)
{
InorderTraversal( BT->Left);
printf(" %c", BT->Data);
InorderTraversal( BT->Right);
}
}
//前序
void PreorderTraversal( BinTree BT )
{
if (BT)
{
printf(" %c", BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
} //后序
void PostorderTraversal( BinTree BT)
{
if (BT)
{
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf(" %c", BT->Data);
}
} //层遍历 用到队列知识
void LevelorderTraversal( BinTree BT )
{
BinTree qu[2000];
int head = 0, tail = 0;
if (BT) qu[tail++] = BT;
while (head < tail)
{
if(qu[head]->Left) qu[tail++] = qu[head]->Left;
if(qu[head]->Right) qu[tail++] = qu[head]->Right;
printf(" %c",qu[head++]->Data);
}
} 相关推荐
shenwenjie 2020-09-24
xiesheng 2020-08-02
mingyunxiaohai 2020-07-28
Cypress 2020-07-08
Cypress 2020-06-28
Dyancsdn 2020-06-27
Masimaro 2020-06-21
waitwolf 2020-06-21
Jasmineyaoyao 2020-06-16
OldBowl 2020-06-16
alicelmx 2020-06-16
Masimaro 2020-06-14
waitwolf 2020-06-08
nurvnurv 2020-06-05
ustbfym 2020-06-04
ziruoxiangyi 2020-06-03
范范 2020-06-03
Jasmineyaoyao 2020-05-31