package go.jacob.day502;
import java.util.Stack;
/**
*
* @author Administrator 前序遍历:6 3 1 2 5 4 9 7 8 中序遍历:1 2 3 4 5 6 7 8 9 后序遍历:2 1
* 4 5 3 8 7 9 6
*/
public class Demo3 {
public static void main(String[] args) {
Node root = init();
System.out.println("|-----递归------|");
System.out.print("前序遍历:");
preOrderTraversal_rec(root);
System.out.println();
System.out.print("中序遍历:");
inOrderTraversal_rec(root);
System.out.println();
System.out.print("后序遍历:");
postOrderTraversal_rec(root);
System.out.println();
System.out.println("|-----循环------|");
preOrderTraversal_cir(root);
inOrderTraversal_cir(root);
postOrderTraversal_cir(root);
}
/*
* 递归实现:前序遍历
*/
private static void preOrderTraversal_rec(Node root) {
if (root == null)
return;
System.out.print(root.val + " ");
preOrderTraversal_rec(root.left);
preOrderTraversal_rec(root.right);
}
/*
* 递归实现:中序遍历
*/
private static void inOrderTraversal_rec(Node root) {
if (root == null)
return;
inOrderTraversal_rec(root.left);
System.out.print(root.val + " ");
inOrderTraversal_rec(root.right);
}
/*
* 递归实现:后续遍历
*/
private static void postOrderTraversal_rec(Node root) {
if (root == null)
return;
postOrderTraversal_rec(root.left);
postOrderTraversal_rec(root.right);
System.out.print(root.val + " ");
}
/*
* 循环实现:前序遍历
*/
private static void preOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
Stack<Node> stack = new Stack<Node>();
System.out.print("先序遍历:");
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
System.out.print(root.val + " ");
root = root.left;
} else {
root = stack.pop();
root = root.right;
}
}
System.out.println();
}
/*
* 循环实现:中序遍历
*/
private static void inOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
Stack<Node> stack = new Stack<Node>();
System.out.print("中序遍历:");
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
System.out.print(root.val + " ");
root = root.right;
}
}
System.out.println();
}
/*
* 循环实现:后续遍历
*/
private static void postOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
System.out.print("后序遍历:");
Stack<Node> stack = new Stack<Node>();
Stack<Node> output = new Stack<Node>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
output.push(root);
root=root.right;
}else{
root=stack.pop();
root=root.left;
}
}
while(!output.isEmpty()){
System.out.print(output.pop().val+" ");
}
System.out.println();
}
private static Node init() {
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A; // 返回根节点
}
}
class Node {
int val = 0;
Node left = null;
Node right = null;
public Node(int val, Node left, Node right) {
this.val = val;
this.left = left;
this.right = right;
}
}