前序遍历:
1.访问根节点
2.前序遍历左子树
3.前序遍历右子树


中序遍历:
1.中序遍历左子树
2.访问根节点
3.中序遍历右子树


后序遍历:
1.后序遍历左子树
2.后序遍历右子树
3.访问根节点
---------------------

package design;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack; public class BinTree {
char data;
BinTree leftChild;
BinTree rightChild;
public BinTree(char c) {
data = c;
}
public static void preSearch(BinTree root){
if(root !=null){
System.out.print(root.data);
preSearch(root.leftChild);
preSearch(root.rightChild);
}
}
public static void midSearch(BinTree root){
if(root !=null){
midSearch(root.leftChild);
System.out.print(root.data);
midSearch(root.rightChild);
}else{
return;
}
}
public static void postSearch(BinTree root){
if(root !=null){
postSearch(root.leftChild);
postSearch(root.rightChild);
System.out.print(root.data);
}
}
// 先序遍历非递归
public static void preOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
while(root !=null || !s.empty()){
while(root!=null){
System.out.print(root.data);
s.push(root);
root = root.leftChild;
}
if(!s.empty()){
root = s.pop();
root = root.rightChild;
}
}
}
// 中序遍历非递归
public static void midOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
while(root!=null || !s.empty()){
while(root!=null){
s.push(root);
root = root.leftChild;
}
if(!s.empty()){
root =s.pop();
System.out.print(root.data);
root = root.rightChild;
}
}
}
// 后序遍历非递归
public static void postOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
Stack<Integer> s2 = new Stack<Integer>();
Integer i = new Integer();
while(root!=null || !s.empty()){
while(root!=null){
s.push(root);
s2.push(new Integer());
root = root.leftChild;
}
while(!s.empty() && s2.peek().equals(i)){
s2.pop();
System.out.print(s.pop().data);
}
if(!s.empty()){
s2.pop();
s2.push(new Integer());
root =s.peek();
root = root.rightChild;
}
}
}
//计算二叉树的深度
public static int level(BinTree root){
if(root == null){
return ;
}
return level(root.leftChild)+>level(root.rightChild)+?level(root.leftChild)+:level(root.rightChild)+; }
//层序遍历二叉树
public static void levelTrav(BinTree root) {
if (root == null)
return;
Queue<BinTree> q = new ArrayDeque<BinTree>();
q.add(root);
BinTree cur;
while (!q.isEmpty()) {
cur = q.peek();
System.out.print(cur.data + " ");
if (cur.leftChild != null)
q.add(cur.leftChild);
if (cur.rightChild != null)
q.add(cur.rightChild);
q.poll();
}
}
public static void main(String[] args) {
BinTree b1 = new BinTree('a');
BinTree b2 = new BinTree('b');
BinTree b3 = new BinTree('c');
BinTree b4 = new BinTree('d');
BinTree b5 = new BinTree('e'); /**
* a
* / \
* b c
* / \
* d e
*/
b1.leftChild = b2;
b1.rightChild = b3;
b2.leftChild = b4;
b2.rightChild = b5; BinTree.preSearch(b1);
System.out.println();
BinTree.preOrder(b1);
System.out.println("========================");
BinTree.midSearch(b1);
System.out.println("");
BinTree.midOrder(b1);
System.out.println("========================");
BinTree.postSearch(b1);
System.out.println();
BinTree.postOrder(b1);
System.out.println("========================");
System.out.println(BinTree.level(b1));
System.out.println("========================");
BinTree.levelTrav(b1);
}
}

