二叉树类:

 package com.antis.tree;

 public class BinaryTree {

      int data;      //根节点数据
BinaryTree left; //左子树
BinaryTree right; //右子树 public BinaryTree(int data) //实例化二叉树类
{
this.data = data;
left = null;
right = null;
}
/**
* 向二叉树中插入子节点
* @param root
* @param data
*/
public void insert(BinaryTree root,int data){
//二叉树的左节点都比根节点小
if(data>root.data)
{
if(root.right==null){
root.right = new BinaryTree(data);
}else{
this.insert(root.right, data);
}
}else{
//二叉树的右节点都比根节点大
if(root.left==null){
root.left = new BinaryTree(data);
}else{
this.insert(root.left, data);
}
}
} }

二叉树遍历代码:

 package com.antis.tree;

 import java.util.Stack;

 public class BinaryTreeTraversal {

      /**
* 先序遍历--先根遍历递归
* @param root
*/
public static void preOrder(BinaryTree root){ //先根遍历
if (root==null) {
return;
}
System.out.print(root.data+"-");
preOrder(root.left);
preOrder(root.right);
}
/**
* 中序遍历--中根遍历递归
* @param root
*/
public static void inOrder(BinaryTree root){ //中根遍历
if (root==null) {
return;
}
inOrder(root.left);
System.out.print(root.data+"--");
inOrder(root.right);
}
/**
* 后序遍历--后根遍历递归
* @param root
*/
public static void postOrder(BinaryTree root){ //后根遍历
if (root==null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data+"---");
}
// 先序遍历非递归
public static void preOrder2(BinaryTree t) {
Stack<BinaryTree> s = new Stack<BinaryTree>();
while (t != null || !s.empty()) {
while (t != null) {
System.out.print(t.data+"-");
s.push(t);
t = t.left;
}
if (!s.empty()) {
t = s.pop();
t = t.right;
}
}
}
// 中序遍历非递归
public static void InOrder2(BinaryTree t) {
Stack<BinaryTree> s = new Stack<BinaryTree>();
while (t != null || !s.empty()) {
while (t != null) {
s.push(t);
t = t.left;
}
if (!s.empty()) {
t = s.pop();
System.out.print(t.data+"--");
t = t.right;
}
}
} // 后序遍历非递归
public static void PostOrder2(BinaryTree t) {
Stack<BinaryTree> s = new Stack<BinaryTree>();
Stack<Integer> s2=new Stack<Integer>();
Integer i=new Integer(1);
while (t != null || !s.empty()) {
while (t != null) {
s.push(t);
s2.push(new Integer(0));
t = t.left;
}
while (!s.empty() && s2.peek().equals(i)) {
s2.pop();
System.out.print(s.pop().data+"---");
} if (!s.empty()) {
s2.pop();
s2.push(new Integer(1));
t = s.peek();
t = t.right;
}
}
}
public static void main(String[] str){
int[] array = {12,76,35,22,16,48,90,46,9,40};
BinaryTree root = new BinaryTree(array[0]); //创建二叉树
for(int i=1;i<array.length;i++){
root.insert(root, array[i]); //向二叉树中插入数据
}
System.out.println("先根遍历:");
preOrder(root);
System.out.println();
System.out.println("先根遍历:");
preOrder2(root);
System.out.println();
System.out.println("中根遍历:");
inOrder(root);
System.out.println();
System.out.println("中根遍历:");
InOrder2(root);
System.out.println();
System.out.println("后根遍历:");
postOrder(root);
System.out.println();
System.out.println("后根遍历:");
PostOrder2(root);
}
}

运行结果:

先根遍历:
12-9-76-35-22-16-48-46-40-90-
先根遍历:
12-9-76-35-22-16-48-46-40-90-
中根遍历:
9--12--16--22--35--40--46--48--76--90--
中根遍历:
9--12--16--22--35--40--46--48--76--90--
后根遍历:
9---16---22---40---46---48---35---90---76---12---
后根遍历:
9---16---22---40---46---48---35---90---76---12---

