二叉树遍历

package com.bupt.hxm;

import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
	int data;
	TreeNode left;
	TreeNode right;
}

public class BinaryTree {

	/**
	 * 二叉树深度
	 * 
	 * @param root
	 * @return
	 */
	private static int depth(TreeNode root) {
		if (root == null) {
			return 0;
		} else {
			return Math.max(depth(root.left), depth(root.right)) + 1;
		}
	}

	/**
	 * 用数组层次初始化二叉树
	 * 
	 * @param root
	 * @param array
	 */
	private static void init(TreeNode root, int[] array) {
		// TODO Auto-generated method stub
<span style="white-space:pre">		</span>if (array == null || array.length == 0) {
<span style="white-space:pre">			</span>return;
<span style="white-space:pre">		</span>}
		if (root == null) {
			root = new TreeNode();
		}
		root.data = array[0];
		int i = 1;
		TreeNode t = root;
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		while (i<array.length) {
			TreeNode node = new TreeNode();
			node.data = array[i++];
			t.left = node;
			queue.add(t.left);
			if (i == array.length) {
				break;
			}

			node = new TreeNode();
			node.data = array[i++];
			t.right = node;
			queue.add(t.right);
			t = queue.poll();
			if (i == array.length) {
				break;
			}
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };
		TreeNode root = new TreeNode();
		init(root, array);

		System.out.println("==递归前序遍历==");
		preOrderRecursion(root);

		System.out.println();
		System.out.println("==非递归前序遍历==");
		preOrder(root);

		System.out.println();
		System.out.println("==递归中序遍历==");
		inOrderRecursion(root);

		System.out.println();
		System.out.println("==非递归中序遍历==");
		inOrder(root);

		System.out.println();
		System.out.println("==递归后序遍历==");
		postOrderRecursion(root);

		System.out.println();
		System.out.println("==非递归后序遍历==");
		postOrder(root);

		System.out.println();
		System.out.println("==递归层次遍历==");
		rankOrderRecursion(root);

		System.out.println();
		System.out.println("==非递归层次遍历==");
		rankOrder(root);

	}

	/**
	 * 递归前序遍历
	 * 
	 * @param root
	 */
	private static void preOrderRecursion(TreeNode root) {
		// TODO Auto-generated method stub
		if (root != null) {
			System.out.print(root.data + " ");
			preOrderRecursion(root.left);
			preOrderRecursion(root.right);
		}

	}

	/**
	 * 非递归前序遍历
	 * 
	 * @param root
	 */
	private static void preOrder(TreeNode root) {
		// TODO Auto-generated method stub
		TreeNode t = root;
		Stack<TreeNode> stack = new Stack<TreeNode>();
		while (t != null) {
			while (t != null) {
				System.out.print(t.data + " ");
				if (t.right != null) {
					stack.push(t.right);
				}
				t = t.left;
			}
			if (!stack.empty()) {
				t = stack.pop();
			}

		}

	}

	/**
	 * 递归中序遍历
	 * 
	 * @param root
	 */
	private static void inOrderRecursion(TreeNode root) {
		// TODO Auto-generated method stub
		if (root != null) {
			inOrderRecursion(root.left);
			System.out.print(root.data + " ");
			inOrderRecursion(root.right);
		}

	}

	/**
	 * 非递归中序遍历
	 * 
	 * @param root
	 */
	private static void inOrder(TreeNode root) {
		// TODO Auto-generated method stub
		TreeNode t = root;
		Stack<TreeNode> stack = new Stack<TreeNode>();
		while (t != null || !stack.empty()) {
			while (t != null) {
				stack.push(t);
				t = t.left;
			}
			if (!stack.empty()) {
				t = stack.pop();
				System.out.print(t.data + " ");
				t = t.right;
			}

		}

	}

	/**
	 * 递归后序遍历
	 * 
	 * @param root
	 */
	private static void postOrderRecursion(TreeNode root) {
		// TODO Auto-generated method stub
		if (root != null) {
			postOrderRecursion(root.left);
			postOrderRecursion(root.right);
			System.out.print(root.data + " ");

		}

	}

	/**
	 * 非递归后续遍历
	 * 
	 * @param root
	 */
	private static void postOrder(TreeNode root) {
		// TODO Auto-generated method stub
		TreeNode t = root;
		Stack<TreeNode> stack = new Stack<TreeNode>();
		while (t != null || !stack.empty()) {
			while (t != null) {
				stack.push(t);
				if (t.left != null) {
					t = t.left;
				} else {
					t = t.right;
				}
			}
			if (!stack.empty()) {
				t = stack.pop();
				System.out.print(t.data + " ");
			}
			// 该节点的右节点已经访问,出栈
			while (!stack.empty() && stack.peek().right == t) {
				t = stack.pop();
				System.out.print(t.data + " ");

			}
			if (!stack.empty()) {
				t = stack.peek().right;
			} else {
				t = null;
			}
		}

	}

	/**
	 * 非递归层次遍历
	 * 
	 * @param root
	 */
	private static void rankOrder(TreeNode root) {
		// TODO Auto-generated method stub
		TreeNode t = root;
		System.out.print(root.data + " ");
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		while (t != null) {
			if (t.left != null) {
				queue.add(t.left);
				System.out.print(t.left.data + " ");
			}
			if (t.right != null) {
				queue.add(t.right);
				System.out.print(t.right.data + " ");
			}
			t = queue.poll();
		}

	}

	/**
	 * 递归层次遍历
	 * 
	 * @param root
	 */
	private static void rankOrderRecursion(TreeNode root) {
		// TODO Auto-generated method stub
		int depth = depth(root);
		for (int i = 1; i <= depth; i++) {
			rankOrderRecursion(root, i);

		}

	}

	/**
	 * 递归层次遍历
	 * 
	 * @param root
	 * @param level
	 */
	private static void rankOrderRecursion(TreeNode root, int level) {
		// TODO Auto-generated method stub
		if (root != null && level >= 1) {
			if (level == 1) {
				System.out.print(root.data + " ");
			}
			rankOrderRecursion(root.left, level - 1);
			rankOrderRecursion(root.right, level - 1);
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值