二叉树的三种遍历递归和非递归实现

摘自《数据结构C++版》,用java实现:
  1. package JobInterview_01;
  2. import java.util.Stack;
  3. public class BinaryRecursive {
  4. public static class Node {
  5. public int value;
  6. public Node left;
  7. public Node right;
  8. public Node(int data) {
  9. this.value = data;
  10. }
  11. }
  12. // 前序遍历
  13. public static void preOrderRecur(Node head) {
  14. if (head == null) {
  15. return;
  16. }
  17. System.out.print(head.value + " ");
  18. preOrderRecur(head.left);
  19. preOrderRecur(head.right);
  20. }
  21. // 中序遍历
  22. public static void inOrderRecur(Node head) {
  23. if (head == null) {
  24. return;
  25. }
  26. inOrderRecur(head.left);
  27. System.out.print(head.value + " ");
  28. inOrderRecur(head.right);
  29. }
  30. // 后序遍历
  31. public static void posOrderRecur(Node head) {
  32. if (head == null) {
  33. return;
  34. }
  35. posOrderRecur(head.left);
  36. posOrderRecur(head.right);
  37. System.out.print(head.value + " ");
  38. }
  39. //非递归 前序遍历
  40. public static void preOrderUnrecur(Node head){
  41. System.out.println( "pre-oder unrecur: ");
  42. if (head != null) {
  43. Stack<Node> stack = new Stack<Node>();
  44. stack.add(head);
  45. while(!stack.isEmpty()){
  46. head = stack.pop();
  47. System.out.print(head.value+ " ");
  48. if (head.right!= null) {
  49. stack.push(head.right);
  50. }
  51. if (head.left != null) {
  52. stack.push(head.left);
  53. }
  54. }
  55. }
  56. System.out.println();
  57. }
  58. //非递归 中序遍历
  59. public static void inOrderUnrecur(Node head){
  60. System.out.println( "in-order unrecur:");
  61. if (head != null) {
  62. Stack<Node> stack = new Stack<Node>();
  63. while (!stack.isEmpty() || head != null) {
  64. if(head != null){
  65. stack.push(head);
  66. head = head.left;
  67. }
  68. else {
  69. head = stack.pop();
  70. System.out.print(head.value+ " ");
  71. head = head.right;
  72. }
  73. }
  74. }
  75. System.out.println();
  76. }
  77. public static void posOrderUnRecur1(Node head) {
  78. System.out.print( "pos-order unrecur: ");
  79. if (head != null) {
  80. Stack<Node> s1 = new Stack<Node>();
  81. Stack<Node> s2 = new Stack<Node>();
  82. s1.push(head);
  83. while (!s1.isEmpty()) {
  84. head = s1.pop();
  85. s2.push(head);
  86. if (head.left != null) {
  87. s1.push(head.left);
  88. }
  89. if (head.right != null) {
  90. s1.push(head.right);
  91. }
  92. }
  93. while (!s2.isEmpty()) {
  94. System.out.print(s2.pop().value + " ");
  95. }
  96. }
  97. System.out.println();
  98. }
  99. public static void main(String[] args) {
  100. Node head = new Node( 5);
  101. head.left = new Node( 3);
  102. head.right = new Node( 8);
  103. head.left.left = new Node( 2);
  104. head.left.right = new Node( 4);
  105. head.left.left.left = new Node( 1);
  106. head.right.left = new Node( 7);
  107. head.right.left.left = new Node( 6);
  108. head.right.right = new Node( 10);
  109. head.right.right.left = new Node( 9);
  110. head.right.right.right = new Node( 11);
  111. preOrderRecur(head);
  112. System.out.println();
  113. preOrderUnrecur(head);
  114. System.out.println( "in-order:");
  115. inOrderRecur(head);
  116. System.out.println();
  117. inOrderUnrecur(head);
  118. System.out.println( "pos order :");
  119. posOrderRecur(head);
  120. System.out.println();
  121. posOrderUnRecur1(head);
  122. }
  123. }

猜你喜欢

转载自www.cnblogs.com/hirampeng/p/9545731.html