package mystudy;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack; public class Tree { private TreeNode root; public Tree() { }; public Tree(TreeNode root) {
this.root = root;
} public void initTree() {
root = new TreeNode(8);
root.setLeft(new TreeNode(5));
root.getLeft().setLeft(new TreeNode(7));
root.getLeft().setRight(new TreeNode(4));
root.setRight(new TreeNode(9));
root.getRight().setRight(new TreeNode(6));
root.getRight().setLeft(new TreeNode(10));
} public void preOrderTraverse() {
preOrderTraverse(root);
} public void preOrderTraverse(TreeNode node) {
if (node != null) {
System.out.println(node.getValue());
preOrderTraverse(node.getLeft());
preOrderTraverse(node.getRight());
}
} public void nPreOrderTraverse() {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
System.out.println(node.getValue());
stack.push(node);
node = node.getLeft();
}
if (!stack.isEmpty()) {
node = stack.pop();
node = node.getRight();
}
}
} public void inOrderTraverse() {
inOrderTraverse(root);
} public void inOrderTraverse(TreeNode node) {
if (node != null) {
inOrderTraverse(node.getLeft());
System.out.println(node.getValue());
inOrderTraverse(node.getRight());
}
} public void nInOrderTraverse() {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (!stack.isEmpty()) {
node = stack.pop();
System.out.println(node.getValue());
node = node.getRight();
}
}
} public void postOrderTraverse() {
postOrderTraverse(root);
} public void postOrderTraverse(TreeNode node) {
if (node != null) {
postOrderTraverse(node.getLeft());
postOrderTraverse(node.getRight());
System.out.println(node.getValue());
}
} public void nPostOrderTraverse() {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node = root;
TreeNode preNode = null;
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (!stack.isEmpty()) {
node = stack.peek();
if (node.getRight() == null || node.getRight() == preNode) {
node = stack.pop();
System.out.println(node.getValue());
preNode = node;
node = null;
} else {
node = node.getRight();
}
}
} } public void levelTraverse() {
levelTraverse(root);
} public void levelTraverse(TreeNode node) {
if (node != null) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(node);
while (!queue.isEmpty()) {
TreeNode mNode = queue.poll();
if (mNode != null) {
System.out.println(mNode.getValue());
if (mNode.getLeft() != null) {
queue.offer(mNode.getLeft());
}
if (mNode.getRight() != null) {
queue.offer(mNode.getRight());
}
}
}
}
} public int treeDepth() {
return treeDepth(root);
} public int treeDepth(TreeNode node) {
if (node == null) {
return 0;
}
int leftDepth = treeDepth(node.getLeft());
int rightDepth = treeDepth(node.getRight());
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
} public int minMaxSpan() {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if (root != null) {
int visitedNum = 0, addedNum = 1, levelNum = 1, min, max, depth = 0, minLevel = 0, maxLevel = 0;
min = max = root.getValue();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode mNode = queue.poll();
if (min > mNode.getValue()) {
min = mNode.getValue();
minLevel = depth;
} else if (max < mNode.getValue()) {
max = mNode.getValue();
maxLevel = depth;
}
visitedNum++;
if (mNode.getLeft() != null) {
queue.offer(mNode.getLeft());
addedNum++;
}
if (mNode.getRight() != null) {
queue.offer(mNode.getRight());
addedNum++;
}
if (visitedNum == levelNum) {
depth++;
levelNum = addedNum;
}
}
System.out.println("min:" + min + "max:" + max + "minLevel:"
+ minLevel + "maxLevel:" + maxLevel + "树的高度:" + depth);
return Math.abs(minLevel - maxLevel);
}
return -1;
} public class TreeNode {
private TreeNode left;
private TreeNode right;
private int value; public TreeNode(TreeNode left, TreeNode right, int value) {
this.left = left;
this.right = right;
this.value = value;
} public TreeNode(int value) {
this(null, null, value);
} public TreeNode getLeft() {
return left;
} public void setLeft(TreeNode left) {
this.left = left;
} public TreeNode getRight() {
return right;
} public void setRight(TreeNode right) {
this.right = right;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} } /**
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
Tree tree = new Tree();
tree.initTree();
System.out.println("树中最大值最小值层数之差:" + tree.minMaxSpan());
System.out.println("前序递归:");
tree.preOrderTraverse();
System.out.println("前序非递归:");
tree.nPreOrderTraverse();
System.out.println("中序递归:");
tree.inOrderTraverse();
System.out.println("中序非递归:");
tree.nInOrderTraverse();
System.out.println("后序递归:");
tree.postOrderTraverse();
System.out.println("后序非递归:");
tree.nPostOrderTraverse();
System.out.println("按层次遍历:");
tree.levelTraverse();
System.out.println("树的高度:" + tree.treeDepth());
} }

树的各种操作java的更多相关文章

  1. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. 使用BeanUtils工具类操作Java bean

    1.类的属性: 1).在Java EE中,类的属性通过setter和getter定义:类中的setter(getter)方法去除set(get)后剩余的部分就是类的属性 2).而之前叫的类的属性,即成 ...

  3. Arrays 类操作 Java 的数组排序

    使用 Arrays 类操作 Java 中的数组 Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等( ...

  4. 线段树区间更新操作及Lazy思想(详解)

    此题题意很好懂:  给你N个数,Q个操作,操作有两种,‘Q a b ’是询问a~b这段数的和,‘C a b c’是把a~b这段数都加上c. 需要用到线段树的,update:成段增减,query:区间求 ...

  5. Splay伸展树入门(单点操作,区间维护)附例题模板

    Pps:终于学会了伸展树的区间操作,做一个完整的总结,总结一下自己的伸展树的单点操作和区间维护,顺便给未来的自己总结复习用. splay是一种平衡树,[平均]操作复杂度O(nlogn).首先平衡树先是 ...

  6. 报表生成poi----java操作java对象生成execl表单

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  7. 标准Trie字典树学习二:Java实现方式之一

    特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! 系列文章: 1. 标准Trie字典树学习一:原理解析 2.标准T ...

  8. Android NDK开发 JNI操作java构造方法,普通方法,静态方法(七)

    Android NDK开发 JNI操作java普通.静态.构造方法 1.Jni实例化一个Java类的实例jobject 1.通过FindClas( ),获取Java类的的jclass 2.通过GetM ...

  9. java实现树的一般操作

    https://www.cnblogs.com/dawnyxl/p/9047437.html 树是数据结构中最基本的结构,今天的博客更新一下树的基本操作: 树的节点结构: package tree; ...

随机推荐

  1. C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码

    项目开始设计的是运行在windows下,所以一开始采用的是windows服务模式来获取多媒体文件信息,后来要求调整为可以在Linux下运行,经过这两天的资料查找,实现了Linux下通过.NET来获取多 ...

  2. vue中的坑 --- 锚点与查询字符串

    在vue中,由于是单页面SPA,所以需要使用锚点来定位,在vue的官方文档中提到过也可以不使用锚点的情况,就是在vue-router中使用history模式,这样,在url中就不会出现丑陋的#了,但是 ...

  3. maven多层项目配置

    今天遇到一个maven项目有3个子项目的配置问题,一开始项目结构是混乱的,而且包引入不能正常解析. 主项目上右键,选择configure->configure and detect nested ...

  4. goLang冒泡

    // test project main.gopackage main import (    "fmt") func main() {    var a = [10]int{1, ...

  5. Call to a member function assign() on null

    Thinkphp: 在子控制器里面写了一个构造函数,如下 //构造函数 public function __construct(){ echo 1; } 结果页面报错了  ---->  Call ...

  6. Ubuntu14.04搭建Oracle instantClient 11.2.0.4并配置cx_Oracle5.1.2

    一.配置Oracle instantClient 11.2.0.4 1.下载Oracle客户端: 打开http://www.oracle.com/technetwork/topics/linuxx86 ...

  7. luajit+nginx+上传模块+lua模块编译安装

    git clone https://github.com/fdintino/nginx-upload-module.git git clone https://github.com/openresty ...

  8. Java 重写(Override)与重载(Overload)区别

    2019-04-1217:31:19 (1)方法重载是一个类中定义了多个方法名相同,而他们的参数的数量不同或数量相同而类型和次序不同,则称为方法的重载(Overloading). (2)方法重写是在子 ...

  9. MySQL:ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)

    错误:ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO) 原因 :登录帐户错误(ODB ...

  10. springboot aop使用介绍

    第一步:添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...