03-树3 Tree Traversals Again
2016-04-21 16:44
501 查看
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
Each input file contains one test case. For each case, the first line contains a positive integer NN (\le
30≤30)
which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to NN).
Then 2N2N 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.
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.
题目思路:以先序遍历压栈,中序遍历pop,后序遍历输出
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 (\le30≤30)
which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to NN).
Then 2N2N 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 extraspace 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
题目思路:以先序遍历压栈,中序遍历pop,后序遍历输出
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MaxTree 30 #define ElementType int #define ERROR -1 #define BinTree int /*以先序遍历压栈,中序遍历pop,后序遍历输出*/ //struct TreeNode //{ // ElementType Element; // BinTree Left; // BinTree Right; //}T1[MaxTree]; //***************堆栈相关 *************************************** typedef int Position; struct SNode { ElementType *Data; /* 存储元素的数组 */ Position Top; /* 栈顶指针 */ int MaxSize; /* 堆栈最大容量 */ }; typedef struct SNode *Stack; 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("堆栈满"); 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("堆栈空"); return ERROR; /* ERROR是ElementType的特殊值,标志错误 */ } else return ( S->Data[(S->Top)--] ); } ElementType GetTopElement(Stack S){ return(S->Data[S->Top]); } //***************全局变量************************************************ int preorder[MaxTree]={0}, inorder[MaxTree]={0}, postorder[MaxTree]={0}; int pre_i = 0 , in_i = 0, post_i = 0; //*********************************************************************** //通过中序遍历和前序遍历来得到后序遍历 void GetPostOrder(int pre_i,int in_i,int post_i,int N) { if(N==0)//没有输入节点 { return; } else if(N == 1){//只有根节点 postorder[post_i+N-1] = preorder[pre_i]; return ; }//后序遍历的最后一个节点和前序遍历的第一个节点为根节点 int left,right,root; root = preorder[pre_i]; postorder[post_i+N-1] = root; for(int i = 0; i < N; i++)//通过中序遍历来找到左子树和右子树 { if(inorder[in_i+i] == root) { left = i;//left tree numnode right = N - i - 1;//right tree num node break; } } //遍历左子树和右子树 GetPostOrder(pre_i+1, in_i,post_i,left); GetPostOrder(pre_i+left+1, in_i+left+1, post_i+left,right); } //*********************************************************** int main() { int data,N; char str[30]; //freopen("test.txt", "r", stdin); scanf("%d\n",&N); Stack S1 = CreateStack(MaxTree); for(int i = 0; i < (2*N); i++ ){ scanf("%s",str); if(!strcmp(str,"Push"))//strcmp比较两个字符串,相等输出0 { scanf("%d",&data); preorder[pre_i++] = data; Push(S1,data); } else if(!strcmp(str,"Pop")) { inorder[in_i++] = Pop(S1); } } GetPostOrder(0,0,0,N); int flag = 1; for(int j = 0; j < N; j++ ){ if(flag){ flag = 0; printf("%d",postorder[j]); } else{ printf(" %d",postorder[j]); } } return 0; }
相关文章推荐
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- 正确使用Block避免Cycle Retain和Crash
- lightoj 1076 - Get the Containers 【二分 + 贪心】
- Failed to read artifact descriptor进行安装maven项目出错
- AI编辑SVG格式的相关问题
- paip.自适应网页设计 跟 响应式 设计的区别跟原理and实践总结
- 图解RAID 0, RAID 1, RAID 5, RAID 10
- go gomail
- R语言中的正则表达式(转载:http://blog.csdn.net/duqi_yc/article/details/9817243)
- kaidi中 install cuda
- stl map用法和make_pair函数
- 关于解决 Failed to prepare partial IU:
- HDU 1848 Fibonacci again and again SG函数详解!
- socket服务器开发中的SO_REUSEADDR选项与让人心烦的TIME_WAIT
- 人工智能与信息爆炸
- 关于未来的目标以及 对人工智能的一些幻想
- GoLang WaitGroup使用
- mac没有安装airport卡以及win7无法上网
- drop表提示Waiting for table metadata lock 解决
- NSBundle 的理解和 mainBundle 类方法详解,nsbundlemainbundle