参考资料:

http://blog.csdn.net/wuwenxiang91322/article/details/12231657

环境:

Java: jdk1.8.0_91

import java.util.Stack;

public class BinaryTreeTest {
/**
* 树结构如下:
* root
* / \
* A B
* / \ \
* C D G
* / / / \
* E F H I
*
*/
public static void main(String[] args) {
Node root = new Node("root");
Node A = new Node("A"), B = new Node("B"), C = new Node("C"), D = new Node("D"), E = new Node("E"),
F = new Node("F"), G = new Node("G"), H = new Node("H"), I = new Node("I");
root.setLeft(A);
A.setLeft(C);
A.setRight(D);
C.setLeft(E);
D.setLeft(F);
root.setRight(B);
B.setRight(G);
G.setLeft(H);
G.setRight(I); BinaryTreeTraversalor tree = BinaryTreeTraversalor.PRE_ORDER;
System.out.print(String.format("%s->", tree.getStrategy()));
tree.execute(root); tree = BinaryTreeTraversalor.IN_ORDER;
System.out.print(String.format("\n%s->", tree.getStrategy()));
tree.execute(root); tree = BinaryTreeTraversalor.POST_ORDER;
System.out.print(String.format("\n%s->", tree.getStrategy()));
tree.execute(root); tree = BinaryTreeTraversalor.PRE_ORDER_NORECU;
System.out.print(String.format("\n%s->", tree.getStrategy()));
tree.execute(root); tree = BinaryTreeTraversalor.IN_ORDER_NORECU;
System.out.print(String.format("\n%s->", tree.getStrategy()));
tree.execute(root); tree = BinaryTreeTraversalor.POST_ORDER_NORECU;
System.out.print(String.format("\n%s->", tree.getStrategy()));
tree.execute(root);
}
}
class Node {
private String value;
private Node left;
private Node right; public Node(String value) {
this.value = value;
} public String getValue() {
return value;
} public Node getLeft() {
return left;
} public void setLeft(Node left) {
this.left = left;
} public Node getRight() {
return right;
} public void setRight(Node right) {
this.right = right;
}
}
enum BinaryTreeTraversalor {
IN_ORDER("递归中序遍历") {
@Override
public void execute(Node node) {
if (node != null) {
execute(node.getLeft());
visit(node);
execute(node.getRight());
}
}
},
PRE_ORDER("递归先序遍历") {
@Override
public void execute(Node node) {
if (node != null) {
visit(node);
execute(node.getLeft());
execute(node.getRight());
}
}
},
POST_ORDER("递归后序遍历") {
@Override
public void execute(Node node) {
if (node != null) {
execute(node.getLeft());
execute(node.getRight());
visit(node);
}
}
},
IN_ORDER_NORECU("非递归中序遍历") {
@Override
public void execute(Node node) {
final Stack<Node> stack = new Stack<>();
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
node = stack.pop();
visit(node);
node = node.getRight();
}
}
}
},
PRE_ORDER_NORECU("非递归先序遍历") {
@Override
public void execute(Node node) {
final Stack<Node> stack = new Stack<>();
while (node != null || !stack.isEmpty()) {
while (node != null) {
visit(node);
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
node = stack.pop();
node = node.getRight();
}
}
}
},
POST_ORDER_NORECU("非递归后序遍历") {// 需要重点理解
@Override
public void execute(Node node) {
final Stack<Node> stack = new Stack<>();
Node tmp = null;
while (node != null) {
while (node.getLeft() != null) {
stack.push(node);
node = node.getLeft();
}
while (node != null && (node.getRight() == null || node.getRight() == tmp)) {// 当前结点无右子树或右子树已经输出
visit(node);
// 记录上一个已输出结点
tmp = node;
if (stack.empty())
return;
node = stack.pop();
}
stack.push(node);
node = node.getRight();
}
}
};
private static void visit(Node root) {
System.out.print(root.getValue() + "\t");
} private String strategy; BinaryTreeTraversalor(String strategy) {
this.strategy = strategy;
} public abstract void execute(Node node); public String getStrategy() {
return strategy;
}
}

