• 平衡二叉树(AVL)Java


    1066 Root of AVL Tree (25 分)

    import java.util.Scanner;
    
    /**
     * @author RabbiT
     * 1066 Root of AVL Tree (25 分)
     */
    public class Main01 {
    
        /**
         * 树节点类
         */
        public static class TreeNode{
            int value;
            int depth;
            TreeNode left;
            TreeNode right;
    
            TreeNode(int value){
                this.value = value;
                this.depth = 1;
                this.left = null;
                this.right = null;
            }
        }
    
        /**
         * 获取当前树节点的高度
         * @param root
         * @return
         */
        public int getDepth(TreeNode root){
            if(root == null){
                return 0;
            }
            return root.depth;
        }
    
        /**
         * 获取当前树节点的平衡因子 五种情况
         * @param root
         * @return
         */
        public int getBalanceFactor(TreeNode root){
            if(root == null){
                return 0;
            }
            if(root.left == null && root.right == null){
                return 0;
            }else if(root.left == null){
                return -getDepth(root.right);
            }else if(root.right == null){
                return getDepth(root.left);
            }else{
                return getDepth(root.left) - getDepth(root.right);
            }
        }
    
        /**
         * 调整后的树需要更新节点的最新深度
         * @param root
         */
        public void updateDepth(TreeNode root){
            if(root.left == null && root.right == null){
                root.depth = 1;
            }else if(root.right == null){
                root.depth = getDepth(root.left) + 1;
            }else if(root.left == null){
                root.depth = getDepth(root.right) + 1;
            }else{
                root.depth = Math.max(getDepth(root.left),getDepth(root.right)) + 1;
            }
        }
    
        /**
         * 当遇到节点平衡因子为负数时 -1 -2 需要左旋
         * @param root
         * @return
         */
        public TreeNode leftCircle(TreeNode root){
            TreeNode temp = root.right;
            root.right = temp.left;
            temp.left = root;
            updateDepth(root);
            updateDepth(temp);
            root = temp;
            return root;
        }
    
        /**
         * 当遇到节点平衡因子为正数时 1 2 需要右旋
         * @param root
         * @return
         */
        public TreeNode rightCircle(TreeNode root){
            TreeNode temp = root.left;
            root.left = temp.right;
            temp.right = root;
            updateDepth(root);
            updateDepth(temp);
            root = temp;
            return root;
        }
    
        /**
         * 和二叉搜索树一致
         * @param root
         * @param value
         * @return
         */
        public boolean search(TreeNode root,int value){
            if(root == null){
                return false;
            }
            if(value == root.value){
                return true;
            }else if(value < root.value){
                return search(root.left,value);
            }else{
                return search(root.right,value);
            }
        }
    
        /**
         * 二叉平衡树进行插入节点操作 需要调整
         * @param root
         * @param node
         * @return
         */
        public TreeNode insert(TreeNode root,TreeNode node){
            if(root.value > node.value){
                if(root.left == null){
                    root.left = node;
                }else{
                    root.left = insert(root.left,node);
                }
            }else{
                if(root.right == null){
                    root.right = node;
                }else{
                    root.right = insert(root.right,node);
                }
            }
            updateDepth(root);
            root = adjustBalance(root);
            return root;
        }
    
        /**
         * 平衡因子为 2 -2需要调整二叉树
         * @param root
         * @return
         */
        public TreeNode adjustBalance(TreeNode root) {
            //为正数 则是L型 有LL LR两种
            if(getBalanceFactor(root) == 2){
                if(getBalanceFactor(root.left) == 1){
                    root = rightCircle(root);
                }else if(getBalanceFactor(root.left) == -1){
                    root.left = leftCircle(root.left);
                    root = rightCircle(root);
                }
            }else if(getBalanceFactor(root) == -2){
                if(getBalanceFactor(root.right) == -1){
                    root = leftCircle(root);
                }else if(getBalanceFactor(root.right) == 1){
                    root.right = rightCircle(root.right);
                    root = leftCircle(root);
                }
            }
            return root;
        }
    
        public TreeNode createTree(TreeNode[] treeArr){
            TreeNode root = treeArr[0];
            for(int i = 1;i<treeArr.length;i++){
                root = insert(root,treeArr[i]);
            }
            return root;
        }
    
        public static void main(String[] args) {
            Main01 main = new Main01();
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            TreeNode[] treeArr = new TreeNode[n];
            for(int i = 0;i<n;i++){
                treeArr[i] = new TreeNode(sc.nextInt());
            }
            TreeNode root = main.createTree(treeArr);
            System.out.println(root.value);
            System.out.println(root.left.value);
            System.out.println(root.right.value);
        }
    }
    
  • 相关阅读:
    一步一步配置 Dell OME 监控 Dell 服务器硬件报警
    比较详细的介绍zabbix监控tomcat
    Docker-03 docker 加速器--DaoCloud 1.0
    Docker-02 无人值守安装 docker
    Docker-01 无人值守升级 CentOS 6.x 系统内核到 3.10.x 长期支持版
    一个awk if 嵌套 if 的脚本
    CentOS 安装 JDK
    zTree默认勾选指定ID并执行事件
    数据库游标的使用
    js进度条实现
  • 原文地址:https://www.cnblogs.com/VictorierJwr/p/16129807.html
Copyright © 2020-2023  润新知