转 原地址:http://ocaicai.iteye.com/blog/1047397

大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿耿于怀,今天又遇见这个问题了,所以花了一下午的时间来编写代码以及介绍思路的文档生成!

目录:

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

2.具体代码

1.树的构建方法

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

最新文章

  1. JavaScript 随笔2 面向对象 原型链 继承
  2. c基础回顾
  3. 认识B/S架构
  4. Eclipse快捷键/快捷操作汇总
  5. 看起来像一个输入框的input,实际上是有两个input
  6. 最小生成树之prim
  7. 初步体验javascript try catch机制
  8. Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
  9. 【jQuery】总结:筛选器、控制隐藏、操作元素style属性
  10. Spring Cloud OAuth
  11. WinForm中TextBox 中判断扫描枪输入与键盘输入
  12. angularjs使用directive实现倒计时按钮
  13. js判断微信内置浏览器
  14. C#事物
  15. 伯克利推出世界最快的KVS数据库Anna:秒杀Redis和Cassandra
  16. Hadoop优化
  17. [UE4]List View
  18. 设计模式(二)策略模式(Strategy)
  19. MacBook鼠标指针乱窜/不受控制问题的解决方法
  20. java设计模式之组合

热门文章

  1. Xocde 自动注释插件
  2. iphone/iOS 访问本地数据库sqlite3
  3. 【spring cloud】注解@SpringCloudApplication和@SpringBootApplication的区别
  4. 从数据库中选取数据形成select标签
  5. nginx 启动,重启,添加开机启动等相关命令
  6. 微信开源组件WCDB漫谈及Demo
  7. RelativeLayout布局(仅在RelativeLayout中有效)
  8. SpringBoot学习之启动方式
  9. CSS3 background属性
  10. 自己定义ProgressDialog载入图片