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.
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 <string.h>
#define MSTR 9
#define EType int
#define Null -1
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define MAXSIZE 30
//二叉树结构
typedef struct TreeNode* BTree;
struct TreeNode{
EType Data;
BTree Left;
BTree Right;
};
//堆栈结构
typedef int Position;
typedef struct TreeNode* ElementType;
struct SNode {
/* 存储元素指针数组,由于压入栈以后还要对左右子树进行操作必须为指针,如果是结构体返回节点就断了 */
ElementType* Data;
Position Top; /* 栈顶指针 */
int MaxSize; /* 堆栈最大容量 */
};
typedef struct SNode *Stack;
typedef int bool;
/*初始化堆栈*/
Stack CreateStack( int MaxSize )
{
Stack s =(Stack)malloc(sizeof(struct 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)){
return FALSE;
}else{
++S->Top;
S->Data[S->Top]=X;
return TRUE;
}
}
//非空校验
bool IsEmpty( Stack S )
{
return (S->Top == -1);
}
//弹栈操作
ElementType Pop( Stack S )
{
if(IsEmpty(S)){
return NULL;
}else{
return S->Data[S->Top--];//S->Top--;
}
}
//构建树的方法
BTree BuildTree();
//后续遍历树
void PostWalk(BTree b,EType data);
//主程序清单
int main(){
BTree root = BuildTree();
PostWalk(root,root->Data);
return 0;
}
//构建树
BTree BuildTree(){
int a;
char str[MAXSIZE];
Stack s;
ElementType root,c,tmp;
scanf("%d",&a);
getchar();
for(int i=0;i<2*a;i++){
gets(str);
if(i==0){
//初始化栈结构,并且创建二叉树树根。
s=CreateStack(MAXSIZE);
root=(ElementType)malloc(sizeof(struct TreeNode));
tmp = root;
tmp->Data = str[strlen(str)-1]-'0';//简单处理二叉树存入数字超过10要单独处理
tmp->Left = NULL;
tmp->Right = NULL;
Push(s,tmp);//
}else{
//弹栈的时候记录当前树的挂载点
if(strcmp("Pop",str)==0){
tmp = Pop(s);
}else{
c=(ElementType)malloc(sizeof(struct TreeNode));
//此处细节处理N>10会出现错误
if(strlen(str)==7)
c->Data = (str[strlen(str)-2]-'0')*10+str[strlen(str)-1]-'0';
else
c->Data = str[strlen(str)-1]-'0';
c->Left = NULL;
c->Right = NULL;
//判断当前挂载点是否已经挂载左树,确定新加入树的挂载位置
if(tmp->Left == NULL){
tmp->Left = c;
}else{
tmp->Right = c;
}
tmp = c;
Push(s,tmp);
}
}
}
return root;
}
/*后续递归调用*/
void PostWalk(BTree b,EType data){
if(b){
PostWalk(b->Left,data);
PostWalk(b->Right,data);
if(b->Data==data){//用根节点值进行判断,也可以用树的节点总数来判断
printf("%d",b->Data);
}else{
printf("%d ",b->Data);
}
}
}