JAVA二叉树递归构造、二叉树普通遍历及递归遍历的更多相关文章

  1. 非递归遍历二叉树Java版的实现代码(没写层次遍历)

    直接上代码呵呵,里面有注解 package www.com.leetcode.specificProblem; import java.util.ArrayList; import java.util ...

  2. java实现二叉树的前中后遍历(递归和非递归)

    这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...

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

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

  4. Java实现二叉树的先序、中序、后序、层序遍历(递归和非递归)

    二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易 ...

  5. [LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)、108. 将有序数组转换为二叉搜索树(递归、二分)

    题目 05. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次 ...

  6. Java实现 LeetCode 106 从中序与后序遍历序列构造二叉树

    106. 从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序 ...

  7. Java实现 LeetCode 105 从前序与中序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中 ...

  8. 【算法】二叉树、N叉树先序、中序、后序、BFS、DFS遍历的递归和迭代实现记录(Java版)

    本文总结了刷LeetCode过程中,有关树的遍历的相关代码实现,包括了二叉树.N叉树先序.中序.后序.BFS.DFS遍历的递归和迭代实现.这也是解决树的遍历问题的固定套路. 一.二叉树的先序.中序.后 ...

  9. Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历

    package com.example.demo; public class BTree { public int data; public BTree left; public BTree rigt ...

随机推荐

  1. 解决ajax请求返回Json无法解析"\"字符的问题

    原因:获取身份证信息,涉及图片路径,存在“\”字符,导致Json解析错误 解决思路:将返回类型从"json"改成"text",此时返回的数据类型变成字符串,将字 ...

  2. Selenium库简介

    Selenium是一个自动化测试工具,利用它可以驱动浏览器执行特定的动作,如点击.下拉等操作,同时还可以获取浏览器当前呈现的页面的源代码,做到可见即可爬.对于一些JavaScript动态渲染的页面来说 ...

  3. Magento 2中文文档教程 - Magento 2.1.x 系统需求

    Magento 2.1.x 系统需求 操作系统 (Linux x86-64) Linux发行版如红帽企业Linux(RHEL),CentOS,Ubuntu,Debian,等等 内存需求 升级的应用程序 ...

  4. MySQL 字段全部转换成小写

    原因: 因为框架某些字段大写有时候不被正确识别,所以字段都修改成小写; 特别说明:因为这里只有表,没有视图,存储过程等等其它所以我可以直接这么写; 步骤: 1.导出结构语句 2. 执行C# 脚本,替换 ...

  5. .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter

    dump文件相信有些朋友已经很熟悉了,dump文件的作用在于保存进程运行时的堆栈信息,方便日后排查软件故障,提升软件质量.关于dump分析工具windbg.adplus的文章更多了,如果您还不知道怎么 ...

  6. svn在commit后报错:is scheduled for addition, but is missing

    今天通过svn 的cr(code review)代码审核后,我欲执行svn ci -m"xxxxxxx(提交注释) ISSUE=3380305",但是没有提交成功,SVN报错啦! ...

  7. mysql表情存储报错问题

    mysql采用utf-8字符编码,但在移动端使用输入法的表情并存储数据库的时候,出现错误. java.sql.SQLException: Incorrect string value: '\xF0\x ...

  8. equals()重写

    ** 注意 ** 1.重写equals方法修饰符必须是public,因为是重写的Object的方法. 2.参数类型必须是Object. 3.重写equals方法后最好重写hashCode方法,否则两个 ...

  9. 5、springboot之修改端口号

    在resources中加入application.properties文件,里面加入 servier.port = 端口号 访问的时候,就用localhost:端口号 完事

  10. 九 ServerSocketChannel

    ServerSocketChannel是一个可以监听进来的TCP连接的通道,就像标准IO的ServerSocket一样.ServerSocketChannel类在java.nio.channels包中 ...