【数据结构和算法分析】二叉树的遍历

二叉树的遍历

二叉树基本概念:

在计算机科学中,树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构。二叉树是每个结点最多有两个子树的有序树。子树通常被称为 “左子树” 和 “右子树”,二叉树常被用于实现二叉查找树和二叉堆。

二叉树的遍历:

二叉树的遍历大致分为先根遍历,中根遍历,后根遍历,层次遍历。按照具体的算法又可分为递归和非递归的,

代码如下:

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


public class BinaryTree {
	
	/*
	 * 测试,打印结果
	 */
	public static void main(String[] args){				
		BinaryTreeMade tree = maketree();
		
		System.out.print("先序遍历(非递归):");
		nonrecursion_preorder(tree.root);
		System.out.println();
		
		System.out.print("先序遍历(递    归):");
		preorder(tree.root);
		System.out.println();
		
		System.out.print("中序遍历(非递归):");
		nonrecursion_inorder(tree.root);
		System.out.println();
		
		System.out.print("中序遍历(递    归):");
		inorder(tree.root);
		System.out.println();
		
		System.out.print("后序遍历(非递归):");
		nonrecursion_postorder(tree.root);
		System.out.println();
		
		System.out.print("后序遍历(递    归):");
		postorder(tree.root);
		System.out.println();
		
		System.out.print("层次遍历:");
		level_order(tree.root);
	}
	
	/*
	 * 构造二叉树,根节点是                  a
	 * 根节点的左右节点是              b          c
	 * b,c的左右节点分别是          d     e    f     g
	 */
	public static BinaryTreeMade maketree(){

		BinaryNode a7 = new BinaryNode('g',null,null);
        BinaryNode a6 = new BinaryNode('f',null,null);
        BinaryNode a5 = new BinaryNode('e',null,null);
        BinaryNode a4 = new BinaryNode('d',null,null);
        BinaryNode a3 = new BinaryNode('c',a6,a7);
        BinaryNode a2 = new BinaryNode('b',a4,a5);
        BinaryNode a1 = new BinaryNode('a',a2,a3);
       		
		BinaryTreeMade tree = new BinaryTreeMade(a1);
		return tree;
		
	}
	
	/*
	 * 递归的先根遍历
	 */
	public static void preorder(BinaryNode node){
		if (node!=null) {
			System.out.print(node.element+"  ");
			preorder(node.lefttree);
			preorder(node.righttree);	
		}
	}
	/*
	 * 递归的中根遍历
	 */
	public static void inorder(BinaryNode node){
		if (node!=null) {
			inorder(node.lefttree);
			System.out.print(node.element+"  ");
			inorder(node.righttree);	
		}
	}
	/*
	 * 递归的后根遍历
	 */
	public static void postorder(BinaryNode node){
		if (node!=null) {
			postorder(node.lefttree);
			postorder(node.righttree);
			System.out.print(node.element+"  ");	
		}	
	}
    /*
     * 非递归的先根遍历
     */
	public static void nonrecursion_preorder(BinaryNode node){
		Stack<BinaryNode> stack = new Stack<BinaryNode>();
		BinaryNode b = node;
		while(b!=null||!stack.isEmpty()){
			while (b!=null) {
				System.out.print(b.element+ "  ");
				stack.push(b);
				b = b.lefttree;
			}
			if (!stack.isEmpty()) {
				b = stack.pop();
				b = b.righttree;
			}	
			else {
				return;
			}
		}
	}
    /*
     *非递归的中跟遍历
     */
	public static void nonrecursion_inorder(BinaryNode node){
		Stack<BinaryNode> stack = new Stack<BinaryNode>();
		BinaryNode b = node;
		for ( ; ; ) {
			while (b!=null) {
				stack.push(b);
				b = b.lefttree;
			}
			if (!stack.isEmpty()) {
				b = stack.pop();
				System.out.print(b.element+"  ");
				b = b.righttree;
			}
			else {
				return;
			}
		}
	}
    /*
     * 非递归的后跟遍历
     */
	public static void nonrecursion_postorder(BinaryNode node){
		Stack<BinaryNode> stack = new Stack<BinaryNode>();
		BinaryNode b = node;
		BinaryNode tmp = null;
		while(b!=null||!stack.isEmpty()){	
			while(b!=null){
				stack.push(b);
				b = b.lefttree;
			}
			if (!stack.isEmpty()) {
				tmp = stack.pop();
				if (tmp.isfirst == true) {
					tmp.isfirst = false;
					stack.push(tmp);
					b = tmp.righttree;
				}else {
					System.out.print(tmp.element+"  ");		
				}
			}		
		}	
	}
	/*
	 * 层次遍历
	 */
	public static void level_order(BinaryNode node){

		LinkedList<BinaryNode> linkedList = new LinkedList<BinaryNode>();
		BinaryNode b = node;

		while (b!=null) {
			System.out.print(b.element+"  ");
			if (b.lefttree!=null) {
				linkedList.add(b.lefttree);
			}
			if (b.righttree!=null) {
				linkedList.add(b.righttree);
			}		 		    
			b = linkedList.get(0);		
		    linkedList.remove(0);   
		    if (linkedList.size()==0) {
		    	System.out.print(b.element+"  ");
		    	break;
			}	
		}
	}
}

/*
 * 二叉树的节点类
 */
class BinaryNode{
	Object element;
	BinaryNode lefttree;
	BinaryNode righttree;
	boolean isfirst;
	
    public BinaryNode(Object e,  BinaryNode l, BinaryNode r){
    	element = e;  
    	lefttree = l;  
    	righttree = r; 
    	isfirst = true;
    }	 
}

/*
 * 二叉树的构造类
 */
class BinaryTreeMade{
	BinaryNode root;
	
	public BinaryTreeMade(BinaryNode root){
		this.root = root;
	}	
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值