MOOC資料結構PTA-03-樹3 Tree Traversals Again
題目表述
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
結尾無空行
題目解析
本題借鑑了一些演算法:通過輸入可以構建前序遍歷陣列,和中序遍歷陣列,然後通過遞迴,根據前項遍歷陣列和中序遍歷陣列得出後續遍歷陣列。
1.首先棧push的過程即是前序遍歷的陣列;
pop的過程即是中序遍歷的陣列。
2.後序遍歷:將前序的第一個放到後序的最後一個,然後通過中序得到左節點數L和右節點數R,將前序的下一個放到後序陣列的左邊的最後一個,右邊同,然後遞迴實現。(每次遞迴前序陣列後移一個,中序用來找左數L和右數R,將前序的本節點賦值給後序陣列的L或R個元素的最後一個)
程式碼
// Tree traversal again
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max 30
int pre[Max], in[Max], post[Max];
typedef struct node {
int data;
struct node* next;
} Node, *Stack;
typedef struct tnode {
int data;
struct tnode *lchild, *rchild;
} TNode, *Tree;
//函式宣告
Stack Init_stack();
void push(Stack S, int x);
bool pop(Stack S, int* x);
bool isempty(Stack S);
void buildTree(int N);
void PostTree(int pre_r, int in_r, int post_r, int N);
//主函式
int main() {
int N;
scanf("%d", &N);
buildTree(N);
PostTree(0, 0, 0, N);
for (int i = 0; i < N; i++) {
printf("%d", post[i]);
if (i < N - 1)
putchar(' ');
}
return 0;
}
//函式定義
Stack Init_stack() {
Stack p = (Stack)malloc(sizeof(Node));
p->next = NULL;
return p;
}
void push(Stack S, int x) {
Node* p = (Node*)malloc(sizeof(Node));
p->data = x;
p->next = S->next;
S->next = p;
}
bool pop(Stack S, int* x) {
if (isempty(S))
return false;
Node* p = S->next;
*x = p->data;
S->next = p->next;
free(p);
return true;
}
bool isempty(Stack S) {
if (S->next == NULL)
return true;
else
return false;
}
void buildTree(int N) {
Stack S = Init_stack();
int index_in = 0; //中序陣列角標
int index_pre = 0; //前序陣列角標
for (int i = 0; i < 2 * N; i++) {
char order[5];
int data;
scanf("%s", order);
if (strcmp(order, "Push") == 0) {
scanf("%d", &data);
pre[index_pre++] = data;
push(S, data);
} else {
pop(S, in + index_in);
index_in++;
}
}
return;
}
void PostTree(int pre_r, int in_r, int post_r, int N) {
if (N == 0)
return;
if (N == 1)
post[post_r] = pre[pre_r];
int i;
int root = pre[pre_r];
post[post_r + N - 1] = root;
for (i = 0; i < N; i++) {
if (in[in_r + i] == root)
break;
}
int L = i, R = N - i - 1;
PostTree(pre_r + 1, in_r, post_r, L);
PostTree(pre_r + 1 + L, in_r + L + 1, post_r + L, R);
}