Java实现二叉树的构建与遍历
转载:http://ocaicai.iteye.com/blog/1047397
目录:
1.把一个数组的值赋值给一颗二叉树
2.具体代码
1.树的构建方法

2.具体代码
- package tree;
- import java.util.LinkedList;
- import java.util.List;
- /**
- * 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历
- *
- * 参考资料0:数据结构(C语言版)严蔚敏
- *
- * 参考资料1:http://zhidao.baidu.com/question/81938912.html
- *
- * 参考资料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java
- *
- * @author ocaicai@yeah.net @date: 2011-5-17
- *
- */
- public class BinTreeTraverse2 {
- private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- private static List<Node> nodeList = null;
- /**
- * 内部类:节点
- *
- * @author ocaicai@yeah.net @date: 2011-5-17
- *
- */
- private static class Node {
- Node leftChild;
- Node rightChild;
- int data;
- Node(int newData) {
- leftChild = null;
- rightChild = null;
- data = newData;
- }
- }
- public void createBinTree() {
- nodeList = new LinkedList<Node>();
- // 将一个数组的值依次转换为Node节点
- for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
- nodeList.add(new Node(array[nodeIndex]));
- }
- // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
- for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
- // 左孩子
- nodeList.get(parentIndex).leftChild = nodeList
- .get(parentIndex * 2 + 1);
- // 右孩子
- nodeList.get(parentIndex).rightChild = nodeList
- .get(parentIndex * 2 + 2);
- }
- // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
- int lastParentIndex = array.length / 2 - 1;
- // 左孩子
- nodeList.get(lastParentIndex).leftChild = nodeList
- .get(lastParentIndex * 2 + 1);
- // 右孩子,如果数组的长度为奇数才建立右孩子
- if (array.length % 2 == 1) {
- nodeList.get(lastParentIndex).rightChild = nodeList
- .get(lastParentIndex * 2 + 2);
- }
- }
- /**
- * 先序遍历
- *
- * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
- *
- * @param node
- * 遍历的节点
- */
- public static void preOrderTraverse(Node node) {
- if (node == null)
- return;
- System.out.print(node.data + " ");
- preOrderTraverse(node.leftChild);
- preOrderTraverse(node.rightChild);
- }
- /**
- * 中序遍历
- *
- * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
- *
- * @param node
- * 遍历的节点
- */
- public static void inOrderTraverse(Node node) {
- if (node == null)
- return;
- inOrderTraverse(node.leftChild);
- System.out.print(node.data + " ");
- inOrderTraverse(node.rightChild);
- }
- /**
- * 后序遍历
- *
- * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
- *
- * @param node
- * 遍历的节点
- */
- public static void postOrderTraverse(Node node) {
- if (node == null)
- return;
- postOrderTraverse(node.leftChild);
- postOrderTraverse(node.rightChild);
- System.out.print(node.data + " ");
- }
- public static void main(String[] args) {
- BinTreeTraverse2 binTree = new BinTreeTraverse2();
- binTree.createBinTree();
- // nodeList中第0个索引处的值即为根节点
- Node root = nodeList.get(0);
- System.out.println("先序遍历:");
- preOrderTraverse(root);
- System.out.println();
- System.out.println("中序遍历:");
- inOrderTraverse(root);
- System.out.println();
- System.out.println("后序遍历:");
- postOrderTraverse(root);
- }
- }
输出结果:
- 先序遍历:
- 1 2 4 8 9 5 3 6 7
- 中序遍历:
- 8 4 9 2 5 1 6 3 7
- 后序遍历:
- 8 9 4 5 2 6 7 3 1
Java实现二叉树的构建与遍历的更多相关文章
- java实现二叉树的构建以及3种遍历方法
转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...
- Java 实现二叉树的构建以及3种遍历方法
转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...
- java实现二叉树的构建以及3种遍历方法(转)
转 原地址:http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历 ...
- Java实现二叉树的创建和遍历操作(有更新)
博主强烈建议跳过分割线前面的部分,直接看下文更新的那些即可. 最近在学习二叉树的相关知识,一开始真的是毫无头绪.本来学的是C++二叉树,但苦于编译器老是出故障,于是就转用Java来实现二叉树的操作.但 ...
- 基于Java的二叉树的三种遍历方式的递归与非递归实现
二叉树的遍历方式包括前序遍历.中序遍历和后序遍历,其实现方式包括递归实现和非递归实现. 前序遍历:根节点 | 左子树 | 右子树 中序遍历:左子树 | 根节点 | 右子树 后序遍历:左子树 | 右子树 ...
- java实现二叉树的Node节点定义手撕8种遍历(一遍过)
java实现二叉树的Node节点定义手撕8种遍历(一遍过) 用java的思想和程序从最基本的怎么将一个int型的数组变成Node树状结构说起,再到递归前序遍历,递归中序遍历,递归后序遍历,非递归前序遍 ...
- java二叉树的实现和遍历
/* * Java实现二叉树 */ public class BinaryTree { int treeNode; BinaryTree leftTree; BinaryTree rightTree; ...
- Java实现二叉树的前序、中序、后序遍历(非递归方法)
在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...
- Java实现二叉树的前序、中序、后序遍历(递归方法)
在数据结构中,二叉树是树中我们见得最多的,二叉查找树可以加速我们查找的效率,那么输出一个二叉树也变得尤为重要了. 二叉树的遍历方法分为三种,分别为前序遍历.中序遍历.后序遍历.下图即为一个二叉 ...
随机推荐
- UVA 10627 - Infinite Race(数论)
UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...
- 使用navicat 11 出现不能返回存储过程结果的问题
问题: 使用navicat 11 调试存储过程,select返回结果,总是不能返回. 原因: 经google发现,navicat仅支持返回10个resultset,超过则不现实. 解决方法: 减少存储 ...
- PRD产品需求文档概要
PRD概念 PRM就是Product Requirements Document的简称,也就是产品需求模型.一般来说一个产品会伴随有市场需求文档(Market Requirements Documen ...
- jQuery循环给某个ID赋值
1.id名为sl的input框循环赋值 $("input[id=sl]").each(function(){alert(this.value) })
- Matlab使用xlsread, xlswrite函数导致excel进程无法终止的问题
系统版本:Win7 64位 Matlab版本:R2015b 问题描述:使用excel的操作函数,比如xlsread,xlswrite,导致excel进程无法终止,任务管理器中仍残留excel进程,打开 ...
- Sql2008中使用DataTable作为存储过程的参数
使用DataTable作为存储过程的参数 最近工作中写了几个存储过 程,需要向存储过程中传递字符串,因为SQL Server 2000中没有内置类似于 split 的函数,只好自己处理,将前台数据 ...
- asp.net模态窗口返回值
个人感觉模态窗口在做网站的时候,使用到的比较少,前段时间在做项目时要实现以模态窗口传值和接收返回值, 模态窗口传值实现比较简单,但是做好后发现在Chrome浏览器中接收不到返回值,修改好Chrome浏 ...
- SGU 231.Prime Sum
题意: 求有多少对质数(a,b)满足a<=b 且a+b也为质数.(a+b<=10^6) Solution: 除了2之外的质数都是奇数,两个奇数的和是偶数,不可能是质数.所以题目就是求差为2 ...
- 【BZOJ3673】【可持久化并查集】可持久化并查集 by zky
Description n个集合 m个操作操作:1 a b 合并a,b所在集合2 k 回到第k次操作之后的状态(查询算作操作)3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0<n ...
- 【POJ1442】【Treap】Black Box
Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...