niushao 2020-03-09
// #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); } }