#include <stdio.h>
#include "windows.h"
typedef char E;
struct twoTree {
E element;
struct twoTree *leftNode;
struct twoTree *rightNode;
int flag;
};
typedef struct twoTree *TreeNode;
struct LNode {
TreeNode element;
struct LNode *Next;
};
typedef struct LNode *Node;
struct Queue {
Node front, rear;
};
typedef struct Queue *LQueue;
BOOL initQueue(LQueue queue) {
queue->front = queue->rear = malloc(sizeof(struct LNode));
if (queue->front == NULL) return 0;
return 1;
}
BOOL pushQueue(LQueue queue, TreeNode element) {
Node NewNode = malloc(sizeof(struct LNode));
if (NewNode == NULL) return 0;
NewNode->element = element;
queue->rear->Next = NewNode;
queue->rear = NewNode;
return 1;
}
BOOL isEmpty(LQueue queue) {
return queue->rear == queue->front;
}
TreeNode popQueue(LQueue queue) {
if (isEmpty(queue)) return NULL;
queue->front = queue->front->Next;
return queue->front->element;
}
void postOrder2(TreeNode root) {
struct Queue queue;
initQueue(&queue);
pushQueue(&queue, root);
while (!isEmpty(&queue)) {
root = popQueue(&queue);
if (root->leftNode != NULL)
pushQueue(&queue, root->leftNode);
if (root->rightNode != NULL)
pushQueue(&queue, root->rightNode);
printf("%c", root->element);
}
}
int main() {
TreeNode a = malloc(sizeof(struct twoTree));
TreeNode b = malloc(sizeof(struct twoTree));
TreeNode c = malloc(sizeof(struct twoTree));
TreeNode d = malloc(sizeof(struct twoTree));
TreeNode e = malloc(sizeof(struct twoTree));
TreeNode f = malloc(sizeof(struct twoTree));
a->element = 'A';
b->element = 'B';
c->element = 'C';
d->element = 'D';
e->element = 'E';
f->element = 'F';
a->leftNode = b;
a->rightNode = c;
b->leftNode = d;
b->rightNode = e;
c->rightNode = f;
c->leftNode = NULL;
d->leftNode = d->rightNode = NULL;
e->leftNode = e->rightNode = NULL;
f->leftNode = f->rightNode = NULL;
postOrder2(a);
}