03-树3 Tree Traversals Again   (25分)

7 篇文章 0 订阅
2 篇文章 0 订阅

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 NN (30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to NN). 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

思路:

1、入栈过程是树的先序遍历,出栈过程是树的中序遍历

2、已知先序和中序,求后序;

参考:已知前序(先序)与中序输出后序

#include <stdio.h>
#include <stdlib.h>
int *pre, *in;
int flag = 0;
//栈的结构数组表示
#define Error -1;
typedef int Position;
typedef int ElementType;
typedef struct SNode *Stack;
struct SNode {
	ElementType * Data;
	Position Top;
	int MaxSize;
};
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) ) {
		printf("stack is full");
		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) ){
		printf("stack is empty");
		return Error;
	}
	else {
		return S->Data[S->Top--];
	}
}
void StackClear( Stack S ){
	S->Top = -1;
}

void Post(int root, int start, int end){
	//当start = end时,还可以继续做下去。(具体细节未细究)
	if(start > end) return ;

	//i - start 是左子树的结点个数,当start = 0, i是左子树结点个数
	int i = start;
	while(i < end && in[i] != pre[root]) i++; 
	//试下i <= end; 结果:可以。
	//原因:因为一定会有根,当i = end时,后面的条件仍会使循环结束
	//(所以也就是前面的条件使循环结束,还是后面的条件使循环结束的问题了)

	
	//start和end 是按 in 中的下标确定的, root是由 pre 中的下标确定(即 pre 的作用是 “寻根”)
	//左子树递归;
	//root + 1 是左子树的根(根左右),
	//i是根, i-1就是左子树的最后一个元素(左根右)
	Post( root + 1, start, i - 1 );

	//右子树递归
	//root + 1 + i - start 是右子树的根(根左右),因此要加上 i - start(左子树的结点个数)
	//i是根, i+1就是右子树的第一个元素(左根右)
	Post( root + 1 + i - start, i + 1, end); 

	if(!flag){
		printf("%d", pre[root]);
		flag = 1;
	}
	else 
		printf(" %d", pre[root]);
}
int main(){
	int N, i = 0, j = 0, val;
	char s[10];
	Stack S;
	scanf("%d", &N);
	S = CreateStack(N);
	pre = (int *)malloc(N * sizeof(int));
	in = (int *)malloc(N * sizeof(int));
	for(int k = 0; k < 2 * N; k++){    //不能用i,会和if语句中的i混淆,错误原因
		scanf("%s", s);
		//printf("%s\n", s);
		if( s[1] == 'u' ){
			scanf("%d", &val);
			pre[i++] = val;
			Push(S, val);
		}
		else{
			in[j++] = Pop(S);
		}
	}
	/*for(int i = 0; i < N; i++)
		printf("%d ", pre[i]);
	printf("\n");
	for(int i = 0; i < N; i++)
		printf("%d ", in[i]);*/
	Post(0, 0, N - 1);
	system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值