• 二叉树的实现


    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;    
            }
            
        }
        
        
    }
  • 相关阅读:
    Python 学习笔记:需要仔细阅读一个函数
    asp.net 实现一个简单CAS Server
    用代码旋转屏幕
    Cassandra API60 Java 代码示例
    Ajax跨域访问代理类,支持GET和POST方法
    Android 给自己的类加个事件
    Python 学习笔记: 备份工具
    Android SQLiteHelper
    转:在 CLI 中練習 Data Model
    Java修饰符public,private,protected及默认的区别
  • 原文地址:https://www.cnblogs.com/luo-mao/p/6075357.html
Copyright © 2020-2023  润新知