7-5 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.
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
stack.h
#include <stdbool.h>
#include <malloc.h>
#define ERROR -1
typedef int ElementType;
typedef int Position;
struct SNode {
ElementType *Data; /* 存儲元素的數組 */
Position Top; /* 棧頂指針 */
int MaxSize; /* 堆棧最大容量 */
};
typedef struct SNode* Stack;
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->Data[++(S->Top)] = X;
return true;
}
}
bool IsEmpty( Stack S )
{
return (S->Top == -1);
}
ElementType Pop( Stack S )
{
if ( IsEmpty(S) ) {
return ERROR; /* ERROR是ElementType的特殊值,標誌錯誤 */
}
else
return ( S->Data[(S->Top)--] );
}
ElementType Top(Stack S){
if(IsEmpty(S)){
return ERROR;
}
else
return S->Data[S->Top];
}
main.h
函數第二層if分支是遞歸過程
#include <stdio.h>
#include <string.h>
//#include "stack.h"
#define MaxNum 30
int IsFirst = 1;
void f(int A[],int lA,int rA,int B[],int lB,int rB){
int i;
if((rA==-1)||(rB==-1)||(rA-lA)!=(rB-lB)) //序列爲空或不一樣長
{
//printf("錯誤序列\n");
return;
}
if(lA==rA&&lB==rB){ //序列都只有一個元素
if(A[lA]!=B[lB]){
//printf("錯誤序列\n");
return;
}
if(IsFirst==1){
printf("%d",A[lA]);
IsFirst = 0;
}
else{
printf(" %d",A[lA]);
}
}
else{
for(i=lB;i<=rB;i++){
if(A[lA]==B[i]){
break;
}
else if(i==rB){
//printf("錯誤序列\n");
return;
}
}
if(i==lB){
f(A,lA+1,rA,B,lB+1,rB);
if(IsFirst==1){
printf("%d",B[i]);
IsFirst = 0;
}
else{
printf(" %d",B[i]);
}
}
if(i>lB&&i<rB){
f(A,lA+1,lA+i-lB,B,lB,i-1);
f(A,lA+i-lB+1,rA,B,i+1,rB);
if(IsFirst==1){
printf("%d",B[i]);
IsFirst = 0;
}
else{
printf(" %d",B[i]);
}
}
if(i==rB){
f(A,lA+1,rA,B,lB,rB-1);
if(IsFirst==1){
printf("%d",B[i]);
IsFirst = 0;
}
else{
printf(" %d",B[i]);
}
}
}
}
int main(){
//freopen("1.txt","r",stdin);
int N,x,i_pre=0,i_in=0;
char c[5];
int pre_list[MaxNum];
int in_list[MaxNum];
scanf("%d\n",&N);
Stack s = CreateStack(N);
for(int i=0;i<2*N;i++){
scanf("%s",c);
if(strcmp(c,"Push")==0){
scanf("%d\n",&x);
Push(s,x);
pre_list[i_pre++] = x;
}
else{
in_list[i_in++] = Pop(s);
getchar();
}
}
f(pre_list,0,N-1,in_list,0,N-1);
return 0;
}