二叉树的生成与遍历

二叉树的生成

一层一层进行输入,如果孩子为空则输入-1

private static TreeNode createTree() {
		// TODO 自动生成的方法存根
		Scanner in = new Scanner(System.in);
		MyQueen queen=new MyQueen();
		String data=in.nextLine();
		TreeNode father=new TreeNode(data);
		TreeNode root;
		TreeNode child;
		queen.offer(father);
		while(true)
		{
			if(queen.isEmpty())
			{
				break;
			}
			root=(TreeNode) queen.peek();
			data=in.nextLine();
			if(!data.equals("-1"))
			{
				child=new TreeNode(data);
				queen.offer(child);
				root.lChild=child;
			}
			data=in.nextLine();
			if(!data.equals("-1"))
			{
				child=new TreeNode(data);
				queen.offer(child);
				root.rChild=child;
			}
			queen.poll();
		}
		return father;
	}
}

树的主要遍历方式

  • 前序遍历:根结点 ---> 左子树 ---> 右子树

  • 中序遍历:左子树---> 根结点 ---> 右子树

  • 后序遍历:左子树 ---> 右子树 ---> 根结点

  • 层次遍历(广度优先遍历):一层一层地遍历

image

上面二叉树的遍历结果

  • 前序遍历:1 2 4 5 7 8 3 6

  • 中序遍历:4 2 7 5 8 1 3 6

  • 后序遍历:4 7 8 5 2 6 3 1

  • 层次遍历:1 2 3 4 5 6 7 8

四种遍历方式的代码

/**
	 * 二叉树的先序遍历
	 * @param node 根节点
	 */
	public static void preRootTreaverse(TreeNode node)
	{
		if(node==null)
			return;
		System.out.print(node.data+" ");
		preRootTreaverse(node.lChild);
		preRootTreaverse(node.rChild);
	}
	/**
	 * 二叉树的中序遍历
	 * @param node 根节点
	 */
	public static void inRootTreaverse(TreeNode node)
	{
		if(node==null)
			return;
		inRootTreaverse(node.lChild);
		System.out.print(node.data+" ");
		inRootTreaverse(node.rChild);
	}
	/**
	 * 二叉树的后序遍历
	 * @param node
	 */
	public static void postRootTreaverse(TreeNode node)
	{
		if(node==null)
			return;
		postRootTreaverse(node.lChild);
		postRootTreaverse(node.rChild);
		System.out.print(node.data+" ");
	}
	/**
	 * 二叉树广度遍历
	 * @param root
	 */
	public static void bfs(TreeNode root)
	{
		MyQueen queen=new MyQueen();
		queen.offer(root);
		TreeNode lChirld,rChirld;
		while(!queen.isEmpty())
		{
			root=(TreeNode)queen.peek();
			System.out.print(root.data+" ");
			lChirld=root.lChild;
			if(lChirld!=null)
			{
				queen.offer(lChirld);
			}
			rChirld=root.rChild;
			if(rChirld!=null)
			{
				queen.offer(rChirld);
			}
			queen.poll();
		}
	}

二叉树叶节点的个数

/**
	 * 广度遍历获得叶节点个数
	 * @param root
	 * @return
	 */
public static int getLeftNum(TreeNode root)
	{
		int count=0;
		int flag=0;
		MyQueen queen=new MyQueen();
		queen.offer(root);
		TreeNode lChirld,rChirld;
		while(!queen.isEmpty())
		{
			root=(TreeNode)queen.peek();
			lChirld=root.lChild;
			if(lChirld!=null)
			{
				queen.offer(lChirld);
			}
			else
			{
				flag+=1;
			}
			rChirld=root.rChild;
			if(rChirld!=null)
			{
				queen.offer(rChirld);
			}
			else
			{
				flag+=1;
			}
			//如果同时没有左孩子和有孩子 那么叶节点个数加一
			if(flag==2)
			{
				count++;
			}
			//标志还原
			flag=0;
			queen.poll();
		}
		return count;
	}
	/**
	 * 通过深度遍历获得叶节点个数
	 * @param node
	 */
	public static void getLeftNum2(TreeNode node)
	{
		if(node.lChild==null&&node.rChild==null)
		{
			nums++;
			return;
		}
		if(node.lChild!=null)
			getLeftNum2(node.lChild);
		if(node.rChild!=null)
			getLeftNum2(node.rChild);
	}

二叉树根节点的个数

/**
	 * 广度遍历获得根节点个数
	 * @param root
	 * @return
	 */
	public static int getRootNum(TreeNode root)
	{
		int count=0;
		//是否为叶子
		boolean isLeft=true;
		MyQueen queen=new MyQueen();
		queen.offer(root);
		TreeNode lChirld,rChirld;
		while(!queen.isEmpty())
		{
			root=(TreeNode)queen.peek();
			lChirld=root.lChild;
			//左右孩子任意一个不为空 说明为根节点
			if(lChirld!=null)
			{
				queen.offer(lChirld);
				isLeft=false;
			}
			rChirld=root.rChild;
			if(rChirld!=null)
			{
				queen.offer(rChirld);
				isLeft=false;
			}
			if(!isLeft)
			{
				count++;
			}
			isLeft=true;
			queen.poll();
		}
		return count;
	}
	/**
	 * 通过深度遍历获得根节点个数
	 * @param node
	 */
	public static void getRootNum2(TreeNode node)
	{
		if(node.lChild==null&&node.rChild==null)
		{
			return;
		}
		else
			nums++;
		if(node.lChild!=null)
			getRootNum2(node.lChild);
		if(node.rChild!=null)
			getRootNum2(node.rChild);
	}

测试代码

public static void main(String[] args) {
		
		TreeNode father=createTree();
		preRootTreaverse(father);
		System.out.println("先序遍历");
		inRootTreaverse(father);
		System.out.println("中序遍历");
		postRootTreaverse(father);
		System.out.println("后序遍历");
		bfs(father);
		System.out.println("广度遍历");
		System.out.println("通过广度遍历获得叶节点个数:"+getLeftNum(father));
		System.out.println("通过广度遍历获得根节点个数:"+getRootNum(father));
		getLeftNum2(father);
		System.out.println("通过深度遍历获得叶节点个数:"+nums);
		nums=0;
		getRootNum2(father);
		System.out.println("通过深度遍历获得根节点个数:"+nums);
		
	}

输入数据(构建上面图的二叉树)
1
2
3
4
5
-1
6
-1
-1
7
8
-1
-1
-1
-1
-1
-1
输出结果
4 2 7 5 8 1 3 6 中序遍历
4 7 8 5 2 6 3 1 后序遍历
1 2 3 4 5 6 7 8 广度遍历
通过广度遍历获得叶节点个数:4
通过广度遍历获得根节点个数:4
通过深度遍历获得叶节点个数:4
通过深度遍历获得根节点个数:4

posted @ 2017-04-18 21:11  jiajun_geek  阅读(2766)  评论(0编辑  收藏  举报