二叉树的递归,非递归遍历,深度优先遍历,广度优先遍历

馋奶兔 提交于 2020-03-01 03:57:10


/**
 * 二叉树的定义和遍历
 */
public class BinaryTree {
	/**
	 * 打印节点的信息
	 */
	public void printNode(TreeNode<String> node){
		System.out.print(node.getData()+"  ");
	}
    /**
     * 递归先序遍历二叉树
     */
    public void preIterator(TreeNode<String> node){
    	this.printNode(node);
    	if (node.getLefNode() != null) {
			this.preIterator(node.getLefNode());
		}
    	if (node.getRigNode() != null) {
			this.preIterator(node.getRigNode());
		}
    }
    /**
     * 递归中序遍历二叉树
     */
    public void midIterator(TreeNode<String> node){
    	if (node.getLefNode() != null) {
			midIterator(node.getLefNode());
		}
    	this.printNode(node);
    	if (node.getRigNode() != null) {
			midIterator(node.getRigNode());
		}
    }
    /**
     * 递归后续遍历二叉树
     */
    public void lasIterator(TreeNode<String> node){
    	if (node.getLefNode() != null) {
			this.lasIterator(node.getLefNode());
		}
    	if (node.getRigNode() != null) {
			this.lasIterator(node.getRigNode());
		}
    	this.printNode(node);
    }
    /**
     * 非递归先序遍历二叉树
     * @return
     */
    public void  noDiGuiPreIterator(TreeNode<String> node) {
		Stack<TreeNode<String>> stack = new Stack<>();
		TreeNode<String> p = node;
		while (p != null || stack.size() > 0) {
			while(p != null){
				//压入所有的左节点,压入前访问它。左节点压入完后pop访问右节点。
				this.printNode(p);
				stack.push(p);
				p = p.getLefNode();
			}
			if(stack.size() > 0){
				p = stack.pop();
				p = p.getRigNode();
			}
		}
    }  
    /**
     * 非递归中序遍历二叉树
     * @return
     */
    public void noDiGuiMidIterator(TreeNode<String> node){
    	Stack<TreeNode<String>> stack = new Stack<>();
		TreeNode<String> p = node;
		while (p != null || stack.size() > 0) {
			while(p != null){
				stack.push(p);
				p = p.getLefNode();
			}
			if(stack.size() > 0){
				p = stack.pop();
				this.printNode(p);
				p = p.getRigNode();
			}
		}
    }

    /**
     * 非递归后续遍历二叉树
     * @return
     */
    public void noDiGuiLasIterator(TreeNode<String> node){
    	Stack<TreeNode<String>> stack = new Stack<>();    
    	TreeNode<String> p = node;
    	TreeNode<String> prev = node;    
        while (p != null || stack.size() > 0) {    
            while (p != null) {    
                stack.push(p);    
                p = p.getLefNode();    
            }    
            if (stack.size() > 0) {    
            	TreeNode<String> temp = stack.peek().getRigNode();    
                if (temp == null || temp == prev) {    
                    p = stack.pop();    
                   this.printNode(p);  
                    prev = p;    
                    p = null;    
                } else {    
                    p = temp;    
                }    
            } 
        }
    }
    
