相关概念

  存储结构:

  1. 顺序存储结构:二叉树的顺序存储结构适用于完全二叉树,对完全二叉树进行顺序编号,通过二叉树的性质五(第1个结点为根结点,第i个结点的左孩子为第2i个结点,右孩子为第2i+1个结点)。
  2. 链式存储结构:一般情况下,采用链式存储结构来存储二叉树。每个结点有3个域:data、left、right。

  遍历:

  1. 先根次序:根->左->右。
  2. 中根次序:左->根->右。
  3. 后根次序:左->右->根。

  遍历算法:

  1. 递归
  2. 非递归:通过设立一个栈。

声明二叉树结点类

 /**
* Copyright 2016 Zhengbin's Studio.
* All right reserved.
* 2016年7月16日 上午8:19:15
*/
package Two; /**
* @author zhengbinMac
*
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
/**
* 先根遍历
* @param p
*/
public void preorder(TreeNode p) {
if(p != null) {
System.out.print(p.val + " ");
preorder(p.left);
preorder(p.right);
}
}
/**
* 中根遍历
* @param p
*/
public void inorder(TreeNode p) {
if(p != null) {
preorder(p.left);
System.out.print(p.val + " ");
preorder(p.right);
}
}
/**
* 后根遍历
* @param p
*/
public void postorder(TreeNode p) {
if(p != null) {
preorder(p.left);
preorder(p.right);
System.out.print(p.val + " ");
}
}
}

声明二叉树类 和 由先根遍历与中根遍历构造二叉树

  建立一颗二叉树必须明确以下两点:

  1. 结点与双亲结点及孩子结点间的层次关系。
  2. 兄弟结点间的左右子树的顺序关系。

  先根次序或后根次序反映双亲与孩子结点的层次关系,中根次序反映兄弟结点间的左右次序。所以,已知先根和中根两种遍历序列,或中根和后根两种遍历序列才能够唯一确定一颗二叉树。而已知先根和后根两种遍历序列仍无法唯一确定一颗二叉树。

 /**
* Copyright 2016 Zhengbin's Studio.
* All right reserved.
* 2016年7月16日 上午9:30:01
*/
package Two; import java.util.Arrays; /**
* @author zhengbinMac
*
*/
public class Tree {
protected TreeNode root;
public Tree() {
root = null;
}
public Tree(int[] pre, int[] in) {
// root = reConstructBinaryTree(pre, in);
root = reConstructBinaryTree1(pre, in);
}
/**
* 先根次序
*/
public void preorderTraversal() {
System.out.println("先根次序遍历:");
if(root != null) {
root.preorder(root);
}
}
/**
* 中根次序
*/
public void inorderTraversal() {
System.out.println("中根次序遍历:");
if(root != null) {
root.inorder(root);
}
}
/**
* 后根次序
*/
public void postorderTraversal() {
System.out.println("后根次序遍历:");
if(root != null) {
root.postorder(root);
}
}
/**
* 通过先根遍历与中根遍历构造二叉树(1)
*/
public TreeNode reConstructBinaryTree1(int[] pre, int[] in) {
if(pre.length == 0 || in.length == 0) {
return null;
}
TreeNode p = new TreeNode(pre[0]);
for (int i = 0; i < in.length; i++) {
if(pre[0] == in[i]) {
p.left = reConstructBinaryTree1(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(in, 0, i));
p.right = reConstructBinaryTree1(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(in, i+1, in.length));
}
}
return p;
}
/**
* 通过先根遍历与中根遍历构造二叉树(2)
*/
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
if(pre == null || in == null) {
return null;
}
TreeNode p = null;
int first;
int n = pre.length;
int k = 0;
if(n > 0) {
// 取第一个为根
first = pre[0];
p = new TreeNode(first);
// 确定根结点在中根序列中的位置
for (int i = 0; i < in.length; i++) {
if(in[i] == first) {
k = i;
break;
}
}
// 左子树
int[] presubLeft = new int[k];
int[] insubLeft = new int[k];
// 先根
for (int i = 1, j = 0; i <= k; i++, j++) {
presubLeft[j] = pre[i];
}
// 中根
for (int i = 0, j = 0; i <= k-1; i++, j ++) {
insubLeft[j] = in[i];
}
p.left = reConstructBinaryTree(presubLeft, insubLeft);
// 右子树
int[] presubRight = new int[n-1-k];
int[] insubRight = new int[n-1-k];
// 先根
for (int i = k+1, j = 0; i <= n-1; i++,j++) {
presubRight[j] = pre[i];
}
// 中根
for (int i = k+1, j = 0; i <= n-1; i++,j++) {
insubRight[j] = in[i];
}
p.right = reConstructBinaryTree(presubRight, insubRight);
}
return p;
}
}

