二叉树的遍历(循环及递归实现)——Java

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;

	}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值