直接上代码,一切尽在代码中。本类可以直接使用,可以按照您的用途做简单修改。单元测试部分在main函数。转载请著名出处,谢谢。
package com.datastructure.tree;
import java.util.Stack;
/**
* 二叉树的遍历算法Java实现版本(递归/非递归/先序/中序/后序)
*
* Note: 1. 要求JDK5及其以上
*
*
* @author Ciro Deng(ciro.deng@qq.com)
* @version 1.0
*
*/
public class TraverseBinTree {
/**
* 首先定义树的类型,使用泛型以保证对不同data类型的兼容.
*
* @param <T>
* 不同data类型
*/
public static class Node<T> {
Node<T> lchild;
Node<T> rchild;
T data;
public Node(Node<T> lchild, Node<T> rchild, T data) {
super();
this.lchild = lchild;
this.rchild = rchild;
this.data = data;
}
@Override
public String toString() {
return data.toString();
}
}
// ===================非递归算法============================//
/**
* 二叉树先序非递归遍历算法。
*
* @param <T>
* 不同data类型
* @param t
* 二叉树的根节点
*/
public static <T> void preOrderUnrecur(Node<T> t) {
if (t == null)
return;
Stack<Node<T>> stack = new Stack<Node<T>>();
stack.push(t);
while (!stack.isEmpty()) {
while (stack.peek() != null) {
System.out.print(stack.peek());
stack.push(stack.peek().lchild);
}
Node<T> p = stack.pop();
if (!stack.isEmpty()) {
p = stack.pop();
stack.push(p.rchild);
}
}
}
/**
* 二叉树中序非递归遍历算法。
*
* @param <T>
* 不同data类型
* @param t
* 二叉树的根节点
*/
public static <T> void inOrderUnrecur(Node<T> t) {
if (t == null)
return;
Stack<Node<T>> stack = new Stack<Node<T>>();
stack.push(t);
while (!stack.isEmpty()) {
while (stack.peek() != null) {
stack.push(stack.peek().lchild);
}
Node<T> p = stack.pop();
if (!stack.isEmpty()) {
System.out.print(stack.peek());
p = stack.pop();
stack.push(p.rchild);
}
}
}
/**
* 二叉树后序非递归遍历算法。
*
* @param <T>
* 不同data类型
* @param t
* 二叉树的根节点
*/
public static <T> void postOrderUnrecur(Node<T> t) {
if (t == null)
return;
Stack<Node<T>> stack = new Stack<Node<T>>();
stack.push(t);
Node<T> p = null;
boolean tag = true;
while (!stack.isEmpty()) {
t = stack.peek();
while (t != null && tag) {
stack.push(t.lchild);
t = t.lchild;
}
if (tag) {
stack.pop();
}
if (!stack.isEmpty()) {
if (stack.peek().rchild == p) {
t = stack.pop();
System.out.print(t);
p = t;
tag = false;
} else {
stack.push(stack.peek().rchild);
tag = true;
p = null;
}
}
}
}
/**
* 二叉树中序非递归遍历算法. 写的冗繁,不建议使用.
*
* @param <T>
* 不同data类型
* @param t
* 二叉树的根节点
* @deprecated
*/
@SuppressWarnings("unused")
private static <T> void inOrderUnrecurByCiro(Node<T> t) {
// 空二叉树,直接返回。
if (t == null)
return;
boolean started = false;
Stack<Node<T>> stack = new Stack<Node<T>>();
stack.push(t);
while (!stack.isEmpty()) {
t = stack.pop();
if ((stack.isEmpty() && !started)
|| (!stack.isEmpty() && stack.peek().lchild != t)) {
stack.push(t);
while (t.lchild != null) {
stack.push(t.lchild);
t = t.lchild;
}
} else {
stack.push(t);
}
t = stack.pop();
System.out.print(t); // 访问节点操作
started = true;
if (t.rchild != null) {
stack.push(t.rchild);
}
}
}
// ===================递归算法============================//
/**
* 二叉树先序递归遍历算法。
*
* @param <T>
* 不同data类型
* @param t
* 二叉树的根节点
*/
public static <T> void preOrderRecur(Node<T> t) {
if (t != null) {
System.out.print(t);
preOrderRecur(t.lchild);
preOrderRecur(t.rchild);
}
}
/**
* 二叉树中序递归遍历算法。
*
* @param <T>
* 不同data类型
* @param t
* 二叉树的根节点
*/
public static <T> void inOrderRecur(Node<T> t) {
if (t != null) {
inOrderRecur(t.lchild);
System.out.print(t);
inOrderRecur(t.rchild);
}
}
/**
* 二叉树后序递归遍历算法。
*
* @param <T>
* 不同data类型
* @param t
* 二叉树的根节点
*/
public static <T> void postOrderRecur(Node<T> t) {
if (t != null) {
postOrderRecur(t.lchild);
postOrderRecur(t.rchild);
System.out.print(t);
}
}
// ===================单元测试============================//
public static void main(String[] args) {
/*
* 样本二叉树
* A
* |
* |---------|
* B C
* | |
* |---------| -----|
* D E F
* |
* ----|
* G
*/
Node<String> g = new Node<String>(null, null, "G");
Node<String> e = new Node<String>(null, null, "E");
Node<String> f = new Node<String>(null, null, "F");
Node<String> d = new Node<String>(null, g, "D");
Node<String> b = new Node<String>(d, e, "B");
Node<String> c = new Node<String>(null, f, "C");
Node<String> a = new Node<String>(b, c, "A");
System.out.println(" ");
System.out.println("* 样本二叉树");
System.out.println("* A");
System.out.println("* | ");
System.out.println("* |---------|");
System.out.println("* B C");
System.out.println("* | |");
System.out.println("* |---------| -----|");
System.out.println("* D E F");
System.out.println("* |");
System.out.println("* ----|");
System.out.println("* G");
System.out.print("非递归先序遍历结果:");
preOrderUnrecur(a);
System.out.println();
System.out.print("递归先序遍历结果:");
preOrderRecur(a);
System.out.println();
System.out.print("非递归中序遍历结果:");
inOrderUnrecur(a);
System.out.println();
System.out.print("递归中序遍历结果:");
inOrderRecur(a);
System.out.println();
System.out.print("非递归后序遍历结果:");
postOrderUnrecur(a);
System.out.println();
System.out.print("递归后序遍历结果:");
postOrderRecur(a);
System.out.println();
}
}
=============执行结果==============
* 样本二叉树
* A
* |
* |---------|
* B C
* | |
* |---------| -----|
* D E F
* |
* ----|
* G
非递归先序遍历结果:ABDGECF
递归先序遍历结果:ABDGECF
非递归中序遍历结果:DGBEACF
递归中序遍历结果:DGBEACF
非递归后序遍历结果:GDEBFCA
递归后序遍历结果:GDEBFCA