《数据结构》03-树3 Tree Traversals Again

ゞ 浴缸里的玫瑰 2022-05-15 15:58 278阅读 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.
20181030171007780.jpg
F i g u r e 1 Figure 1 Figure1
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

分析

题目大意就是中序非递归遍历需要用到一个栈,给定针对这个栈的操作,就可以唯一确定一个树,我们的任务就是后序遍历这棵树
回忆一下如何利用栈进行中序遍历,首先根入栈,其左结点循环入栈,如果此时栈不空,出栈栈顶结点并输出,再让栈顶结点等于其右子结点,直到整个栈为空且结点为空
所以对于 push 操作要做的事有两种可能:

  1. 入栈当前结点的左儿子结点
  2. 入栈当前结点的右儿子结点

好像说了废话,想想非递归遍历的操作,如果左儿子结点一直都有,那么入栈的一直是左儿子结点,只有当左儿子结点已经没有了,才”勉为其难”入栈右儿子结点。
而对于 pop 操作要做的事也有两种可能:

  1. 当前结点的左儿子结点为空时出栈
  2. 当前结点的右儿子结点为空时出栈

抱歉又说了废话…其实也不是,每次 push 后第一个 pop 时肯定代表着当前结点的左儿子为空,其后的 pop 代表着当前右儿子为空(想一想为什么?)
大概轮廓有了,剩下的就是细节了,最麻烦的一个就是,如何找到”当前结点”
当 pop 时,出栈的栈顶结点肯定是”当前结点”了,
而当 push 时,入栈的结点肯定是”当前结点”啦

还原出了树用递归实现后序遍历,(毕竟非递归有点麻烦)

  1. #include<iostream>
  2. #include<stack>
  3. #include<string>
  4. #include<stdio.h>
  5. #include<malloc.h>
  6. using namespace std;
  7. typedef struct TreeNode *Tree;
  8. struct TreeNode{
  9. string data;
  10. Tree left; // 左子树
  11. Tree right; // 右子树
  12. };
  13. // 初始化一个树结点
  14. Tree create(){
  15. Tree T;
  16. T = (Tree)malloc(sizeof(struct TreeNode));
  17. T->left = NULL;
  18. T->right = NULL;
  19. return T;
  20. }
  21. // 根据中序遍历整理出这棵树
  22. Tree restore(Tree T){
  23. int n;
  24. string str;
  25. stack<Tree> s;
  26. Tree node = T;
  27. bool flag = false;
  28. string value;
  29. scanf("%d\n",&n);
  30. // 根节点赋值
  31. getline(cin,str);
  32. value = str.substr(5); // 从第五个开始截取
  33. node->data = value;
  34. // 根结点入栈
  35. s.push(node);
  36. for(int i=1;i<2*n;i++){
  37. getline(cin,str);
  38. if(str=="Pop"){
  39. // 如果是 pop 操作
  40. node = s.top();
  41. s.pop();
  42. }else{
  43. // push
  44. value = str.substr(5); // 从第五个开始截取
  45. Tree tmp = create();
  46. tmp->data = value;
  47. if(!node->left){
  48. // 如果左儿子空,新结点就是左儿子
  49. node->left = tmp;
  50. node = node->left;
  51. }else if(!node->right){
  52. // 如果右儿子空,新结点就是右儿子
  53. node->right = tmp;
  54. node = node->right;
  55. }
  56. s.push(tmp);
  57. }
  58. }
  59. return T;
  60. }
  61. // 后序递归遍历
  62. void bl(Tree T,bool &flag){
  63. if(T){
  64. bl(T->left,flag);
  65. bl(T->right,flag);
  66. if(!flag)
  67. flag = true;
  68. else
  69. cout<<" ";
  70. cout<<T->data;
  71. }
  72. }
  73. int main(){
  74. Tree T;
  75. bool flag = false;
  76. string str;
  77. T = create();
  78. T = restore(T);
  79. bl(T,flag);
  80. return 0;
  81. }

发表评论

表情:
评论列表 (有 0 条评论,278人围观)

还没有评论,来说两句吧...

相关阅读

    相关 数据结构(Tree)详解

    树型结构是一类重要的非线性数据结构。其中以树和二叉树最为常用,直观看来,树是以分支关系定义的层次结构。树结构在客观世界中广泛存在,如人类社会的族谱和各种社会组织机构可用树来形象

    相关 数据结构-(tree)

    本篇文章先介绍关于树的一些基础概念。 常见的数组、链表、栈和队列都是线性结构,在存储大量数据时访问速度比较慢,而树(tree)则是一种非线性结构,使得访问时间复杂度降低到O(