#include<stdio.h>
typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode {
ElementType Data;
BinTree Left;
BinTree Right;
};
void PreorderTraversal1(BinTree BT) {
if (BT) {
printf("%d", BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
}
void PreorderTraversal2(BinTree BT) {
BinTree T = BT;
Stack S = CreateStack(MaxSize);
while (T || !isEmpty(S)) {
while (T) {
Push(S, T);
printf("%d", T->Data);
T = T->Left;
}
if (!isEmpty(S)) {
T = Pop(S);
T = T->Right;
}
}
}
void InorderTraversal1(BinTree BT) {
if (BT) {
PreorderTraversal(BT->Left);
printf("%d", BT->Data);
PreorderTraversal(BT->Right);
}
}
void InorderTraversal2(BinTree BT) {
BinTree T = BT;
Stack S = CreateStack(MaxSize);
while (T || !isEmpty(S)) {
while (T) {
Push(S, T);
T = T->Left;
}
if (!isEmpty(S)) {
T = Pop(S);
printf("%d", T->Data);
T = T->Right;
}
}
}
void PostorderTraversal1(BinTree BT) {
if (BT) {
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
printf("%d", BT->Data);
}
}
void LevelorderTraversal(BinTree BT) {
BinTree TP;
if (!BT) return;
Queue Q = CreateQueue();
AddQuene(Q, BT);
while (isEmpty(Q)) {
TP = DeleteQueue(Q);
printf("%d ", TP->Data);
if (TP->Left) AddQuene(Q, TP->Left);
if (TP->Right) AddQuene(Q, TP->Right);
}
}