二叉树类:

 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. BNU44583——Star Trek: First Contact——————【01背包】

    H. Star Trek: First Contact Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld  ...

  2. step2: 爬取廖雪峰博客

    #https://zhuanlan.zhihu.com/p/26342933 #https://zhuanlan.zhihu.com/p/26833760 scrapy startproject li ...

  3. [APIO2018] Circle selection 选圆圈

    Description 给出 \(n\) 个圆 \((x_i,y_i,r_i)\) 每次重复以下步骤: 找出半径最大的圆,并删除与这个圆相交的圆 求出每一个圆是被哪个圆删除的 Solution \(k ...

  4. 第四章使用java实现面向对象-接口

    一.接口 1.接口可以看作是一种特殊的“抽象类”. 2.接口有比抽象类更好的特性 3.可以被多继承 4.设计和实现完全分离 5.更自然的使用多态 二.接口约定 1.接口表示一种约定:体现在接口名称和注 ...

  5. Lucene学习之二:Lucene的总体架构

    本文转载自:http://www.cnblogs.com/forfuture1978/archive/2009/12/14/1623596.html Lucene总的来说是: 一个高效的,可扩展的,全 ...

  6. RabbitMQ - 介绍

    RabbitMQ是个健壮.易用.开源.支持多种操作系统和语言的message broker. 当然,一切的前提是机器里面正在运行着rabbitmq-server. 点击下面的图片下载: rabbitM ...

  7. nginx+nodejs+mysql+memcached服务器后台架设centos6.5

    需要的下面四个工具最好都采用yum安装,不要采用编译安装的方法,因为编译安装会导致某些依赖关系丢失. nginx 作为HTTP和反向代理,处理静态页面,动态服务交由nodejs服务. nodejs作为 ...

  8. Dockerfile定制镜像

    一.Dockerfile是什么? 镜像定制实质就是定制每一层所添加的配置.文件. Dockerfile就是一个脚本来构建和定制镜像,把每一层的修改.安装.构建.操作都写入脚本.以此来解决体积.镜像构建 ...

  9. httpclient x-www-form-urlencoded

    1. 使用Apache httpclient提交post请求 http工具方法(需指定编码, 否则出错,这里用的UTF-8) public static String postWithParamsFo ...

  10. Python-并发编程(协程)

    今天说说协程 一.引子 本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两 ...