zoukankan      html  css  js  c++  java
  • 使用一维数组创建出完全二叉树

    下面直接上代码(我是直接用的for循环初始化开始做的,也可以使用递归解决)
    这是树节点类

    package com.edu.dataStructure.treeDemo;
    
    import java.util.*;
    
    /**
     * <p>
     * 自己创建的树节点
     * </P>
     *
     */
    public class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
    
        public TreeNode(int val) {
            this.val = val;
        }
    
        public TreeNode() {
        }
    
        /**
         * <p>
         * 前序遍历:根节点+左节点+右节点
         * </p>
         */
        public void preShow() {
            if (this != null) {
                System.out.print(this.val + "	");
                if (this.left != null) {
                    this.left.preShow();
                }
                if (this.right != null) {
                    this.right.preShow();
                }
            }
    
        }
    
        /**
         * <p>
         * 中序遍历:左节点+根节点+右节点
         * </p>
         */
        public void midShow() {
            if (this != null) {
                if (this.left != null) {
                    this.left.preShow();
                }
                System.out.print(this.val + "	");
                if (this.right != null) {
                    this.right.preShow();
                }
            }
        }
    
        /**
         * <p>
         * 后序遍历:左节点+右节点+根节点
         * </p>
         */
        public void postShow() {
            if (this != null) {
                if (this.left != null) {
                    this.left.preShow();
                }
                if (this.right != null) {
                    this.right.preShow();
                }
                System.out.print(this.val + "	");
            }
        }
    
        /**
         * <p>
         * 将数组转换为完全二叉树<br>
         * 完全二叉树的特性:<br>
         * 1.第 n 个元素的左子节点为	2 * n + 1   <br>
         * 2.第 n 个元素的右子节点为	2 * n + 2   <br>
         * 3.第 n 个元素的父节点为	(n-1) / 2   <br>
         * </p>
         *
         * @return
         */
        public static TreeNode arraytoCompletebinarytree(int[] arr) {
            if (arr == null || arr.length == 0) {
                return new TreeNode();
            }
            //创建一堆树
            List<TreeNode> treeNodeList = new ArrayList<>(arr.length);
            for (int i = 0; i < arr.length; i++) {
                TreeNode treeNode = new TreeNode(i+1);
                treeNodeList.add(treeNode);
                }
                //开始构建,初始化左树和右树(常识:只要父节点有左右节点)
                for (int i = 0; i < (arr.length - 1) / 2; i++) {
                    //左
                    if (treeNodeList.get(2 * i + 1) != null) {
                        treeNodeList.get(i).left = treeNodeList.get(2 * i + 1);
                    }
                    //右
                    if (treeNodeList.get(2 * i + 2) != null) {
                    treeNodeList.get(i).right = treeNodeList.get(2 * i + 2);
                }
            }
            //最后的一个父节点的右节点要判断所以单独拿出来处理
            int lastIndex = (arr.length - 1) / 2;
            //左
            if (2 * lastIndex + 1 < arr.length) {
                treeNodeList.get(lastIndex).left = treeNodeList.get(2 * lastIndex + 1);
            }
            //右(当数组为奇数的时候才有右节点)
            if (2 * lastIndex + 2 < arr.length) {
                if (arr.length % 2 != 0) {
                    treeNodeList.get(lastIndex).right = treeNodeList.get(2 * lastIndex + 2);
                }
            }
            return treeNodeList.get(0);
        }
    }
    
    

    这是测试类

    package com.edu.dataStructure.treeDemo;
    
    /**
     * <p>
     * 该类{@link TreeNodeTest} 是{@link TreeNode} 的测试类
     * </p>
     *
     */
    public class TreeNodeTest {
        public static void main(String[] args) {
    //        TreeNode instanceToTest = getInstanceToTest();
    //        instanceToTest.preShow();
            TreeNode.arraytoCompletebinarytree(new int[]{1,2,3,4,5,6,7,8,9}).preShow();
        }
    
        /**
         * <p>得到一个测试用例</p>
         * @return
         */
        private static TreeNode getInstanceToTest() {
            TreeNode treeNode1=new TreeNode(1);
            TreeNode treeNode2=new TreeNode(2);
            TreeNode treeNode3=new TreeNode(3);
            TreeNode treeNode4=new TreeNode(4);
            TreeNode treeNode5=new TreeNode(5);
            TreeNode treeNode6=new TreeNode(6);
            TreeNode treeNode7=new TreeNode(7);
            TreeNode treeNode8=new TreeNode(8);
            TreeNode treeNode9=new TreeNode(9);
            treeNode1.left=treeNode2;
            treeNode1.right=treeNode3;
            treeNode2.left=treeNode4;
            treeNode2.right=treeNode5;
            treeNode3.left=treeNode6;
            treeNode3.right=treeNode7;
            treeNode4.left=treeNode8;
            treeNode4.right=treeNode9;
            return treeNode1;
        }
    }
    
    

    测试结果如图:在这里插入图片描述

  • 相关阅读:
    css 实现div内显示两行或三行,超出部分用省略号显示
    vue组件中的样式属性:scoped,解决在父组件中无法修改子组件样式问题
    HBuilder打包app(vue项目)
    vue动态路由传值以及get传值及编程式导航
    vue路由vue-router的安装和使用
    vue组件传值之父传子
    vue生命周期钩子函数
    vue定义组件
    vue定义自定义事件方法、事件传值及事件对象
    vue中操作Dom节点的方法
  • 原文地址:https://www.cnblogs.com/five-five/p/14347941.html
Copyright © 2011-2022 走看看