测试类

 /**
* Copyright 2016 Zhengbin's Studio.
* All right reserved.
* 2016年7月16日 上午9:27:40
*/
package Two; /**
* @author zhengbinMac
*
*/
public class Test { public static void main(String[] args) {
int[] pre = {1,2,4,3,5,6};
int[] in = {4,2,1,5,3,6};
Tree t = new Tree(pre, in);
t.inorderTraversal();
System.out.println();
t.postorderTraversal();
System.out.println();
t.preorderTraversal();
}
}

在线编程:

牛客网——《剑指Offer》-重建二叉树

数据结构——Java实现二叉树的更多相关文章

  1. 【数据结构】之二叉树的java实现

    转自:http://blog.csdn.net/wuwenxiang91322/article/details/12231657 二叉树的定义: 二叉树是树形结构的一个重要类型.许多实际问题抽象出来的 ...

  2. 数据结构(5):Java实现二叉树

    二叉树图: package com.test.Sort; import java.util.ArrayList; import java.util.LinkedList; public class B ...

  3. 【Java】 二叉树的遍历(递归与循环+层序遍历)

    在[Java] 大话数据结构(9) 树(二叉树.线索二叉树)一文中,已经实现了采用递归方法的前.中.后序遍历,本文补充了采用循环的实现方法.以及层序遍历并进行了一个总结. 递归实现 /* * 前序遍历 ...

  4. Java实现二叉树及相关遍历方式

    Java实现二叉树及相关遍历方式 在计算机科学中.二叉树是每一个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(r ...

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

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

  6. javascript数据结构与算法-- 二叉树

    javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...

  7. SDUT 3346 数据结构实验之二叉树七:叶子问题

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

  8. SDUT 3345 数据结构实验之二叉树六:哈夫曼编码

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 字符的编 ...

  9. SDUT 3340 数据结构实验之二叉树一:树的同构

    数据结构实验之二叉树一:树的同构 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两棵树 ...

随机推荐

  1. node操作mysql数据库

    1.建立数据库连接:createConnection(Object)方法       该方法接受一个对象作为参数,该对象有四个常用的属性host,user,password,database.与php ...

  2. Struts2 Convention插件的使用(1)

    刚刚查阅官方文档(convention-plugin.html)并学习了Struts2的Convention插件,文章这里只作为一个笔记,建议大家去看官方文档比较清晰和全面. 需要在项目添加这些包 c ...

  3. lintcode:完美平方

    题目 给一个正整数 n, 找到若干个完全平方数(比如1, 4, 9, ... )使得他们的和等于 n.你需要让平方数的个数最少. 样例 给出 n = 12, 返回 3 因为 12 = 4 + 4 + ...

  4. 540A: Combination Lock

    题目链接:http://codeforces.com/problemset/problem/540/A 题意: 输入的两个长度一样的数,求对应位置的某位数到下一个数需要最小的步长,每次只能先前或先后走 ...

  5. [Microsoft][ODBC Microsoft Access Driver] INSERT INTO 语句的语法错误。

    遇到的情景: sta.executeUpdate("insert into 表1(longitude,latitude,time) values("+a[0]+",&qu ...

  6. Android ListView无法触发ItemClick事件

    Android ListView无法触发ItemClick事件 开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承Base ...

  7. linux 安装scons

     scons是一个Python写的自动化构建工具,需要安装python和scons后才能运行,能够跨平台.其集成功能类似于autoconf/automake ,是一个简洁可靠的工具.现在很多系统都自带 ...

  8. hadoop中的ssh无密码登录配置

    在配置Hadoop集群分布时,要使用SSH免密码登录,假设现在有两台机器hadoop@Master(192.168.1.101),作为Master机,hadoop@Slave(192.168.1.10 ...

  9. USACO Section 2.4: The Tamworth Two

    这题我是用蒙的方法来弄出最后的不能碰到的条件的(用1000试了下account跳出条件),结果竟然还过了,不过网上有精准的求出这个碰不到的条件,farm的状态为10*10*4 = 400,cow的状态 ...

  10. 存根类STUB

    当我们创建一个指定各种方法集合的接口时,我们可以考虑使用"存根”STUB,“存根”就是用空方法体实现该接口中所有方法的类,这样我们就可以通过继承该“存根”创建一个实现该接口的类,这样一来,该 ...