zoukankan      html  css  js  c++  java
  • 二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度

    二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度计算

    输入格式:如   abd###ce##f##*

    package tree;
    //二叉树的二叉链表实现
    
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    import java.util.Stack;
    
    
    public class BTree<AnyType> {
    
    	BTNode rootNode=new BTNode();
    	
    	  class BTNode<AnyType>{
    		char data;
    		BTNode<AnyType> leftChildNode;
    		BTNode<AnyType> rightChildNode;
    		public BTNode(){
    			data=0;
    			leftChildNode=rightChildNode=null;
    		}
    		public BTNode(char data){
    			this.data=data;
    			leftChildNode=rightChildNode=null;
    		}
    		public BTNode(char data,BTNode leftChildNode,BTNode rightChildNode){
    			this.data=data;
    			leftChildNode=leftChildNode;
    			rightChildNode=rightChildNode;
    		}
    	}
    
    	 //先序创建二叉树
            char d[]=new char[100];
    		int i=0;
    		public BTNode creatBTree(){
    			BTNode node=null;
    		    if(d[i]!='*'){
    			    if(d[i]=='#'){
    				    node=null;
    				    i++;
    			    }
    			    else{
    				    node=new BTNode(d[i]);
    				    i++; 
    				    node.leftChildNode=creatBTree();   
    				    node.rightChildNode=creatBTree();			
    			}
    			}
    			return node;
    		}
    
    	//先序递归遍历
    	public void preOrder(BTNode<AnyType> t){
    		if(t!=null){
    			System.out.print(t.data);
    			preOrder(t.leftChildNode);
    			preOrder(t.rightChildNode);
    		}
    	}
    	//先序非递归遍历
    	public void preStackOrder(BTNode t){
    		Stack s=new Stack();
    		BTNode p=t;
    		while(p!=null&&s.isEmpty()!=true){
    			if(p!=null){
    				System.out.print(p.data);
    				s.push(p);
    				p=p.leftChildNode;
    			}
    			if(s.isEmpty()){
    				s.pop();
    				p=p.rightChildNode;
    			}
    		}
    	}
    	//中序递归遍历
    	public void inOrder(BTNode t){
    		if(t!=null){
    			inOrder(t.leftChildNode);
    			System.out.print(t.data);
    			inOrder(t.rightChildNode);
    		}
    		
    	}
    	//中序非递归遍历
        public void inStackOrder(BTNode t){
      
        	Stack<BTNode> s=new Stack<BTNode>();
        	BTNode p=t;
        	while(p!=null&&s.isEmpty()){
        		if(p!=null){
        			s.push(p);
        			p=p.leftChildNode;
        		}
        		if(s.isEmpty()!=true){
        			p=s.pop();
        			System.out.print(p.data);
        			p=p.rightChildNode;
        		}
        	}
        }
    	//后序递归遍历
    	public void postOrder(BTNode t){
    		if(t!=null){
    			postOrder(t.leftChildNode);
    			postOrder(t.rightChildNode);
    			System.out.print(t.data);
    		}
    	}
    	//后序非递归遍历
    	public void postStackOrder(BTNode t){
    		Stack<BTNode> s=new Stack<BTNode>();
    		Stack<Integer> ss=new Stack<Integer>();
            Integer i=new Integer(1);
    		BTNode p=t;
    		BTNode q=t;
    		while(p!=null||s.isEmpty()!=true){	
    			while(p!=null){
    				s.push(p);
    				ss.push(new Integer(0));
    				p=p.leftChildNode;	
    			}
    			while(s.isEmpty()!=true&&ss.peek().equals(i)){
    				ss.pop();
    				q=s.pop();
    				System.out.print(q.data);	
    			}
    			if(s.isEmpty()!=true){
    				ss.pop();
    				ss.push(i);
    				p=s.peek();
    				p=p.rightChildNode;	
    			}	
    		}	
    	}
         //层次非递归遍历
    	public void levelQueueOrder(BTNode t){
    		Queue<BTNode> q=new LinkedList<BTNode>();
    		q.add(t);
    		while(q.isEmpty()!=true){
    			BTNode step=q.remove();
    			System.out.print(step.data);
    			if(step.leftChildNode!=null){
    				q.add(step.leftChildNode);
    			}
    			if(step.rightChildNode!=null){
    				q.add(step.rightChildNode);
    			}
    		}
    	}
    	//计算二叉树深度
    	public int depth(BTNode t){
    		int leftDepth,rightDepth;
    		if(t==null) 
    			return 0;
    		leftDepth=depth(t.leftChildNode);
    		rightDepth=depth(t.rightChildNode);
    		return Math.max(leftDepth,rightDepth)+1;
    	}
    	
    	//叶子结点个数
    	int num=0;
    	public int leaf(BTNode t){
    		if(t!=null){
    			if(t.leftChildNode==null&&t.rightChildNode==null)
    				num++;
    		    leaf(t.leftChildNode);
    		    leaf(t.rightChildNode);
    		}
    		return num;
    	}
    	
    	public static void main(String[] args) {
    	    BTree bt=new BTree();
    		Scanner sc=new Scanner(System.in);
    		String a=sc.next();
    		char c[]=a.toCharArray();
    		for(int i=0;i<c.length;i++){
    		     bt.d[i]=c[i];
    		}
    		bt.rootNode=bt.creatBTree();
    		System.out.println("先序遍历");
    		bt.preOrder(bt.rootNode);
    		System.out.println();
    		System.out.println("中序遍历");
    		bt.inOrder(bt.rootNode);
    		System.out.println();
    		System.out.println("后序遍历");
    		bt.postOrder(bt.rootNode);
    		System.out.println();
    		System.out.println("后序非递归遍历");
    		bt.postStackOrder(bt.rootNode);
    		System.out.println("层次遍历");
    		bt.levelQueueOrder(bt.rootNode);
    		System.out.println();
    		System.out.println("二叉树深度");
    		System.out.println(bt.depth(bt.rootNode));
    		System.out.println("叶子结点个数");
    		System.out.println(bt.leaf(bt.rootNode));
    	}
    
    }
    
    
    
    
  • 相关阅读:
    单例模式
    面向对象编程(一)
    杨辉三角形
    静态方法,Arrays类,二维数组
    数组,查找算法,二分查找法,冒泡排序,选择排序,插入排序
    万年历(二)
    循环结构
    万年历(一)
    条件结构
    类型转换,位运算符
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3752253.html
Copyright © 2011-2022 走看看