怎么把除了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);
}
}