【问题标题】:why is the average height of my BST so high?为什么我的 BST 的平均身高这么高?
【发布时间】:2021-07-22 12:01:04
【问题描述】:

我目前正在从事一个 uni 项目,并且我的二叉搜索树有些困难,每个节点都必须有一个值,但如果节点平衡值是 0 到 1 之间的随机“平衡值”比它的父母多,那么树需要旋转,左右旋转取决于孩子坐在哪一边。

public class RandomBST {
    class Node {
        int x;
        double balanceValue;
        Node parent;
        Node LChild;
        Node RChild;

        public Node(int i, double b) {
            x = i;
            balanceValue = b;
            parent = this;
            LChild = RChild =  null;
        }
    }

    Node root;

    public double randomDouble() {
        Random Ran = new Random();
        return (0 + (1 - 0) * Ran.nextDouble());
    }

    public void insert(int i) {
        double b = randomDouble();
        root = Rec_insert(root, i, b);
        Node p = findParent(root,i,-1);
        if (p.balanceValue < b ){
            if (p.x > i){
                rotateLeft();
            }else{
                rotateRight();
            }
        }
    }

    Node Rec_insert(Node root, int i, double b) {
        if (root == null) {
            root = new Node(i, b);
            return root;
        }
        if (i < root.x)
            root.LChild = Rec_insert(root.LChild, i, b);
        else if (i > root.x)
            root.RChild = Rec_insert(root.RChild, i, b);


        return root;
    }

    static Node findParent(Node node,int i, int parent) {
        if (node == null)
            return null;
        if (node.x == i) {
            return node.parent;
        } else {
            findParent(node.LChild, i, node.x);
            findParent(node.RChild, i, node.x);
        }
        return node.parent;
    }



    int findMax(int a, int b){
        if(a >= b)
            return a;
        else
            return b;
    }

    int findHeight(Node root){
        if(root == null)
            return 0;

        return findMax(findHeight(root.LChild), findHeight(root.RChild)) + 1;
    }
    
    public void rotateRight(){
        Node previoius = root;
        if (root.RChild!=null){
            root = root.RChild;
        }
        previoius.RChild = root.LChild;
        root.LChild = previoius;
    }
    
    public void rotateLeft(){
        Node previoius = root;
        if (root.LChild!=null){
            root = root.LChild;
        }
        previoius.LChild = root.RChild;
        root.RChild = previoius;
    }

 public static void main(String[] args) {
        int total = 0;
        for (int j = 0; j<1000;j++) {
            RandomBST RBST = new RandomBST();
            for (int i = 0; i < 1000; i++) {
                RBST.insert(i);
            }
            int height = RBST.findHeight(RBST.root);
            total =total + height;
        }
        System.out.println(total/1000);

    }

}

任何关于我哪里出错的建议都很棒,输出应该是 20 到 21 左右,但我得到了 850 左右。

【问题讨论】:

  • 这段代码错误太多。例如,parent 成员始终是自引用,因此所有依赖于它的逻辑都会出错。另一件事:当旋转方法中的if 条件为假时,旋转会出错。另外:你确定你应该按递增顺序插入值吗?我建议您按字面意思引用作业。
  • 是的,这些值是按递增顺序插入的
  • 你能编辑你的问题并引用作业/挑战吗?
  • 以上评论只涉及到一些问题。还有更多:findParent 方法总是最终返回给定节点的父节点,不一定是具有给定值的节点的父节点。 findParent 的最后一个参数从未被使用过......你真的应该调试你的代码,只用一棵树来完成这项工作,比如说 5 个节点(0、1、2、3、4)。调试和检查变量。这段代码问题太多了。
  • 您确定需要旋转树的,而不是插入节点的吗?

标签: java binary-search-tree


【解决方案1】:

制作一个全新的随机数生成器
Random Ran = new Random();

可能让你的随机数...有点随机。

在您的应用程序中创建一个生成器并将所有调用定向到它。

【讨论】:

  • 感谢您的建议,它仍然返回平均 850 左右,所以不是问题,但还是谢谢您!
  • @danCodes,你怎么把这个答案标记为已接受,同时你说它没有解决问题?
猜你喜欢
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 2012-04-26
  • 1970-01-01
  • 2018-09-18
相关资源
最近更新 更多