乘一先生 2023-12-29 10:41 采纳率: 33.3%
浏览 17
已结题

Java static怎么修改

怎么把除了main函数以外的所有static变量和函数变成可在类外访问的,怎么把static删掉

//在二叉排序树中,ASL = (所有节点的深度之和)/ (所有节点数量 )
public class BinarySortTreeASL {
    //定义了一个Node类来表示二叉排序树的节点
    private static class Node {
        int value;
        Node left;
        Node right;

        public Node(int value) {
            this.value = value;
        }
    }

    //在main方法中给定了二叉排序树的节点值
    public static void main(String[] args) {
        int[] values = {54, 18, 81, 12, 36, 6, 76, 99, 57, 87, 66};//给定数组

        Node root = buildBinarySortTree(values); //接受一个整数数组为参数并返回二叉排序树根节点
        double asl = calculateASL(root);//接受二叉排序树的根节点作为参数,并返回该树的ASL

        System.out.println("ASL成功= " + asl);
    }

    //用buildBinarySortTree方法构建二叉排序树
    private static Node buildBinarySortTree(int[] values) {
        Node root = null;
        //通过循环迭代插入节点来构建二叉排序树
        for (int value : values) {
            root = insert(root, value);
        }
        return root;
    }

    //处理了节点的插入逻辑,并递归调用自身来插入节点
    private static Node insert(Node node, int value) {
        if (node == null) {
            return new Node(value);
        }
        //检查新节点是否比父节点小
        if (value < node.value) {
            node.left = insert(node.left, value);
        } else if (value > node.value) {
            node.right = insert(node.right, value);
        }

        return node;
    }

    //将根节点传递给calculateASL方法来计算ASL成功
    //首先计算总深度和节点数量。然后通过总深度除以节点数量来计算ASL成功
    private static double calculateASL(Node node) {
        if (node == null) {
            return 0;
        }

        double totalDepth = calculateTotalDepth(node, 1);//初始深度设为1
        int nodeCount = countNodes(node);

        return totalDepth / nodeCount;//计算ASL成功
    }

    //使用递归计算每个节点的深度,并将其与左右子树的深度相加
    private static double calculateTotalDepth(Node node, int depth) {
        if (node == null) {
            return 0;
        }

        double leftDepth = calculateTotalDepth(node.left, depth + 1);
        double rightDepth = calculateTotalDepth(node.right, depth + 1);

        return  depth + leftDepth + rightDepth;
    }

    //使用递归计算二叉排序树中的节点数量
    private static int countNodes(Node node) {
        if (node == null) {
            return 0;
        }        
        return 1+countNodes(node.left)+countNodes(node.right);
    }
}


  • 写回答

22条回答

  • 社区专家-Monster-XH 2023-12-29 10:52
    关注
    获得0.45元问题酬金

    转化成实例就行

    public class BinarySortTreeASL {
        private class Node {
            int value;
            Node left;
            Node right;
    
            public Node(int value) {
                this.value = value;
            }
        }
    
        public static void main(String[] args) {
            BinarySortTreeASL bst = new BinarySortTreeASL();
            int[] values = {54, 18, 81, 12, 36, 6, 76, 99, 57, 87, 66};
    
            Node root = bst.buildBinarySortTree(values);
            double asl = bst.calculateASL(root);
    
            System.out.println("ASL成功= " + asl);
        }
    
        public Node buildBinarySortTree(int[] values) {
            Node root = null;
            for (int value : values) {
                root = insert(root, value);
            }
            return root;
        }
    
        private Node insert(Node node, int value) {
            if (node == null) {
                return new Node(value);
            }
            if (value < node.value) {
                node.left = insert(node.left, value);
            } else if (value > node.value) {
                node.right = insert(node.right, value);
            }
            return node;
        }
    
        public double calculateASL(Node node) {
            if (node == null) {
                return 0;
            }
    
            double totalDepth = calculateTotalDepth(node, 1);
            int nodeCount = countNodes(node);
    
            return totalDepth / nodeCount;
        }
    
        private double calculateTotalDepth(Node node, int depth) {
            if (node == null) {
                return 0;
            }
    
            double leftDepth = calculateTotalDepth(node.left, depth + 1);
            double rightDepth = calculateTotalDepth(node.right, depth + 1);
    
            return depth + leftDepth + rightDepth;
        }
    
        private int countNodes(Node node) {
            if (node == null) {
                return 0;
            }        
            return 1 + countNodes(node.left) + countNodes(node.right);
        }
    }
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月6日
  • 创建了问题 12月29日

悬赏问题

  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错