从上往下遍历二元树(层次遍历二元树)

题目:输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。

例如输入

      8
    /  \
   6    10
  /\     /\
5  7   9  11

输出8   6   10   5   7   9   11。

分析:这曾是微软的一道面试题。这道题实质上是要求遍历一棵二元树,只不过不是我们熟悉的前序、中序或者后序遍历。

我们从树的根结点开始分析。自然先应该打印根结点8,同时为了下次能够打印8的两个子结点,我们应该在遍历到8时把子结点6和10保存到一个数据容器中。现在数据容器中就有两个元素6 和10了。按照从左往右的要求,我们先取出6访问。打印6的同时要把6的两个子结点5和7放入数据容器中,此时数据容器中有三个元素10、5和7。接下来我们应该从数据容器中取出结点10访问了。注意10比5和7先放入容器,此时又比5和7先取出,就是我们通常说的先入先出。因此不难看出这个数据容器的类型应该是个队列。

既然已经确定数据容器是一个队列,现在的问题变成怎么实现队列了。实际上我们无需自己动手实现一个,因为STL已经为我们实现了一个很好的deque(两端都可以进出的队列),我们只需要拿过来用就可以了。

我们知道树是图的一种特殊退化形式。同时如果对图的深度优先遍历和广度优先遍历有比较深刻的理解,将不难看出这种遍历方式实际上是一种广度优先遍历。因此这道题的本质是在二元树上实现广度优先遍历。

import java.util.LinkedList;


public class Test_12 {

	public static void main(String[] args) {
		BTree bt = new BTree();
		int[] arr = {8,6,10,5,7,9,11};
		for(int i = 0;i<arr.length;i++){
			bt.insert(arr[i]);
		}
		PrintFromTopToButton(bt.getRoot());

	}
	public static void PrintFromTopToButton(TreeNode2 root){
		if(root == null){
			System.out.println("树为空");
			return;
		}
		LinkedList<TreeNode2> queue = new LinkedList<TreeNode2>();
		queue.add(root);
		while(queue.size()!=0){
			TreeNode2 pnode = queue.removeFirst();
			System.out.println(pnode.getKey());
			if(pnode.getLeftchild()!=null){
				queue.add(pnode.getLeftchild());
			}
			if(pnode.getRightchild() !=null){
				queue.add(pnode.getRightchild());
			}
		}
	}

}
//以下为二元树的构建

//树节点类
class TreeNode2{
	private int key;
	private TreeNode2 leftchild;
	private TreeNode2 rightchild;
	public int getKey() {
		return key;
	}
	public void setKey(int key) {
		this.key = key;
	}
	public TreeNode2 getLeftchild() {
		return leftchild;
	}
	public void setLeftchild(TreeNode2 leftchild) {
		this.leftchild = leftchild;
	}
	public TreeNode2 getRightchild() {
		return rightchild;
	}
	public void setRightchild(TreeNode2 rightchild) {
		this.rightchild = rightchild;
	}
	public TreeNode2(int key,TreeNode2 leftchild,TreeNode2 rightchild){
		this.key = key;
		this.leftchild = leftchild;
		this.rightchild = rightchild;
	}
	
}
//二元树类
class BTree{
	private TreeNode2 root;

	public TreeNode2 getRoot() {
		return root;
	}

	public void setRoot(TreeNode2 root) {
		this.root = root;
	}
	public void insert(int key){
		TreeNode2 newnode = new TreeNode2(key,null,null);
		if(root == null){
			root = newnode;
			return;
		}
		LinkedList<TreeNode2> queue = new LinkedList<TreeNode2>();
		queue.add(root);
		while(queue.size() != 0){
			TreeNode2 pnode = queue.removeFirst();
			if(pnode.getLeftchild() == null){
				pnode.setLeftchild(newnode);
				return;
			}
			if(pnode.getRightchild() == null){
				pnode.setRightchild(newnode);
				return;
			}
			queue.add(pnode.getLeftchild());
			queue.add(pnode.getRightchild());
		}
	}
	public void MidOrder(TreeNode2 root){
		if(root == null){
			System.out.println("树为空");
			return;
		}
		if(root.getLeftchild()!=null){
			MidOrder(root.getLeftchild());
		}
		System.out.println(root.getKey());
		if(root.getRightchild()!=null){
			MidOrder(root.getRightchild());
		}
	}
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值