An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#define MaxSize 30
#define ERROR -1
typedef int ElementType;
typedef int Position;
typedef struct SNode {
ElementType *Data; /* 存储元素的数组 */
Position Top; /* 栈顶指针 */
int Maxsize; /* 堆栈最大容量 */
}SNode, *Stack;
ElementType PreArray[MaxSize]; /*先序序列数组*/
ElementType InArray[MaxSize]; /*中序序列数组*/
ElementType PostArray[MaxSize]; /*后续序列数组*/
Stack CreateStack(int Maxsize); //创建堆栈
bool IsFull(Stack S); //是否满栈
bool Push(Stack S, ElementType X); //入栈
bool IsEmpty(Stack S); //栈是否为空
ElementType Pop(Stack S); //出栈
void GetPostOrder(int preIx, int InIx, int PostIx, int NodNum); //获得后序序列
int main()
{
for (int i = 0; i < MaxSize; i++)
{
PreArray[i] = 0;
InArray[i] = 0;
PostArray[i] = 0;
}
Stack stack = CreateStack(MaxSize);
Position PreIx = 0, InIx = 0, PostIx = 0;/*先序、中序、后序数组的索引*/
ElementType Data;
int NodeNum; //二叉树的节点数
char* str = (char*)malloc(sizeof(char)*5);
//char* s1 = "push", *s2 = "pull";
scanf("%d", &NodeNum);
/*push的元素为前序遍历,pop对应中序遍历,求前序遍历序列和中序遍历序列,并存入数组*/
for (int i = 0; i < 2 * NodeNum; i++)
{
scanf("%s", str);
if (strcmp(str, "Push") == 0)
{
scanf("%d", &Data);
Push(stack, Data);
PreArray[PreIx++] = Data;
}
else if(strcmp(str, "Pop") == 0)
{
Data = Pop(stack);
InArray[InIx++] = Data;
}
}
PreIx = 0, InIx = 0, PostIx = 0;
GetPostOrder(PreIx, InIx, PostIx, NodeNum);
int flag = 0; //控制空格的输出
for (int i = 0; i < NodeNum; i++)
{
if (flag == 0)
flag = 1;
else
{
printf(" ");
}
printf("%d", PostArray[i]);
}
return 0;
}
Stack CreateStack(int Maxsize) //创建堆栈
{
Stack S = (Stack)malloc(sizeof(SNode));
S->Data = (ElementType *)malloc(Maxsize * sizeof(ElementType));
S->Top = -1;
S->Maxsize = Maxsize;
return S;
}
bool IsFull(Stack S) //是否满栈
{
return (S->Top == S->Maxsize - 1);
}
bool Push(Stack S, ElementType X) //入栈
{
if (IsFull(S)) {
printf("堆栈满");
return false;
}
else {
S->Data[++(S->Top)] = X;
return true;
}
}
bool IsEmpty(Stack S) //栈是否为空
{
return (S->Top == -1);
}
ElementType Pop(Stack S) //出栈
{
if (IsEmpty(S)) {
printf("堆栈空");
return ERROR; /* ERROR是ElementType的特殊值,标志错误 */
}
else
return (S->Data[(S->Top)--]);
}
void GetPostOrder(int preIx, int InIx, int PostIx, int NodNum)
{
if (NodNum == 0)
return;
if (NodNum == 1)
PostArray[PostIx] = PreArray[preIx];
int Root = PreArray[preIx];
int RNum, LNum;
int i;
PostArray[PostIx + NodNum - 1] = Root; //前序序列首元素为根节点
/*寻找根节点在中序序列的位置,将树分为左右子树,递归调用GetPostOrder函数*/
for (i = 0; i < NodNum; i++)
{
if(InArray[InIx + i] == Root)
break;
}
LNum = i;
RNum = NodNum - i - 1;
GetPostOrder(preIx + 1, InIx, PostIx, LNum);
GetPostOrder(preIx + LNum + 1, InIx + LNum + 1, PostIx + LNum, RNum);
}