二叉树的先序遍历、中序遍历和后序遍历及层次遍历

package 二叉树遍历;

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

public class BinaryTree {
	// 二叉树结点
	public static class BinaryTreeNode {
		int value;
		BinaryTreeNode left;
		BinaryTreeNode right;

		public BinaryTreeNode(int value) {
			this.value = value;
		}

		public BinaryTreeNode(int value, BinaryTreeNode left, BinaryTreeNode right) {
			this.value = value;
			this.left = left;
			this.right = right;
		}
	}

	// 访问树的结点
	public static void visit(BinaryTreeNode node) {
		System.out.println(node.value);
	}

	// 递归版先序遍历
	public static void preOrder(BinaryTreeNode node) {
		if (node != null) {
			visit(node);
			preOrder(node.left);
			preOrder(node.right);
		}
	}

	// 递归版中序遍历
	public static void inOrder(BinaryTreeNode node) {
		if (node != null) {
			inOrder(node.left);
			visit(node);
			inOrder(node.right);
		}
	}

	// 递归版后序遍历
	public static void postOrder(BinaryTreeNode node) {
		if (node != null) {
			postOrder(node.left);
			postOrder(node.right);
			visit(node);
		}
	}

	// 非递归版先序遍历
	public static void iterativePreOrder(BinaryTreeNode node) {
		if (node == null) {
			return;
		}
		Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
		stack.push(node);
		while (!stack.isEmpty()) {
			BinaryTreeNode temp = stack.pop();
			visit(node);
			if (node.right != null) {
				stack.push(node.right);
			}
			if (node.left != null) {
				stack.push(node.left);
			}
		}
	}

	// 非递归版中序遍历
	public static void iterativeInOrder(BinaryTreeNode node) {
		if (node == null) {
			return;
		}
		Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
		while (node != null || !stack.isEmpty()) {
			while (node != null) {
				stack.push(node);
				node = node.left;
			}
			if (!stack.isEmpty()) {
				node = stack.pop();
				visit(node);
				node = node.right;
			}
		}
	}

	// 非递归版单栈后序遍历
	public static void iterativePostOrder(BinaryTreeNode node) {
		if (node == null) {
			return;
		}
		Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
		BinaryTreeNode preNode = null;
		while (node != null || !stack.isEmpty()) {
			while (node != null) {
				stack.push(node);
				node = node.left;
			}
			BinaryTreeNode temp;
			if (!stack.isEmpty()) {
				temp = stack.peek().right;
				if (temp == null || temp == preNode) {
					node = stack.pop();
					visit(node);
					preNode = node;
					node = null;
				} else {
					node = temp;
				}
			}
		}
	}

	// 非递归双栈后序遍历
	public static void iterativePostOrderBytwoStacks(BinaryTreeNode node) {
		if (node == null) {
			return;
		}
		Stack<BinaryTreeNode> stack = new Stack<>();
		Stack<BinaryTreeNode> temp = new Stack<>();
		while (node != null || !stack.isEmpty()) {
			while (node != null) {
				stack.push(node);
				temp.push(node);
				node = node.right;
			}
			BinaryTreeNode cc = null;
			if (!stack.isEmpty()) {
				node = stack.pop().left;
			}
		}
		while (!temp.isEmpty()) {
			node = temp.pop();
			visit(node);
		}
	}

	public static void layerTraversal(BinaryTreeNode root) {
		Queue<BinaryTreeNode> queue = new LinkedList<>();
		if (root != null) {
			queue.add(root);
			while (!queue.isEmpty()) {
				BinaryTreeNode currentNode = queue.poll();
				visit(currentNode);
				if (currentNode.left != null) {
					queue.add(currentNode.left);
				}

				if (currentNode.right != null) {
					queue.add(currentNode.right);
				}

			}
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_27378875/article/details/81380590