二叉树的前序,中序,后序和层序遍历java实现

二叉树的定义:

二叉树(Binary Tree) 是n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两颗互不相交的、分别称为根结点的左子树和右子树的二叉树组成。

二叉树的性质:

性质1:在二叉树的第i层上至多有2i-1个结点(i>=1)。
性质2:深度为k的二叉树至多有2k-1个结点(k>=1)。
性质3:对任何一颗二叉树T,如果其终端结点数为n0,度为2的 结点数为n2,则n0 = n2+1.
性质4:具有n个结点的完全二叉树深度为[log2n+1] ([x]表示不 大于x的最大整数)。
性质5:如果对一颗有n个结点的完全二叉树(其深度为[log2n]+1) 的结点按层序编号(从第1层到第[log2n]+1层,每层从左到右),对任意一个结点i(1<=i<=n)有:
1).如果i=1,则结点i是二叉树的根,无双亲;如果i>1,则其双亲是结 点[i/2]
2).如果2i>n,则结点i无左孩子(结点i为叶子结点);否则其左孩 子是结点2i。
3).如果2i+1>n,则结点i无右孩子;否则其右孩子是结点2i+1。

二叉树的遍历方式:

前序遍历:

规则是若二叉树为空,则空操作返回,否则先访问跟结点,然后前序遍历左子树,再前序遍历右子树


中序遍历:

规则是若树为空,则空操作返回,否则从根结点开始(注意并不是先访问根结点),中序遍历根结点的左子树,然后是访问根结点,最后中序遍历右子树


后序遍历:

规则是若树为空,则空操作返回,否则从左到右先叶子后结点的方式遍历访问左右子树,最后是访问根结点


层序遍历:

规则是若树为空,则空操作返回,否则从树的第一层,也就是根结点开始访问,从上而下逐层遍历,在同一层,按从左到右的顺序对结点逐个访问


二叉树对象:

	public class TreeNode{
		private String data;
		private TreeNode leftChild;
		private TreeNode rightChild;
		private boolean isFirstCome = true;//是否第一次访问,非递归后序遍历使用
		
	}

前序遍历

/**
	 * 前序遍历——递归
	 * @author Administrator
	 *
	 */
	public void preOrder(TreeNode node){
		if(node == null){
			return;
		}else{
			System.out.print(node.getData()+",");
			preOrder(node.leftChild);
			preOrder(node.rightChild);
		}
	}
	
	/**
	 * 前序遍历——非递归
	 */
	
	public void nonRecursionPreOrder(TreeNode node){
		if(node == null){
			return;
		}
		//采用栈
		Stack<TreeNode> stack = new Stack<TreeNode>();
		stack.push(node);
		while(!stack.isEmpty()){
			TreeNode n = stack.pop();
			System.out.print(""+n.getData()+",");
			if(n.rightChild!=null){
				stack.push(n.rightChild);
				
			}
			if(n.leftChild!=null){
				stack.push(n.leftChild);
			}
		}
	}

中序遍历:

/**
	 * 中序遍历——递归
	 * @author Administrator
	 *
	 */
	public void midOrder(TreeNode node){
		if(node == null){
			return;
		}else{
			midOrder(node.leftChild);
			System.out.print(node.getData()+",");
			midOrder(node.rightChild);
		}
	}
	/**
	 * 中序遍历——非递归
	 */
	public void nonRecursionMinOrder(TreeNode node){
		if(node == null){
			return;
		}
		Stack<TreeNode> stack = new Stack<TreeNode>();
		TreeNode n = node;
		while(n!=null || !stack.isEmpty()){
			while(n!=null){
				stack.push(n);
				n = n.leftChild;
			}
			n = stack.pop();
			System.out.print(""+n.getData()+",");
			n = n.rightChild;
		}
	}

后续遍历:

/**
	 * 后序遍历——递归
	 * @author Administrator
	 *
	 */
	public void postOrder(TreeNode node){
		if(node == null){
			return;
		}else{
			postOrder(node.leftChild);
			postOrder(node.rightChild);
			System.out.print(node.getData()+",");
		}
	}
	
	/**
	 * 后序遍历——非递归
	 */
	public void nonRecursionPostOrder(TreeNode node){
		if(node == null){
			return;
		}
		Stack<TreeNode> stack = new Stack<TreeNode>();
		TreeNode n = node;
		int index = -1;
		while(n!=null || !stack.isEmpty()){
			while(n!=null){
				stack.push(n);
				stack.get(++index).isFirstCome = true;
				n = n.leftChild;
			}
			if(index!=-1 && stack.get(index).isFirstCome){
				n = stack.get(index);
				stack.get(index).isFirstCome = false;
				n = n.rightChild;
			}else{
				while(index!=-1 && !stack.get(index).isFirstCome){
					n = stack.pop();
					index--;
					System.out.print(""+n.getData()+",");
				}
				n = null;
			}
		}
	}

层序遍历:

public void levelOrder(TreeNode node){
		if(node == null){
			return;
		}
		//采用队列
		LinkedList<TreeNode> queue = new LinkedList<>();
		queue.offer(node);
		while(!queue.isEmpty()){
			TreeNode n = queue.poll();
			System.out.print(""+n.getData()+",");
			if(n.leftChild != null){
				queue.offer(n.leftChild);
			}
			if(n.rightChild != null){
				queue.offer(n.rightChild);
			}
		}
	}

以上遍历方式没有经过严格测试,仅供学习测试使用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值