JAVA递归、非递归遍历二叉树的更多相关文章

  1. java创建二叉树并实现非递归中序遍历二叉树

    java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...

  2. 二叉树的先序、中序以及后序遍历(递归 && 非递归)

    树节点定义: class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } 递归建立二 ...

  3. 【数据结构】——搜索二叉树的插入,查找和删除(递归&非递归)

    一.搜索二叉树的插入,查找,删除 简单说说搜索二叉树概念: 二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值 若它的右 ...

  4. 最近公共祖先 LCA 递归非递归

    给定一棵二叉树,找到两个节点的最近公共父节点(LCA).最近公共祖先是两个节点的公共的祖先节点且具有最大深度.假设给出的两个节点都在树中存在. dfs递归写法 查找两个node的最近公共祖先,分三种情 ...

  5. Reverse Linked List 递归非递归实现

    单链表反转--递归非递归实现 Java接口: ListNode reverseList(ListNode head) 非递归的实现 有2种,参考 头结点插入法 就地反转 递归的实现 1) Divide ...

  6. Java实现二叉树的创建、递归/非递归遍历

    近期复习数据结构中的二叉树的相关问题,在这里整理一下 这里包含: 1.二叉树的先序创建 2.二叉树的递归先序遍历 3.二叉树的非递归先序遍历 4.二叉树的递归中序遍历 5.二叉树的非递归中序遍历 6. ...

  7. 二叉树的递归,非递归遍历(java)

    import java.util.Stack; import java.util.HashMap; public class BinTree { private char date; private ...

  8. 二叉树——遍历篇(递归/非递归,C++)

    二叉树--遍历篇 二叉树很多算法题都与其遍历相关,笔者经过大量学习.思考,整理总结写下二叉树的遍历篇,涵盖递归和非递归实现. 1.二叉树数据结构及访问函数 #include <stdio.h&g ...

  9. 树的广度优先遍历和深度优先遍历(递归非递归、Java实现)

    在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.广度优先遍历 英文缩写为BFS即B ...

  10. 二叉树的递归,非递归遍历(C++)

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

随机推荐

  1. KVM---利用 libvirt+qemu-kvm 创建虚拟机

    KVM 虚拟化已经是一个工业级的虚拟化解决方案了,以前都是直接下载 VMware,然后安装其他操作系统的,今天我们来体验一下自己动手创建一台虚拟机,这样你就会知道在KVM下创建一台虚拟机,是多么简单的 ...

  2. PAT Advanced 1015 Reversible Primes (20) [素数]

    题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...

  3. 题解【[BJOI2015]树的同构】

    切了省选题+紫题,来写个题解 这题其实挺水,才120行代码 该题写了我一天(上午1.5h,晚上10min = 一天) hash,对于节点A,\[hashval[A] = \{hashval[i]\ti ...

  4. Graph & Tree

    图论学习笔记 TYQ图论真是个渣渣呢 所以TYQ决定猛补图论 好的从0x60开始 表示博客园不用Latex真的烦呢QAQ,公式难打的要命QAQ 0x60~0x62 最短路讲解跳过 最小生成树: Kru ...

  5. Reservoir Computing论文学习

    目录 背景: RC优势: 储备池计算主要理论组成: ESNS数学模型 结构表示 状态方程和输出方程 计算过程 储备池的优化 GA:使用进化算法对参数进行优化: 基于随机梯度下降法的储备池参数优化 参考 ...

  6. Gson使用指南(二)

    注:此系列基于Gson 2.4. 一.Gson的流式反序列化 自动方式 常用的重载方法: Gson.toJson(Object); Gson.fromJson(Reader,Class); Gson. ...

  7. CodeForces 992B Nastya Studies Informatics + Hankson的趣味题(gcd、lcm)

    http://codeforces.com/problemset/problem/992/B  题意: 给你区间[l,r]和x,y 问你区间中有多少个数对 (a,b) 使得 gcd(a,b)=x lc ...

  8. day49-线程-事件

    #1.Event里面有一个标志flag,当e = Event()刚创建的时候,flag=False,阻塞,这个时候,e.is_set()也是等于False,阻塞. #e.set()让flag变成非阻塞 ...

  9. TPO2-1Desert Formation

    The extreme seriousness of desertification results from the vast areas of land and the tremendous nu ...

  10. 用户界面编程模式 MVC MVP MVVM

    用户界面编程模式 MVC MVP MVVM 程序 = 数据 + 算法 数据:就是待处理的东西 算法:就是代码 涉及到人机交互的程序,不可避免涉及到界面和界面上显示的数据原始方式是界面代码和逻辑代码糅合 ...