Tree Traversals Again(PAT)

1.题目描述

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.
在这里插入图片描述
无序二叉树遍历可以使用堆栈以非递归方式实现。例如,假设当遍历一个6节点二叉树(键编号从1到6)时,堆栈操作是:Push(1);Push(2);Push(3);POP();POP();Push(4);POP();POP(5);Push(6);POP();POP()。然后,可以从这个操作序列生成唯一的二叉树(如图1所示)。您的任务是给出此树的后继遍历序列。

2.输入描述:

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.
每个输入文件包含一个测试用例。对于每一种情况,第一行包含一个正整数N(<=30),它是一棵树中的节点总数(因此节点编号从1到N)。接下来是2N行,每一行描述一个格式的堆栈操作:“Push X”,其中X是被推到堆栈上的节点的索引;或者“Pop”表示从堆栈中弹出一个节点。

3.输出描述:

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.
对于每个测试用例,在一行中打印对应树的后继遍历序列。一个解决方案是肯定存在的。所有的数字都必须用一个空格隔开,并且在行的末尾不能有额外的空格。

4.输入例子:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

5.输出例子:

3 4 2 6 5 1

6.源代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct BiTNode
{
	int date;
	struct BiTNode *Lchild,*Rchild;
}BiTNode, *BiTree;//二叉树结构

typedef struct StackNode
{
	BiTree Tree;
	struct StackNode *next;
}StackNode, *LinkStack;//栈结构

void InitStack(LinkStack &S)//初始化栈
{
	S = NULL;
}

void Push(LinkStack &S, BiTree e)//入栈
{
	LinkStack p;
	p = (StackNode *)malloc(sizeof(StackNode));
	p->Tree = e;
	p->next = S;
	S = p;
}

void Pop(LinkStack &S, BiTree &e)//出栈
{
	LinkStack p;
	if(S)
	{
		e = S->Tree;
		p = S;
		S = S->next;
		free(p);
	}
}


void CreateBiTree(BiTree &T)//创建二叉树
{
	T = NULL;	//初始化空二叉树
	LinkStack S;
	InitStack(S);//初始化一个栈

	char str[5];
	int i, N, data, flag = 1;
	scanf("%d", &N);

	BiTree p, q;
	for(i = 0; i < 2 * N; i++)
	{
		scanf("%s", str);
		if(strcmp(str,"Push") == 0)//入栈
		{
			scanf("%d", &data);
			
			BiTree s;//定义二叉树结点
			s = (BiTNode *)malloc(sizeof(BiTNode));//结点分配空间
			s->date = data;		//结点数据
			s->Lchild = NULL;	//初始化
			s->Rchild = NULL;	//初始化

			if(!T)	//二叉树根结点
			{
				T = s;
				p = T;
			}
			else
			{	
				if(flag)//前序连接左子结点
				{
					p->Lchild = s;
					p = s;
				}
				else	//中序连接右子结点
				{
					p->Rchild = s;
					p = s;
					flag = 1;
				}
			}			
			Push(S, p);
		}
		else	//出栈
		{
			Pop(S, q);
			p = q;		//中序回溯到上一个结点
			flag = 0;
		}
	}
}

void PostOrderTraverse(BiTree T)//非递归遍历二叉树
{
	LinkStack S;
	InitStack(S);
	
	BiTree p = T, q, top, flag = T;
	while(p || S)
	{
		while(p)
		{
			Push(S, p);		//入栈
			p = p->Lchild;	//遍历左子树
		}
		if(S)
		{
			top = S->Tree;	//回溯到上一个结点
			if(top->Rchild && top->Rchild != flag)
				p = top->Rchild;//遍历右子树
			else
			{
				printf("%d", top->date);
				if(top != T)
					printf(" ");
				flag = top;	//设置哨站判断右子树是否已经被遍历到
				Pop(S, q);	//出栈
			}
		}
	}
}

int main()
{
	BiTree T;

	CreateBiTree(T);

	PostOrderTraverse(T);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小-黯

免费的,尽力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值