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

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);
				}

			}
		}
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值