Java实现二叉树遍历的更多相关文章

  1. Java实现二叉树的创建和遍历操作(有更新)

    博主强烈建议跳过分割线前面的部分,直接看下文更新的那些即可. 最近在学习二叉树的相关知识,一开始真的是毫无头绪.本来学的是C++二叉树,但苦于编译器老是出故障,于是就转用Java来实现二叉树的操作.但 ...

  2. JAVA实现二叉树(简易版--实现了二叉树的各种遍历)

    1,个人感觉二叉树的实现主要还是如何构造一颗二叉树.构造二叉树函数的设计方法多种多样,本例采用 addNode 方法实现.以下程序通过定义内部类来表示二叉树的结点,然后再实现了二叉树这种数据结构的一些 ...

  3. java 二叉树遍历

    package com.lever; import java.util.LinkedList;import java.util.Queue; /** * 二叉树遍历 * @author lckxxy ...

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

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

  5. Java实现二叉树及相关遍历方式

    Java实现二叉树及相关遍历方式 在计算机科学中.二叉树是每一个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(r ...

  6. 二叉树遍历(Java实现)

    二叉树遍历(Java实现)   主要是二叉树的遍历,包括递归遍历和非递归遍历 import java.util.ArrayDeque; import java.util.ArrayList; impo ...

  7. java实现二叉树的Node节点定义手撕8种遍历(一遍过)

    java实现二叉树的Node节点定义手撕8种遍历(一遍过) 用java的思想和程序从最基本的怎么将一个int型的数组变成Node树状结构说起,再到递归前序遍历,递归中序遍历,递归后序遍历,非递归前序遍 ...

  8. Java实现二叉树先序,中序,后序,层次遍历

    一.以下是我要解析的一个二叉树的模型形状.本文实现了以下方式的遍历: 1.用递归的方法实现了前序.中序.后序的遍历: 2.利用队列的方法实现层次遍历: 3.用堆栈的方法实现前序.中序.后序的遍历. . ...

  9. java数据结构之二叉树遍历的非递归实现

    算法概述递归算法简洁明了.可读性好,但与非递归算法相比要消耗更多的时间和存储空间.为提高效率,我们可采用一种非递归的二叉树遍历算法.非递归的实现要借助栈来实现,因为堆栈的先进后出的结构和递归很相似.对 ...

随机推荐

  1. 78-DeMarker,价格波动指数.(2015.7.1)

    DeMarker 价格波动指数 观井映天 2015.7.1

  2. Codeforces 805 D Minimum number of steps

    题意: 给定一串字符串,将所有“ab”的子串替换为“bba”,询问多少次操作后没有子串“ab”. 分析: 观察可得,将“ab”替换为“bba”有两种结果. ①a移到了b的后面 ②增加了一个b 而且最终 ...

  3. MySQL:视图、触发器、存储过程、事务

    视图: 视图,虚拟表 创建虚拟表: # 语法: # create view 虚拟表名称 as 虚拟表; create view course_and_teacher as select * from ...

  4. Codeforces698C. LRU

    n<=20种东西,有个大小k<=n的箱子,每次会以固定的概率从所有东西里选一种,若箱子里有空位且这种东西没出现过就丢进去,若箱子满了且这种东西没出现过就把最早访问过的一个丢掉,(只要在每次 ...

  5. 类(Class)

    类 · 目的 面向对象的最主要目的是提高程序的重复使用性. · 包括 属性(attribute).方法(method) · 示例 class Bird(object): have_feather = ...

  6. Apache 使用localhost(127.0.0.1)可以访问,使用本机IP(局域网)不能访问

    本机ip是:192.168.1.25,输入后提示: Forbidden You don't have permission to access / on this server 对于此问题的解决办法, ...

  7. School Marks-CodeForces

    B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. reader dc

    https://get.adobe.com/cn/reader/otherversions/

  9. 【SQL Server 学习系列】-- 随机生成日期时间的SQL脚本

    DECLARE @dt1 DATETIME,@dt2 DATETIME,@a BIGINT,@b BIGINT SET @dt1='2010-01-01'--开始日期 SET @dt2='2010-0 ...

  10. LoadRunner脚本编写

    性能測试project师要懂代码么?答案是必须的.好多測试员觉得在loadrunner中编写脚本非常难非常牛X ,主要是大多測试人员并未做过开发工作,大学的那点程序基础也忘记的几乎相同了. 还有非计算 ...