二叉树类:

 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. Java给树加子节点个数统计

    通过后台实现 private List<Photo> getChildren(Photo photo) { List<Photo> children = new ArrayLi ...

  2. CVE-2017-6920 Drupal远程代码执行漏洞学习

     1.背景介绍: CVE-2017-6920是Drupal Core的YAML解析器处理不当所导致的一个远程代码执行漏洞,影响8.x的Drupal Core. Drupal介绍:Drupal 是一个由 ...

  3. MySQL wait_timeout参数设置与网上常见错误小纠

    discard connection com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link fail ...

  4. [生活] 日常英语学习笔记-NEVER HAVE I EVER游戏

    逛油管,看视频,学英语. 大家要过周末了说啥 Happy Sunday Have a restful  Sunday 有个空闲的周末 我们正在看电影 We are watching movie it ...

  5. 二、单层感知器和BP神经网络算法

    一.单层感知器 1958年[仅仅60年前]美国心理学家FrankRosenblant剔除一种具有单层计算单元的神经网络,称为Perceptron,即感知器.感知器研究中首次提出了自组织.自学习的思想, ...

  6. MVC设计模式实现权限管理登录,超详细

    功能实现:在页面输入给定的用户名之一,可以显示当前用户的权限,也可以在页面更改该用户的权限,更新之后保存.像下面这样. 填写用户名提交: 显示用户AAA的权限: 修改权限(增加article3): 点 ...

  7. java温故而知新(9)OOP(面向对象编程)理念

    Object   Oriented   Programming   (面向对象的程序设计) 1.定义 面向对象程序设计(OOP)的具体定义很难下,也很容易因此而引起争论,在   Object-Orie ...

  8. Linux 安装 EPEL YUM源

    原文:https://blog.csdn.net/harbor1981/article/details/51135623 我们用yum安装软件时,经常发现我们的yum源里面没有该软件,需要自己去wge ...

  9. alert 多语言的处理

    1. code <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...

  10. 关于background定位

    直到刚刚我才发现我小瞧了background定位 因为项目里需要显示隐藏的按钮上有两个图标 开始想了几种方法都不行,然后突然就想到了background定位 果断试了一下 <input type ...