当前位置:   article > 正文

二叉树的先序、中序、后序遍历_二叉树的先序,中序,后序遍历

二叉树的先序,中序,后序遍历

二叉树的遍历是数据结构与算法非常重要也是非常基础的内容,首先讲什么是二叉树的先序、中序以及后序遍历:                 先序、中序、后序遍历其实都是对于根节点来说的。                                                                                                       

先序遍历:其实就是对于一棵树先遍历根节点,再遍历左右孩子;中序遍历:先遍历左孩子,再遍历根节点,再遍历右孩子;后序遍历:先遍历左孩子节点,再遍历右孩子节点,再遍历左孩子节点。

如图所示:


如上图二叉树所示(咳咳,图丑了一点,不过问题不大~):

先序遍历结果:5783649

中序遍历结果:8735469

后序遍历结果:8374965

下面来看先、中、后序的递归以及非递归实现:

  1. import java.util.Stack;
  2. /**
  3. * 二叉树的先序、中序、后序遍历
  4. * 先序:先根节点再左孩子再右孩子;中序:先左孩子再根节点再右孩子;后序:先左孩子、载右孩子、再根节点(先序中序后序都是针对根节点来说的)
  5. * @author zhmm
  6. *
  7. */
  8. public class Code_05_PreInPosTraversal {
  9. public static class Node {
  10. public int value;
  11. public Node left;
  12. public Node right;
  13. public Node(int data) {
  14. this.value = data;
  15. }
  16. }
  17. //二叉树先序遍历打印递归版
  18. public static void preOrderRecur(Node head) {
  19. if (head == null) {
  20. return;
  21. }
  22. System.out.print(head.value + " ");
  23. preOrderRecur(head.left);
  24. preOrderRecur(head.right);
  25. }
  26. //二叉树中序打印递归版
  27. public static void inOrderRecur(Node head) {
  28. if (head == null) {
  29. return;
  30. }
  31. inOrderRecur(head.left);
  32. System.out.print(head.value + " ");
  33. inOrderRecur(head.right);
  34. }
  35. //二叉树后序打印递归版
  36. public static void posOrderRecur(Node head) {
  37. if (head == null) {
  38. return;
  39. }
  40. posOrderRecur(head.left);
  41. posOrderRecur(head.right);
  42. System.out.print(head.value + " ");
  43. }
  44. //二叉树先序遍历打印非递归版
  45. //使用栈实现
  46. public static void preOrderUnRecur(Node head) {
  47. System.out.print("pre-order: ");
  48. if (head != null) {
  49. Stack<Node> stack = new Stack<Node>();
  50. stack.add(head);
  51. while (!stack.isEmpty()) {
  52. //根节点弹出即打印
  53. head = stack.pop();
  54. System.out.print(head.value + " ");
  55. //孩子节点先放右孩子再放左孩子
  56. if (head.right != null) {
  57. stack.push(head.right);
  58. }
  59. if (head.left != null) {
  60. stack.push(head.left);
  61. }
  62. }
  63. }
  64. System.out.println();
  65. }
  66. //二叉树中序遍历非递归版
  67. //也是利用了栈这个数据结构,首先把这棵树的所有左边界全部压进栈里,当弄个左边界的左子树为空时,弹出并打印该节点并判断该节点的右子树是否为空
  68. public static void inOrderUnRecur(Node head) {
  69. System.out.print("in-order: ");
  70. if (head != null) {
  71. Stack<Node> stack = new Stack<Node>();
  72. while (!stack.isEmpty() || head != null) {
  73. if (head != null) {
  74. stack.push(head);
  75. head = head.left;
  76. } else {
  77. head = stack.pop();
  78. System.out.print(head.value + " ");
  79. head = head.right;
  80. }
  81. }
  82. }
  83. System.out.println();
  84. }
  85. //二叉树后序遍历非递归版
  86. //这个非常简单,因为要实现打印的方式为左右根的话,因为先序是根左右,我们只需要先按先序一样操作,只需要在压栈的时候,先序是先压右再压左,我们先压左再压右,这样就能够
  87. //达到这次压栈压的为根右左,借助一个help栈,把上面栈的元素全部放在help栈里,然后弹出得到的就是一个弹出顺序为左右根的顺序的数据
  88. public static void posOrderUnRecur1(Node head) {
  89. System.out.print("pos-order: ");
  90. if (head != null) {
  91. Stack<Node> s1 = new Stack<Node>();
  92. Stack<Node> s2 = new Stack<Node>();
  93. s1.push(head);
  94. while (!s1.isEmpty()) {
  95. head = s1.pop();
  96. s2.push(head);
  97. if (head.left != null) {
  98. s1.push(head.left);
  99. }
  100. if (head.right != null) {
  101. s1.push(head.right);
  102. }
  103. }
  104. while (!s2.isEmpty()) {
  105. System.out.print(s2.pop().value + " ");
  106. }
  107. }
  108. System.out.println();
  109. }
  110. public static void posOrderUnRecur2(Node h) {
  111. System.out.print("pos-order: ");
  112. if (h != null) {
  113. Stack<Node> stack = new Stack<Node>();
  114. stack.push(h);
  115. Node c = null;
  116. while (!stack.isEmpty()) {
  117. c = stack.peek();
  118. if (c.left != null && h != c.left && h != c.right) {
  119. stack.push(c.left);
  120. } else if (c.right != null && h != c.right) {
  121. stack.push(c.right);
  122. } else {
  123. System.out.print(stack.pop().value + " ");
  124. h = c;
  125. }
  126. }
  127. }
  128. System.out.println();
  129. }
  130. public static void main(String[] args) {
  131. Node head = new Node(5);
  132. head.left = new Node(3);
  133. head.right = new Node(8);
  134. head.left.left = new Node(2);
  135. head.left.right = new Node(4);
  136. head.left.left.left = new Node(1);
  137. head.right.left = new Node(7);
  138. head.right.left.left = new Node(6);
  139. head.right.right = new Node(10);
  140. head.right.right.left = new Node(9);
  141. head.right.right.right = new Node(11);
  142. // recursive
  143. System.out.println("==============recursive==============");
  144. System.out.print("pre-order: ");
  145. preOrderRecur(head);
  146. System.out.println();
  147. System.out.print("in-order: ");
  148. inOrderRecur(head);
  149. System.out.println();
  150. System.out.print("pos-order: ");
  151. posOrderRecur(head);
  152. System.out.println();
  153. // unrecursive
  154. System.out.println("============unrecursive=============");
  155. preOrderUnRecur(head);
  156. inOrderUnRecur(head);
  157. posOrderUnRecur1(head);
  158. posOrderUnRecur2(head);
  159. }
  160. }

运行结果:

==============recursive==============
pre-order: 5 3 2 1 4 8 7 6 10 9 11 
in-order: 1 2 3 4 5 6 7 8 9 10 11 
pos-order: 1 2 4 3 6 7 9 11 10 8 5 
============unrecursive=============
pre-order: 5 3 2 1 4 8 7 6 10 9 11 
in-order: 1 2 3 4 5 6 7 8 9 10 11 
pos-order: 1 2 4 3 6 7 9 11 10 8 5 
pos-order: 1 2 4 3 6 7 9 11 10 8 5 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/625801
推荐阅读
相关标签
  

闽ICP备14008679号