zoukankan      html  css  js  c++  java
  • 二叉树的实现

    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;    
            }
            
        }
        
        
    }
  • 相关阅读:
    arduino链接GY521(MPU6050)模块
    I2C Python Library ITG3205 API
    [翻译]AxureInteractive Prototypes原型设计工具Axure学习第2.3节
    [Java]XML数据的请求和DOM技术解析
    [Linux]VI相关操作
    mysql数据库连接错误问题
    关于管理单元初始化失败的解决方法
    彻底明白Java的IO系统(网上找的,还没看,先放这)
    一个关于C++ Inline关键字的引发的一个错误
    orcale 中日期类型相加的处理
  • 原文地址:https://www.cnblogs.com/luo-mao/p/6075357.html
Copyright © 2011-2022 走看看