歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java版 二叉樹 所有遞歸和非遞歸遍歷算法

Java版 二叉樹 所有遞歸和非遞歸遍歷算法

日期:2017/3/1 10:10:58   编辑:Linux編程

通過數組構造二叉樹,所有遍歷算法以及求二叉樹深度的遞歸算法

  1. import java.util.LinkedList;
  2. public class BinaryTree {
  3. private Node<Integer> root;
  4. private int size;
  5. public BinaryTree() {
  6. root = new Node<Integer>();
  7. }
  8. public BinaryTree(int[] values) {
  9. System.out.print("新建binaryTree:");
  10. for (int i : values) {
  11. System.out.print(i);
  12. }
  13. System.out.println();
  14. boolean isLeft = true;
  15. int len = values.length;
  16. if (len == 0)
  17. return ;
  18. LinkedList<Node<Integer>> queue = new LinkedList<Node<Integer>>();
  19. root = new Node<Integer>(values[0]);
  20. queue.addLast(root);
  21. Node parent = null;
  22. Node current = null;
  23. for (int i=1; i<len; i++) {
  24. current = new Node<Integer>(values[i]);
  25. queue.addLast(current);
  26. if (isLeft)
  27. parent = queue.getFirst();
  28. else
  29. parent = queue.removeFirst();
  30. if (isLeft) {
  31. parent.setLeftChild(current);
  32. isLeft = false;
  33. }else {
  34. parent.setRightChild(current);
  35. isLeft = true;
  36. }
  37. }
  38. }
  39. public void inorder() {
  40. System.out.print("binaryTree遞歸中序遍歷:");
  41. inorderTraverseRecursion(root);
  42. System.out.println();
  43. }
  44. public void layerorder() {
  45. System.out.print("binaryTree層次遍歷:");
  46. LinkedList<Node<Integer>> queue = new LinkedList<Node<Integer>>();
  47. queue.addLast(root);
  48. Node<Integer> current = null;
  49. while(!queue.isEmpty()) {
  50. current = queue.removeFirst();
  51. if (current.getLeftChild() != null)
  52. queue.addLast(current.getLeftChild());
  53. if (current.getRightChild() != null)
  54. queue.addLast(current.getRightChild());
  55. System.out.print(current.getValue());
  56. }
  57. System.out.println();
  58. }
  59. public int getLength() {
  60. return getLengthRecursion(root);
  61. }
  62. private int getLengthRecursion(Node<Integer> node){
  63. if (node == null)
  64. return 0;
  65. int llen = getLengthRecursion(node.getLeftChild());
  66. int rlen = getLengthRecursion(node.getRightChild());
  67. int maxlen = Math.max(llen, rlen);
  68. return maxlen + 1;
  69. }
  70. public void preorder() {
  71. System.out.print("binaryTree遞歸先序遍歷:");
  72. preorderTraverseRecursion(root);
  73. System.out.println();
  74. }
  75. private void inorderTraverseRecursion(Node<Integer> node) {
  76. // TODO Auto-generated method stub
  77. if (node.getLeftChild() != null)
  78. inorderTraverseRecursion(node.getLeftChild());
  79. System.out.print(node.getValue());
  80. if (node.getRightChild() != null)
  81. inorderTraverseRecursion(node.getRightChild());
  82. }
  83. private void preorderTraverseRecursion(Node<Integer> node){
  84. System.out.print(node.getValue());
  85. if (node.getLeftChild() != null)
  86. preorderTraverseRecursion (node.getLeftChild());
  87. if (node.getRightChild() != null)
  88. preorderTraverseRecursion (node.getRightChild());
  89. }
  90. public void preorderNoRecursion() {
  91. System.out.print("binaryTree非遞歸先序遍歷:");
  92. LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>();
  93. stack.push(root);
  94. Node<Integer> current = null;
  95. while (!stack.isEmpty()) {
  96. current = stack.pop();
  97. System.out.print(current.getValue());
  98. if (current.getRightChild() != null)
  99. stack.push(current.getRightChild());
  100. if (current.getLeftChild() != null)
  101. stack.push(current.getLeftChild());
  102. }
  103. System.out.println();
  104. }
  105. /**
  106. * 棧內保存將要訪問的元素
  107. */
  108. public void inorderNoRecursion() {
  109. System.out.print("binaryTree非遞歸中序遍歷:");
  110. LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>();
  111. Node<Integer> current = root;
  112. while (current != null || !stack.isEmpty()) {
  113. while(current != null) {
  114. stack.push(current);
  115. current = current.getLeftChild();
  116. }
  117. if (!stack.isEmpty()) {
  118. current = stack.pop();
  119. System.out.print(current.getValue());
  120. current = current.getRightChild();
  121. }
  122. }
  123. System.out.println();
  124. }
  125. /**
  126. * 當上一個訪問的結點是右孩子或者當前結點沒有右孩子則訪問當前結點
  127. */
  128. public void postorderNoRecursion() {
  129. System.out.print("binaryTree非遞歸後序遍歷:");
  130. Node<Integer> rNode = null;
  131. Node<Integer> current = root;
  132. LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>();
  133. while(current != null || !stack.isEmpty()) {
  134. while(current != null) {
  135. stack.push(current);
  136. current = current.getLeftChild();
  137. }
  138. current = stack.pop();
  139. while (current != null && (current.getRightChild() == null ||current.getRightChild() == rNode)) {
  140. System.out.print(current.getValue());
  141. rNode = current;
  142. if (stack.isEmpty()){
  143. System.out.println();
  144. return;
  145. }
  146. current = stack.pop();
  147. }
  148. stack.push(current);
  149. current = current.getRightChild();
  150. }
  151. }
  152. public static void main(String[] args) {
  153. BinaryTree bt = new BinaryTree(new int[]{1,2,3,4,5,6,7,8});
  154. bt.inorder();
  155. bt.preorder();
  156. bt.layerorder();
  157. bt.preorderNoRecursion();
  158. bt.inorderNoRecursion();
  159. bt.postorderNoRecursion();
  160. System.out.println("深度為:" + bt.getLength());
  161. }
  162. }
  163. class Node<V>{
  164. private V value;
  165. private Node<V> leftChild;
  166. private Node<V> rightChild;
  167. public Node(){
  168. };
  169. public Node(V value) {
  170. this.value = value;
  171. leftChild = null;
  172. rightChild = null;
  173. }
  174. public void setLeftChild(Node<V> lNode) {
  175. this.leftChild = lNode;
  176. }
  177. public void setRightChild(Node<V> rNode) {
  178. this.rightChild = rNode;
  179. }
  180. public V getValue() {
  181. return value;
  182. }
  183. public void setValue(V value) {
  184. this.value = value;
  185. }
  186. public Node<V> getLeftChild() {
  187. return leftChild;
  188. }
  189. public Node<V> getRightChild() {
  190. return rightChild;
  191. }
  192. }
Copyright © Linux教程網 All Rights Reserved