浙江大学数据结构 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

思路:

这个题目的意思是给出一个二叉树的中序遍历的非递归操作,要求输出后序遍历的访问的顺序
由于题目指出6-Node binary tree(with the keys numbered from 1 to 6)
证明每个结点的数据是不相同的,而且是从1~n的顺序
这样我们采用定义树结点的结构数组,来储存结点的左子树 右子树以及父结点的信息

struct TreeNode{
	int father;
	int left;
	int right;
	TreeNode(){
		father=-1;
		left=-1;
		right=-1;
	}
}T[50];

在逐行读入输入时,如果读到Push,证明该点压入堆栈**,由于是中序遍历,在输入/输出右子树的所有信息之前,结点信息已经被先输出了**,因此这里只需要判断是否结点还在stack中(这里建立stack模拟这个过程,建立哈希表来判断是否结点还在stack中)
相同的原理,在读取pop时,如果该结点的信息已经输出了(即hash[i]==0),那么说明说明此时正在输出右子树,反之在输出左子树
这样还原出这个树
值得注意的是,在pop的过程中如果不存在右子树,那么会向上回到碰到的第一个未输出的祖先结点那里去,但是必须判定到达根结点就结束
后面就是对这个树进行一次递归后序遍历即可

AC代码

#include<stack>
#include<iostream>
#include<cstring>
using namespace std;
struct TreeNode{
	int father;
	int left;
	int right;
	TreeNode(){
		father=-1;
		left=-1;
		right=-1;
	}
}T[40];
int cnt=0;
const int null=-1;
int BuildTree(struct TreeNode T[]){
	string s;
	stack<int> st;
	int n,r=null,hash[40];
	memset(hash,0,sizeof(hash));
	string push="Push"; string pop="Pop";	
	int m,thisnode;
	cin >> n;
	for (int i=0;i<2*n;i++){
		cin >> s;
		if(s==push){
			cin >> m;
			hash[m]=1;
			st.push(m);			
			if (r==null){  //代表根结点 
				r=m;
				thisnode=m; 
			}
			else{   //不是根节点 
				T[m].father=thisnode;
				if (hash[thisnode])	T[thisnode].left=m;
				else T[thisnode].right=m;
				thisnode=m;
			}
		}
		if (s==pop){		
			if (hash[thisnode]){  //如果thisnode的hash对应是1证明还未弹出,此时讨论的是左子树 
				T[thisnode].left=null;
			}
			else{
				T[thisnode].right=null;
				thisnode=T[thisnode].father;
				//右子树讨论结束后
				while(!hash[thisnode]&&hash[thisnode]!=r){
					thisnode=T[thisnode].father;
				}
			}
			if (!st.empty()){
				int x=st.top();
				hash[x]=0;
				st.pop();
			}
		}
	}
	return r;	
};
void PostOrderTraversal(struct TreeNode T[],int r){
	if (r!=null){
		PostOrderTraversal(T,T[r].left);
		PostOrderTraversal(T,T[r].right);
		if (!cnt){      //这里的cnt是全局变量,否则每一次的递归调用都是新的函数,对应的cnt都是定义的初始值0,就不会输出空格
			cout << r;
			cnt++;
		}
		else cout << " " << r;
	}
};
int main(){
	int r=BuildTree(T);
	PostOrderTraversal(T,r);
	return 0;	
}

测试点

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值