二叉树的实现

import java.util.LinkedList;
import java.util.List;

public class createBinaryTree {

    int [] array={1,2,3,5,6,7,8,9,11,23,45};
    static List<BinaryNode> nodelist=null;
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        createBinaryTree ct=new createBinaryTree();
        ct.createBinaryTree();
        BinaryNode root=nodelist.get(0);
        ct.preOrder(root);
        int high=ct.hight(root);
        System.out.println("hight is "+high+"");
        int size=ct.size(root);
        System.out.println("size is "+size+"");
    }

    
    public void createBinaryTree(){
        
        nodelist=new LinkedList<BinaryNode>();
        // 将一个数组的值依次转换为Node节点  
        for(int i=0;i<array.length;i++){
            
            nodelist.add(new BinaryNode(array[i]));
            
        }
        
        // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
        for(int parentIndex=0;parentIndex<array.length/2-1;parentIndex++){
             // 左孩子 
            nodelist.get(parentIndex).leftchild=nodelist.get(parentIndex*2+1);
            // // right孩子 
            nodelist.get(parentIndex).rightchild=nodelist.get(parentIndex*2+2);
            
        }
        
        // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理  
        int lastparentIndex=array.length/2-1;
        nodelist.get(lastparentIndex).leftchild=nodelist.get(lastparentIndex*2+1);
         // 右孩子,如果数组的长度为奇数才建立右孩子 
        if(array.length%2==1){
            
            
            nodelist.get(lastparentIndex).rightchild=nodelist.get(lastparentIndex*2+2);
            
        }
        
        
    }
    
    //树的高度
    public int hight(BinaryNode node){
        
        if(node==null){
            return 0;
        }
        
        int i=hight(node.leftchild);
        int j=hight(node.rightchild);
        
        return (i<j)?(j+1):(i+1);
        
    }
    //节点个数
    public int size(BinaryNode node){
        if(node==null){
            
            return 0;
        }
        
        return 1+size(node.leftchild)+size(node.rightchild);
    }
    
    //先序遍历
    public void preOrder(BinaryNode node){
        
        if(node==null){
            
            return;
            
            
        }
        System.out.println(node.data);
        preOrder(node.leftchild);
        preOrder(node.rightchild);
    }
    
    //中序遍历
    public  void inorder(BinaryNode node) {
        
        if(node==null){
            
            
            
            return;
        }
        
        
        
        preOrder(node.leftchild);
        System.out.print(node.data);
        preOrder(node.rightchild);
        
        
    }
    
    //后序遍历
    public void behandorder(BinaryNode node){
        
        if(node==null){
            return;
        }
        
        preOrder(node.leftchild);
        
        preOrder(node.rightchild);
        
        System.out.print(node.data);
    }
    
    
    
    
    private  class BinaryNode{
        
        BinaryNode leftchild;
        BinaryNode rightchild;
        int data;
        
        BinaryNode(int newdata){
            leftchild=null;
            rightchild=null;
            data=newdata;    
        }
        
    }
    
    
}

 

posted on 2016-11-17 20:26  猫儿爹  阅读(191)  评论(0编辑  收藏  举报

导航