二叉树的基本遍历方法

包括二叉树的前、中、后序递归及非递归遍历方法,层序遍历方法

/*
 * 二叉树的前、中、后、层序遍历
 * author:xc 2019/4/8 14:33
 */
package jianzhioffer;

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

public  class BinaryTree {
	
	public static class BinaryTreeNode
	{
		int val;
		BinaryTreeNode left;
		BinaryTreeNode right;
		
		public BinaryTreeNode (int val, BinaryTreeNode left, BinaryTreeNode right) {
			super();
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}
	
	//前序遍历递归方法
	public void preOrder(BinaryTreeNode root)
	{
		if(root != null)
		{
			System.out.print(root.val + " ");
			preOrder(root.left);
			preOrder(root.right);
		}
	}
	
	//前序遍历非递归方法
	public void preOrderNonRecursive(BinaryTreeNode root)
	{
		Stack<BinaryTreeNode> stack = new Stack<>();
		while(true)
		{
			while(root != null)
			{
				System.out.print(root.val + " ");
				stack.push(root);
				root = root.left;
			}
			if(stack.empty()) break;
			root = stack.pop();
			root = root.right;
		}
	}
	
	//中序遍历递归方法
	public void inOrder(BinaryTreeNode root)
	{
		if(root != null)
		{
			inOrder(root.left);
			System.out.print(root.val + " ");
			inOrder(root.right);
		}
		
	}
	
	//中序遍历非递归方法
	public void inOrderNonRecursive(BinaryTreeNode root)
	{
		Stack<BinaryTreeNode> stack = new Stack<>();
		while(true)
		{
			while(root != null)
			{
				stack.push(root);
				root = root.left;
			}
			if(stack.empty()) break;
			root = stack.pop();
			System.out.print(root.val + " ");
			root = root.right;
		}
	}
	
	
	//后序遍历递归方法
	public void postOrder(BinaryTreeNode root)
	{
		if(root != null)
		{
			postOrder(root.left);
			postOrder(root.right);
			System.out.print(root.val + " ");
		}
	}
	
	//后序遍历非递归方法
	public void postOrderNonRecursive(BinaryTreeNode root)
	{
		Stack<BinaryTreeNode> stack = new Stack<>();
		while(true)
		{
			if(root != null)
			{
				stack.push(root);
				root = root.left;
			}
			else
			{
				if(stack.empty()) return;
				
				if(stack.lastElement().right == null)
				{
					root = stack.pop();
					System.out.print(root.val + " ");
					while(stack.lastElement().right == root)
					{
						System.out.print(stack.lastElement().val + " ");
						root = stack.pop();
						if(stack.empty()) break;
					}
				}
				if(!stack.empty())
					root = stack.lastElement().right;
				else
					root = null;
			}

		}
	}
	
	//层序遍历
	public void levelOrder(BinaryTreeNode root)
	{
		BinaryTreeNode temp;
		Queue<BinaryTreeNode> queue = new LinkedList<>();
		queue.offer(root);
		while(!queue.isEmpty())
		{
			temp = queue.poll();
			System.out.print(temp.val + " ");
			if(temp.left != null)
				queue.offer(temp.left);
			if(temp.right != null)
				queue.offer(temp.right);
		}
	}
	
	public static void main(String[] args)
	{
		BinaryTreeNode node7 = new BinaryTreeNode(16, null, null);
		BinaryTreeNode node6 = new BinaryTreeNode(12, null, null);
		BinaryTreeNode node5 = new BinaryTreeNode(8, null, null);
		BinaryTreeNode node4 = new BinaryTreeNode(4, null, null);
		BinaryTreeNode node3 = new BinaryTreeNode(14, node6, node7);
		BinaryTreeNode node2 = new BinaryTreeNode(6, node4, node5);
		BinaryTreeNode node1 = new BinaryTreeNode(10, node2, node3);
		
		
		BinaryTree tree = new BinaryTree();
		System.out.println("递归前序遍历:");
		tree.preOrder(node1);
		
		
		System.out.println();
		System.out.println("非递归前序遍历:");
		tree.preOrderNonRecursive(node1);
		
		System.out.println();
		System.out.println("递归中序遍历:");
		tree.inOrder(node1);
		
		System.out.println();
		System.out.println("非递归中序遍历:");
		tree.inOrderNonRecursive(node1);
		
		System.out.println();
		System.out.println("递归后序遍历:");
		tree.postOrder(node1);
		
		System.out.println();
		System.out.println("非递归后序遍历:");
		tree.postOrderNonRecursive(node1);
		
		System.out.println();
		System.out.println("层序遍历:");
		tree.levelOrder(node1);
	}
}



结果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值