MOOC数据结构课程 题集08 Tree Traversals Again()

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.


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

这题有点难度,要求根据入栈出栈的顺序来得到后序遍历,不过一旦理解题目就好做了

入栈的顺序是先序,出栈的顺序是后序

有了先序后序就能建立一个树,然后输出后序即可,基本思路就是先序的第一个节点是root,然后根据root把中序分成 两部分。

再根据这两部分确定先序的左右子树然后从中找root,再去分隔中序。

按照这个思路可以做,我是这么做的,但是去看了课程给的解答,他们的更加简洁

因为先序的root实际上就是后序遍历的最后一个节点,所以根据上面的方式找root,但是不用建树,而是直接放到最后即可。

下面是用建树的方式

#include <iostream>
#include <string>

#define Tree int
#define MAX_SIZE 30

using namespace std;

Tree stack[MAX_SIZE + 1], pre_order[MAX_SIZE + 2], mid_order[MAX_SIZE + 1];

void print_post_order(Tree root, Tree left_boundary, Tree right_boundary);

int main()
{
	int N, n;
	Tree node;
	int top = 0, pre_rear = 0,  mid_rear = 0;
	string opt;

	//读入数据,将其分为先序与中序
	cin >> N;
	n = N * 2;
	for (int i = 0; i < n; i++)
	{
		cin >> opt;
		if (opt == "Push")
		{
			cin >> node;
			pre_order[pre_rear++] = node;	//push的顺序是先序
			stack[top++] = node;			//入栈,等待pop
		}
		else if (opt == "Pop")
		{
			if (top != 0) {
				mid_order[mid_rear++] = stack[--top];//pop的顺序是中序
			}
			else cout << "error 01"<< endl;		//错误1,空栈出栈,表明push,pop的顺序有误
		}
		else cout << "error 02" << endl;		//错误2,既不是push也不是pop,输入的格式有误
	}

	//处理数据,输出后序
	print_post_order(0, 0, N-1);

	return 0;
}

void print_post_order(Tree pre_root, Tree left_boundary, Tree right_boundary)
{
	bool is_find = 0; //是否找到根
	if (left_boundary == right_boundary)		//左右边界相等,说明是叶子节点
	{
		cout << pre_order[pre_root];
		if (pre_order[pre_root] != pre_order[0])
			cout << " ";
		return ;
	}
	if (left_boundary > right_boundary) return; //左右边界交错,说明是空节点

	for (int mid_root = left_boundary; mid_root <= right_boundary; mid_root++)	//根据先序提供的根,寻找其在中序的位置
	{
		if (mid_order[mid_root] == pre_order[pre_root])
		{
			is_find = 1;
			print_post_order(pre_root + 1, left_boundary, mid_root-1);									//递归寻找左半边
			print_post_order(pre_root + 1 + (mid_root - left_boundary), mid_root+1, right_boundary);	//递归寻找右半边
			break;
		}
	}
	if (is_find != 0) {					//左右递归完成,再输出根节点
		cout << pre_order[pre_root];		
		if (pre_order[pre_root] != pre_order[0])
			cout << " ";
	}else cout << "error 03" << endl; //错误3,未在中序中找到先序提供的根
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值