数据结构PTA习题:03-树3 Tree Traversals Again (25分)

03-树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.
在这里插入图片描述

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

C语言程序代码:

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node
{
 int data;
 struct Node * left;
 struct Node * right;
};
typedef struct Node * Bintree;
struct stack
{
 Bintree * data;
 int top;
 int maxsize;
};
typedef struct stack * Stack;
Stack Create();
void Push(Bintree B, Stack S);
Bintree Pop(Stack S);
void postorder(Bintree B);//后序遍历
int first = 1;//第一个输出前不带空格

int main()
{
 int n;
 scanf("%d\n", &n);
 struct Node * T;
 int i = 0;
 char a[5];
 int num;
 Stack S;
 S = Create();
 Bintree B, p;
 B = (Bintree)malloc(sizeof(struct Node));
 scanf("%s", a); //第一个输入的根结点
 if (strcmp(a, "Push") == 0)
 {
  scanf("%d\n", &num);
  B->data = num;
  B->left = B->right = NULL;
  Push(B, S);
 }
 T = B;
 int flag = 0;
 for (i = 1; i < 2 * n; i++) //输入2n行
 {
  scanf("%s", a);
  if (strcmp(a, "Push") == 0)
  {
   //两次Pop后跟的Push,此时T需要沿用上一次弹出的父节点,不能重新弹出,通过flag实现
   if (flag == 0) { T = Pop(S); }//从堆栈中弹出父结点
   if (T->left == NULL)//结点左孩子为空则输入做左孩子
   {
    scanf("%d\n", &num);
    p = (Bintree)malloc(sizeof(struct Node));
    p->data = num;
    p->left = p->right = NULL;
    T->left = p;
    if (flag == 0) { Push(T, S); }//父结点压栈
    Push(p, S);//左孩子结点压栈
   }
   else//结点右孩子为空则输入做右孩子
   {
    scanf("%d\n", &num);
    p = (Bintree)malloc(sizeof(struct Node));
    p->data = num;
    p->left = p->right = NULL;
    T->right = p;
    if (flag == 0) { Push(T, S); }//父结点压栈
    Push(p, S);//右孩子结点压栈
   }
   flag = 0;
  }
  else if (strcmp(a, "Pop") == 0)
  {
   T = Pop(S);
   flag = 1;
  }
 }
 postorder(B);
 return 0;
}

//递归实现后序遍历输出
void postorder(Bintree B)
{
 if (B)
 {
  postorder(B->left);
  postorder(B->right);
  if (first == 0) {
   printf(" %d", B->data);
  }
  else { printf("%d", B->data); first = 0; }
 }
}

//创建堆栈
Stack Create()
{
 Stack S;
 S = (Stack)malloc(sizeof(struct stack));
 S->data = (Bintree *)malloc(30 * sizeof(Bintree));
 int i;
 for (i = 0; i < 30; i++)
 {
  S->data[i] = (Bintree)malloc(sizeof(struct Node));
 }
 S->top = -1;
 S->maxsize = 30;
 return S;
}

//压栈
void Push(Bintree B, Stack S)
{
 S->top++;
 S->data[S->top] = B;
}
//出栈
Bintree Pop(Stack S)
{
 return S->data[S->top--];
}
发布了21 篇原创文章 · 获赞 2 · 访问量 1633

猜你喜欢

转载自blog.csdn.net/wulila/article/details/105296546