C/C++ 二叉树的非递归遍历(前序、中序、后序非递归遍历)

Love The Way You Lie 2022-02-02 05:53 421阅读 0赞

二叉树的非递归遍历C/C++实现:


非递归先序遍历代码:

  1. void PreOrderTraversal (struct tree* root) { //非递归先序遍历
  2. struct tree* temp = root;
  3. while (temp != NULL || S.top > 0) {
  4. while (temp != NULL) {
  5. cout << temp->data << " ";
  6. Push(temp);
  7. temp = temp->left;
  8. }
  9. if (S.top != 0) {
  10. temp = Pop();
  11. temp = temp->right;
  12. }
  13. }
  14. cout << endl;
  15. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMTI2NDcx_size_16_color_FFFFFF_t_70

思路:例如上面这棵二叉树,前序遍历的序列为(根-左-右):5 2 3 6 11 4 8;那么首先我们访问到5时先把5输出,然后再入栈,然后再去访问它的左子结点,直到访问到3这个结点往下再没有左结点时把栈顶元素出栈,然后在访问栈顶元素的右子结点。

非递归的中序遍历和后序遍历的思路和前序遍历差不多,大家自己看代码理解领悟,多思考,多动笔画一下结点入栈时栈内元素时怎样的,这样才能更加深入的理解树的遍历。

  1. /* 完整代码 */
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <stack>
  6. using namespace std;
  7. #define ElementType string
  8. #define MaxSize 100
  9. struct tree{
  10. ElementType data;
  11. int tab;
  12. struct tree* left;
  13. struct tree* right;
  14. };
  15. typedef struct stack {
  16. struct tree* Stree[MaxSize];
  17. int top;
  18. }Stack;
  19. Stack S; //定义栈
  20. void Initialize () { //初始化栈
  21. S.top = 0;
  22. }
  23. void Push(struct tree* dummy) { //入栈
  24. S.Stree[S.top++] = dummy;
  25. }
  26. struct tree* Pop() { //出栈
  27. return S.Stree[--S.top];
  28. }
  29. bool empty() { //判断栈是否为空
  30. return S.top == 0;
  31. }
  32. struct tree* creatTree (struct tree* root) { //创建二叉树
  33. ElementType val;
  34. cin >> val;
  35. if (val == ".")
  36. return NULL;
  37. root = (struct tree*)malloc(sizeof(struct tree));
  38. root->data = val;
  39. cout << "请输入" << root->data << "的左子树:";
  40. root->left = creatTree(root->left);
  41. cout << "请输入" << root->data << "的右子树:";
  42. root->right = creatTree(root->right);
  43. return root;
  44. }
  45. void PreOrderTraversal (struct tree* root) { //非递归先序遍历
  46. struct tree* temp = root;
  47. while (temp != NULL || S.top > 0) {
  48. while (temp != NULL) {
  49. cout << temp->data << " ";
  50. Push(temp);
  51. temp = temp->left;
  52. }
  53. if (S.top != 0) {
  54. temp = Pop();
  55. temp = temp->right;
  56. }
  57. }
  58. cout << endl;
  59. }
  60. void InOrderTraversal (struct tree* root) { //非递归中序遍历
  61. struct tree* temp = root;
  62. while (temp != NULL || S.top > 0) {
  63. while (temp != NULL ) {
  64. Push(temp);
  65. temp = temp->left;
  66. }
  67. if (S.top != 0) {
  68. temp = Pop();
  69. cout << temp->data << " ";
  70. temp = temp->right;
  71. }
  72. }
  73. cout << endl;
  74. }
  75. void PostOrderTraversal (struct tree* root) { //非递归后序遍历
  76. struct tree* temp = root;
  77. while (temp != NULL || S.top > 0) {
  78. while (temp != NULL) {
  79. temp->tab = 1;
  80. Push(temp);
  81. temp = temp->left;
  82. }
  83. if (S.top > 0) {
  84. temp = Pop();
  85. if (temp->tab == 1) {
  86. temp->tab ++;
  87. Push(temp);
  88. temp = temp->right;
  89. }else {
  90. if (temp->tab == 2) {
  91. cout << temp->data << " ";
  92. temp = NULL;
  93. }
  94. }
  95. }
  96. }
  97. cout << endl;
  98. }
  99. void NotPostOrder(struct tree* root) { //非递归:后序遍历(双栈法,更简单,易理解)
  100. //思路:先放根然后放右孩子,最后放左孩子
  101. stack<struct tree*> ans;
  102. stack<struct tree*> out;
  103. struct tree* Node = root;
  104. while (Node != NULL || !ans.empty()) {
  105. if (Node != NULL) {
  106. ans.push(Node);
  107. out.push(Node);
  108. Node = Node->right;
  109. }else {
  110. Node = ans.top();
  111. Node = Node->left;
  112. ans.pop();
  113. }
  114. }
  115. while (!out.empty()) {
  116. cout << out.top()->val << " ";
  117. out.pop();
  118. }
  119. }
  120. int main() {
  121. cout << "请输入头节点:";
  122. struct tree* root = creatTree(root);
  123. Initialize();
  124. PreOrderTraversal(root);
  125. InOrderTraversal(root);
  126. PostOrderTraversal(root);
  127. return 0;
  128. }

发表评论

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

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

相关阅读