二叉树的遍历是数据结构与算法非常重要也是非常基础的内容,首先讲什么是二叉树的先序、中序以及后序遍历: 先序、中序、后序遍历其实都是对于根节点来说的。
先序遍历:其实就是对于一棵树先遍历根节点,再遍历左右孩子;中序遍历:先遍历左孩子,再遍历根节点,再遍历右孩子;后序遍历:先遍历左孩子节点,再遍历右孩子节点,再遍历左孩子节点。
如图所示:
如上图二叉树所示(咳咳,图丑了一点,不过问题不大~):
先序遍历结果:5783649
中序遍历结果:8735469
后序遍历结果:8374965
下面来看先、中、后序的递归以及非递归实现:
import java.util.Stack;
/**
* 二叉树的先序、中序、后序遍历
* 先序:先根节点再左孩子再右孩子;中序:先左孩子再根节点再右孩子;后序:先左孩子、载右孩子、再根节点(先序中序后序都是针对根节点来说的)
* @author zhmm
*
*/
public class Code_05_PreInPosTraversal {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
//二叉树先序遍历打印递归版
public static void preOrderRecur(Node head) {
if (head == null) {
return;
}
System.out.print(head.value + " ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
//二叉树中序打印递归版
public static void inOrderRecur(Node head) {
if (head == null) {
return;
}
inOrderRecur(head.left);
System.out.print(head.value + " ");
inOrderRecur(head.right);
}
//二叉树后序打印递归版
public static void posOrderRecur(Node head) {
if (head == null) {
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.print(head.value + " ");
}
//二叉树先序遍历打印非递归版
//使用栈实现
public static void preOrderUnRecur(Node head) {
System.out.print("pre-order: ");
if (head != null) {
Stack<Node> stack = new Stack<Node>();
stack.add(head);
while (!stack.isEmpty()) {
//根节点弹出即打印
head = stack.pop();
System.out.print(head.value + " ");
//孩子节点先放右孩子再放左孩子
if (head.right != null) {
stack.push(head.right);
}
if (head.left != null) {
stack.push(head.left);
}
}
}
System.out.println();
}
//二叉树中序遍历非递归版
//也是利用了栈这个数据结构,首先把这棵树的所有左边界全部压进栈里,当弄个左边界的左子树为空时,弹出并打印该节点并判断该节点的右子树是否为空
public static void inOrderUnRecur(Node head) {
System.out.print("in-order: ");
if (head != null) {
Stack<Node> stack = new Stack<Node>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
System.out.print(head.value + " ");
head = head.right;
}
}
}
System.out.println();
}
//二叉树后序遍历非递归版
//这个非常简单,因为要实现打印的方式为左右根的话,因为先序是根左右,我们只需要先按先序一样操作,只需要在压栈的时候,先序是先压右再压左,我们先压左再压右,这样就能够
//达到这次压栈压的为根右左,借助一个help栈,把上面栈的元素全部放在help栈里,然后弹出得到的就是一个弹出顺序为左右根的顺序的数据
public static void posOrderUnRecur1(Node head) {
System.out.print("pos-order: ");
if (head != null) {
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
s1.push(head);
while (!s1.isEmpty()) {
head = s1.pop();
s2.push(head);
if (head.left != null) {
s1.push(head.left);
}
if (head.right != null) {
s1.push(head.right);
}
}
while (!s2.isEmpty()) {
System.out.print(s2.pop().value + " ");
}
}
System.out.println();
}
public static void posOrderUnRecur2(Node h) {
System.out.print("pos-order: ");
if (h != null) {
Stack<Node> stack = new Stack<Node>();
stack.push(h);
Node c = null;
while (!stack.isEmpty()) {
c = stack.peek();
if (c.left != null && h != c.left && h != c.right) {
stack.push(c.left);
} else if (c.right != null && h != c.right) {
stack.push(c.right);
} else {
System.out.print(stack.pop().value + " ");
h = c;
}
}
}
System.out.println();
}
public static void main(String[] args) {
Node head = new Node(5);
head.left = new Node(3);
head.right = new Node(8);
head.left.left = new Node(2);
head.left.right = new Node(4);
head.left.left.left = new Node(1);
head.right.left = new Node(7);
head.right.left.left = new Node(6);
head.right.right = new Node(10);
head.right.right.left = new Node(9);
head.right.right.right = new Node(11);
// recursive
System.out.println("==============recursive==============");
System.out.print("pre-order: ");
preOrderRecur(head);
System.out.println();
System.out.print("in-order: ");
inOrderRecur(head);
System.out.println();
System.out.print("pos-order: ");
posOrderRecur(head);
System.out.println();
// unrecursive
System.out.println("============unrecursive=============");
preOrderUnRecur(head);
inOrderUnRecur(head);
posOrderUnRecur1(head);
posOrderUnRecur2(head);
}
}
运行结果:
==============recursive==============
pre-order: 5 3 2 1 4 8 7 6 10 9 11
in-order: 1 2 3 4 5 6 7 8 9 10 11
pos-order: 1 2 4 3 6 7 9 11 10 8 5
============unrecursive=============
pre-order: 5 3 2 1 4 8 7 6 10 9 11
in-order: 1 2 3 4 5 6 7 8 9 10 11
pos-order: 1 2 4 3 6 7 9 11 10 8 5
pos-order: 1 2 4 3 6 7 9 11 10 8 5