美文网首页二叉树
基于JAVA实现的二叉树

基于JAVA实现的二叉树

作者: 凌风x | 来源:发表于2019-01-20 17:54 被阅读13次

    天下武功千奇百怪,武林高手神仙打架之时,以不变应万变,唯有内功深厚,方可傲视群雄

     private static BinaryTreeNode createBinaryTree(Integer[] srcArr) {
            BinaryTreeNode binaryTreeNode = new BinaryTreeNode();
            for(int x=0;x<srcArr.length;x++){
                findBiggerNode(binaryTreeNode,srcArr[x]);
            }
            return binaryTreeNode;
        }
    
        private static void findBiggerNode(BinaryTreeNode tree,Integer x){
            if(tree.getNode()==null){//根节点为空
                tree.setNode(x);
                return;
            }
             //根节点不为空
            if(tree.getNode()>x){//根节点大于目标数
                if(tree.getLeftNode()==null){//左子树为空
                    tree.setLeftNode(new BinaryTreeNode(x));//设置左子树
                }else{//左子树不为空 递归查找
                    findBiggerNode(tree.getLeftNode(),x);
                }
            }else{//目标大于等于 根节点
               if(tree.getRightNode()==null){//右子树为空 设置右子树
                   tree.setRightNode(new BinaryTreeNode(x));
               }else{//右子树不为空 递归查找
                   findBiggerNode(tree.getRightNode(),x);
               }
            }
        }
    
        public static Integer[] createArray(int length){
            Integer[] arr=new Integer[length];
             for(int x=0;x<length;x++)
                arr[x]=(int)(Math.random()*1000);
             return arr;
        }
    
        public static void main(String[] args) {
            Integer[] arr=createArray(10);
            System.out.println("原始数组");
            System.out.println(Arrays.asList(arr));
            System.out.println("=============");
    
            System.out.println("开始生成二叉树");
    
            BinaryTreeNode binaryTree = createBinaryTree(arr);
            System.out.println(binaryTree.toString());
        }
    
    }
    
    //定义根节点
    class BinaryTreeNode{
        private Integer node;
        private BinaryTreeNode  leftNode;//左子树
        private BinaryTreeNode  rightNode;//右子树
    
        public BinaryTreeNode(){}
        public BinaryTreeNode(Integer selfNode){
            this.node=selfNode;
        }
        //初始化构建
        public BinaryTreeNode(Integer selfNode,BinaryTreeNode leftNode,BinaryTreeNode rightNode){
                this.node=selfNode;
                this.leftNode=leftNode;
                this.rightNode=rightNode;
        }
    
        public Integer getNode() {
            return node;
        }
        public void setNode(Integer node) {
            this.node = node;
        }
        public BinaryTreeNode getLeftNode() {
            return leftNode;
        }
    
        public void setLeftNode(BinaryTreeNode leftNode) {
            this.leftNode = leftNode;
        }
    
        public BinaryTreeNode getRightNode() {
            return rightNode;
        }
    
        public void setRightNode(BinaryTreeNode rightNode) {
            this.rightNode = rightNode;
        }
    }
    

    相关文章

      网友评论

        本文标题:基于JAVA实现的二叉树

        本文链接:https://www.haomeiwen.com/subject/qltfjqtx.html