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

目录:

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

Java实现二叉树的构建与遍历的更多相关文章

  1. java实现二叉树的构建以及3种遍历方法

    转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...

  2. Java 实现二叉树的构建以及3种遍历方法

    转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...

  3. java实现二叉树的构建以及3种遍历方法(转)

    转 原地址:http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历 ...

  4. Java实现二叉树的创建和遍历操作(有更新)

    博主强烈建议跳过分割线前面的部分,直接看下文更新的那些即可. 最近在学习二叉树的相关知识,一开始真的是毫无头绪.本来学的是C++二叉树,但苦于编译器老是出故障,于是就转用Java来实现二叉树的操作.但 ...

  5. 基于Java的二叉树的三种遍历方式的递归与非递归实现

    二叉树的遍历方式包括前序遍历.中序遍历和后序遍历,其实现方式包括递归实现和非递归实现. 前序遍历:根节点 | 左子树 | 右子树 中序遍历:左子树 | 根节点 | 右子树 后序遍历:左子树 | 右子树 ...

  6. java实现二叉树的Node节点定义手撕8种遍历(一遍过)

    java实现二叉树的Node节点定义手撕8种遍历(一遍过) 用java的思想和程序从最基本的怎么将一个int型的数组变成Node树状结构说起,再到递归前序遍历,递归中序遍历,递归后序遍历,非递归前序遍 ...

  7. java二叉树的实现和遍历

    /* * Java实现二叉树 */ public class BinaryTree { int treeNode; BinaryTree leftTree; BinaryTree rightTree; ...

  8. Java实现二叉树的前序、中序、后序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...

  9. Java实现二叉树的前序、中序、后序遍历(递归方法)

      在数据结构中,二叉树是树中我们见得最多的,二叉查找树可以加速我们查找的效率,那么输出一个二叉树也变得尤为重要了.   二叉树的遍历方法分为三种,分别为前序遍历.中序遍历.后序遍历.下图即为一个二叉 ...

随机推荐

  1. UVA 10627 - Infinite Race(数论)

    UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...

  2. 使用navicat 11 出现不能返回存储过程结果的问题

    问题: 使用navicat 11 调试存储过程,select返回结果,总是不能返回. 原因: 经google发现,navicat仅支持返回10个resultset,超过则不现实. 解决方法: 减少存储 ...

  3. PRD产品需求文档概要

    PRD概念 PRM就是Product Requirements Document的简称,也就是产品需求模型.一般来说一个产品会伴随有市场需求文档(Market Requirements Documen ...

  4. jQuery循环给某个ID赋值

    1.id名为sl的input框循环赋值 $("input[id=sl]").each(function(){alert(this.value) })

  5. Matlab使用xlsread, xlswrite函数导致excel进程无法终止的问题

    系统版本:Win7 64位 Matlab版本:R2015b 问题描述:使用excel的操作函数,比如xlsread,xlswrite,导致excel进程无法终止,任务管理器中仍残留excel进程,打开 ...

  6. Sql2008中使用DataTable作为存储过程的参数

    使用DataTable作为存储过程的参数   最近工作中写了几个存储过 程,需要向存储过程中传递字符串,因为SQL Server 2000中没有内置类似于 split 的函数,只好自己处理,将前台数据 ...

  7. asp.net模态窗口返回值

    个人感觉模态窗口在做网站的时候,使用到的比较少,前段时间在做项目时要实现以模态窗口传值和接收返回值, 模态窗口传值实现比较简单,但是做好后发现在Chrome浏览器中接收不到返回值,修改好Chrome浏 ...

  8. SGU 231.Prime Sum

    题意: 求有多少对质数(a,b)满足a<=b 且a+b也为质数.(a+b<=10^6) Solution: 除了2之外的质数都是奇数,两个奇数的和是偶数,不可能是质数.所以题目就是求差为2 ...

  9. 【BZOJ3673】【可持久化并查集】可持久化并查集 by zky

    Description n个集合 m个操作操作:1 a b 合并a,b所在集合2 k 回到第k次操作之后的状态(查询算作操作)3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0<n ...

  10. 【POJ1442】【Treap】Black Box

    Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...