3 Tree Traversals Again (25 分)
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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int Position;
typedef int ElementType;
typedef struct SNode* PtrToSNode;
struct SNode {
ElementType* Data;
Position Top;
int MaxSize;
};
typedef PtrToSNode Stack;
int IsFull(Stack a);
Stack CreateSNode(int Size);
bool Push(Stack a, ElementType n);
ElementType Pop(Stack a);
void Read(int n, int* Preorder, int* inorder, Stack a);
void GetPostOrder(int PreL, int InL, int PostL, int* PreOrder, int* Inorder, int* PostOrder, int n);
int main()
{
int n;
scanf("%d", &n);
int* Preorder = (int*)malloc(sizeof(int) * n);
int* Inorder = (int*)malloc(sizeof(int) * n);
int* Postorder = (int*)malloc(sizeof(int) * n);
Stack a = CreateSNode(n);
Read(n, Preorder, Inorder,a);
GetPostOrder(0, 0, 0, Preorder, Inorder, Postorder,n);
for (int i = 0; i < n-1; i++)
printf("%d ", Postorder[i]);
printf("%d", Postorder[n-1]);
}
Stack CreateSNode(int Size)
{
Stack a = (Stack)malloc(sizeof(struct SNode));
a->Data = (ElementType*)malloc(sizeof(ElementType) * Size);
a->Top = -1;//栈为空
a->MaxSize = Size;
return a;
}
int IsFull(Stack a)
{
return(a->Top == (a->MaxSize-1));
}
bool Push(Stack a,ElementType n)
{
if (!IsFull(a))
{
a->Data[++(a->Top)] = n;
return true;
}
else
{
printf("Stack is full\n");
return false;
}
}
bool IsEmpty(Stack a)
{
return(a->Top == -1);
}
ElementType Pop(Stack a)
{
if (!IsEmpty(a))
{
return(a->Data[(a->Top)--]);
}
else
{
printf("Stack is empty!\n");
return -1;//-1不放进去
}
}
void Read(int n, int* Preorder, int* inorder, Stack a)
{
int i=0;//pop次数
int r = 0;
char str[5];
int number;
while (i < n)
{
scanf("%s", str);
if (str[1] == 'u')//如果是Push
{
scanf("%d", &number);
Push(a, number);
*(Preorder + r) = number;
r++;
}
else//Pop
{
*(inorder + i) = Pop(a);
i++;
}
}
}
void GetPostOrder(int PreL, int InL, int PostL, int* PreOrder, int* Inorder, int* PostOrder, int n)//前中后位置,数组,个数
{
if (n == 0)
return;
if (n == 1)
{
PostOrder[PostL]=PreOrder[PreL];
return;
}
int L; int R;//左右元素
int Root = PreOrder[PreL];
int i;//找到根在中序数组中的位置
PostOrder[PostL+n-1] = Root;
for (i = 0; i < n; i++)
if (Inorder[i+InL] == Root)
break;
L = i;
R = n - L - 1;
GetPostOrder(PreL + 1, InL, PostL, PreOrder,Inorder,PostOrder,L);
GetPostOrder(PreL + L + 1, InL + L + 1, PostL + L, PreOrder, Inorder, PostOrder, R);
}
收获了:递归和栈之间的关系
其实我想到了怎么用前序遍历的数组和中序遍历的数组去生成后序遍历的数组,以及递归的方法
但是我搞不清应该向函数输入什么以及递归应该怎么写