    /**
     * 不仅是二叉树,其他的多叉数也适合
     * 深度优先遍历
     */
    public void depthOrderTraversal(TreeNode<String> node){
        Stack<TreeNode<String>> stack=new Stack<>();
        if (node != null){
        	stack.push(node);       
		}
        while(stack.isEmpty()==false){
        	TreeNode<String> p = stack.pop();
            this.printNode(p);
            //栈是后进先出,因为先遍历左节点,所有左节点要后添加,即先添加右节点。
            if(p.getRigNode() != null){
                stack.push(p.getRigNode());
            }
            if(p.getLefNode() != null){
                stack.push(p.getLefNode());
            }           
        }
        System.out.print("\n");
    }
    /**
     * 不仅适合二叉树,也适合其他的多叉树
     * 广度优先遍历
     */
    public void levelOrderTraversal(TreeNode<String> node){
        Queue<TreeNode<String>> queue=new ArrayDeque<>();
        queue.add(node);
        while(queue.isEmpty()==false){
        	TreeNode<String> p = queue.remove();
        	this.printNode(p);
        	//队列是先进先出,所有先放左子树,先遍历左边的节点
        	if(p.getLefNode() != null){
                queue.add(p.getLefNode());
            }
            if(p.getRigNode() != null){
                queue.add(p.getRigNode());
            }
        }
    }
    // 初始化二叉树  
    public TreeNode<String> init() {  
        TreeNode<String> D = new TreeNode<String>("D", null, null);  
        TreeNode<String> H = new TreeNode<String>("H", null, null);  
        TreeNode<String> I = new TreeNode<String>("I", null, null);  
        TreeNode<String> J = new TreeNode<String>("J", null, null);  
        TreeNode<String> P = new TreeNode<String>("P", null, null);  
        TreeNode<String> G = new TreeNode<String>("G", P, null);  
        TreeNode<String> F = new TreeNode<String>("F", null, J);  
        TreeNode<String> E = new TreeNode<String>("E", H, I);  
        TreeNode<String> B = new TreeNode<String>("B", D, E);  
        TreeNode<String> C = new TreeNode<String>("C", F, G);  
        TreeNode<String> A = new TreeNode<String>("A", B, C);  
        return A;  
    } 
   /**
    * 测试
    * @param args
    */
    public static void main(String[] args) {
		BinaryTree binaryTree = new BinaryTree();
		TreeNode<String> node = binaryTree.init();
		
		System.out.println("二叉树的递归先序遍历");
		binaryTree.preIterator(node);
		System.out.println("\n二叉树的非递归先序遍历");
		binaryTree.noDiGuiPreIterator(node);
		
		System.out.println("\n二叉树的递归中序遍历");
		binaryTree.midIterator(node);
		System.out.println("\n二叉树的非递归中序遍历");
		binaryTree.noDiGuiMidIterator(node);
		
		System.out.println("\n二叉树的递归后序遍历");
		binaryTree.lasIterator(node);
		System.out.println("\n二叉树的非递归后序遍历");
		binaryTree.noDiGuiLasIterator(node);

		System.out.println("\n数的深度优先遍历");
		binaryTree.depthOrderTraversal(node);

		System.out.println("\n数的广度优先遍历");
		binaryTree.levelOrderTraversal(node);
    }
}

/**
 * 定义节点
 */
class TreeNode<T>{
	
	private T data;
	private TreeNode<T> lefNode;
	private TreeNode<T> rigNode;
	
	public TreeNode(T data, TreeNode<T> lefNode, TreeNode<T> rigNode) {
		super();
		this.data = data;
		this.lefNode = lefNode;
		this.rigNode = rigNode;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	public TreeNode<T> getLefNode() {
		return lefNode;
	}
	public void setLefNode(TreeNode<T> lefNode) {
		this.lefNode = lefNode;
	}
	public TreeNode<T> getRigNode() {
		return rigNode;
	}
	public void setRigNode(TreeNode<T> rigNode) {
		this.rigNode = rigNode;
	}
}

结果:

二叉树的递归先序遍历
A  B  D  E  H  I  C  F  J  G  P  
二叉树的非递归先序遍历
A  B  D  E  H  I  C  F  J  G  P  
二叉树的递归中序遍历
D  B  H  E  I  A  F  J  C  P  G  
二叉树的非递归中序遍历
D  B  H  E  I  A  F  J  C  P  G  
二叉树的递归后序遍历
D  H  I  E  B  J  F  P  G  C  A  
二叉树的非递归后序遍历
D  H  I  E  B  J  F  P  G  C  A  
数的深度优先遍历
A  B  D  E  H  I  C  F  J  G  P  

数的广度优先遍历
A  B  C  D  E  F  G  H  I  J  P  

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!