Java实现二叉树的构建与遍历

时间:2023-03-08 15:44:45
Java实现二叉树的构建与遍历

转载:http://ocaicai.iteye.com/blog/1047397

目录:

1.把一个数组的值赋值给一颗二叉树

2.具体代码

1.树的构建方法

Java实现二叉树的构建与遍历

2.具体代码

  1. package tree;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. /**
  5. * 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历
  6. *
  7. * 参考资料0:数据结构(C语言版)严蔚敏
  8. *
  9. * 参考资料1:http://zhidao.baidu.com/question/81938912.html
  10. *
  11. * 参考资料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java
  12. *
  13. * @author ocaicai@yeah.net @date: 2011-5-17
  14. *
  15. */
  16. public class BinTreeTraverse2 {
  17. private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  18. private static List<Node> nodeList = null;
  19. /**
  20. * 内部类:节点
  21. *
  22. * @author ocaicai@yeah.net @date: 2011-5-17
  23. *
  24. */
  25. private static class Node {
  26. Node leftChild;
  27. Node rightChild;
  28. int data;
  29. Node(int newData) {
  30. leftChild = null;
  31. rightChild = null;
  32. data = newData;
  33. }
  34. }
  35. public void createBinTree() {
  36. nodeList = new LinkedList<Node>();
  37. // 将一个数组的值依次转换为Node节点
  38. for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
  39. nodeList.add(new Node(array[nodeIndex]));
  40. }
  41. // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
  42. for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
  43. // 左孩子
  44. nodeList.get(parentIndex).leftChild = nodeList
  45. .get(parentIndex * 2 + 1);
  46. // 右孩子
  47. nodeList.get(parentIndex).rightChild = nodeList
  48. .get(parentIndex * 2 + 2);
  49. }
  50. // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
  51. int lastParentIndex = array.length / 2 - 1;
  52. // 左孩子
  53. nodeList.get(lastParentIndex).leftChild = nodeList
  54. .get(lastParentIndex * 2 + 1);
  55. // 右孩子,如果数组的长度为奇数才建立右孩子
  56. if (array.length % 2 == 1) {
  57. nodeList.get(lastParentIndex).rightChild = nodeList
  58. .get(lastParentIndex * 2 + 2);
  59. }
  60. }
  61. /**
  62. * 先序遍历
  63. *
  64. * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
  65. *
  66. * @param node
  67. *            遍历的节点
  68. */
  69. public static void preOrderTraverse(Node node) {
  70. if (node == null)
  71. return;
  72. System.out.print(node.data + " ");
  73. preOrderTraverse(node.leftChild);
  74. preOrderTraverse(node.rightChild);
  75. }
  76. /**
  77. * 中序遍历
  78. *
  79. * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
  80. *
  81. * @param node
  82. *            遍历的节点
  83. */
  84. public static void inOrderTraverse(Node node) {
  85. if (node == null)
  86. return;
  87. inOrderTraverse(node.leftChild);
  88. System.out.print(node.data + " ");
  89. inOrderTraverse(node.rightChild);
  90. }
  91. /**
  92. * 后序遍历
  93. *
  94. * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
  95. *
  96. * @param node
  97. *            遍历的节点
  98. */
  99. public static void postOrderTraverse(Node node) {
  100. if (node == null)
  101. return;
  102. postOrderTraverse(node.leftChild);
  103. postOrderTraverse(node.rightChild);
  104. System.out.print(node.data + " ");
  105. }
  106. public static void main(String[] args) {
  107. BinTreeTraverse2 binTree = new BinTreeTraverse2();
  108. binTree.createBinTree();
  109. // nodeList中第0个索引处的值即为根节点
  110. Node root = nodeList.get(0);
  111. System.out.println("先序遍历:");
  112. preOrderTraverse(root);
  113. System.out.println();
  114. System.out.println("中序遍历:");
  115. inOrderTraverse(root);
  116. System.out.println();
  117. System.out.println("后序遍历:");
  118. postOrderTraverse(root);
  119. }
  120. }

输出结果:

    1. 先序遍历:
    2. 1 2 4 8 9 5 3 6 7
    3. 中序遍历:
    4. 8 4 9 2 5 1 6 3 7
    5. 后序遍历:
    6. 8 9 4 5 2 6 7 3 1