验证二叉树的Java方法?[重复]

xdnvmnnf  于 2023-04-19  发布在  Java
关注(0)|答案(2)|浏览(111)

此问题已在此处有答案

What is a NullPointerException, and how do I fix it?(12个回答)
2天前关闭。
好的,我试图解决的问题是创建一个递归线性时间算法来验证每个节点上的BT。

public class BinaryTree {
    Node root;

    private Node addR(Node c, int x, Node parent) {
        if (c == null) {
            c = new Node(x);
            c.parent = parent;
            return c;
        }
        if (x < c.value) {
            parent = c;
            c.left = addR(c.left, x, c);
        }
        else if (x > c.value) {
            parent = c;
            c.right = addR(c.right, x, c);
        }
        else {
            return c;
        }
        return c;
    }
    
    public void add(Node parent, int x) {
        root = addR(root, x, parent);
    }
    
    static int max(Node n) {
        if (n == null) {
            return Integer.MIN_VALUE;
        }
        int value = n.value;
        int leftMax = max(n.left);
        int rightMax = max(n.right);
        return Math.max(value, Math.max(leftMax, rightMax));
    }
        
    static int min(Node n) {
        if (n == null) {
            return Integer.MAX_VALUE;
        }
        int value = n.value;
        int leftMax = min(n.left);
        int rightMax = min(n.right);
        
        return Math.min(value,  Math.min(leftMax, rightMax));
    }
    
    static int verifyBST(Node n) {
        if (n.left != null && max(n.left) > n.value) {
            return 0;
        }
        
        if (n.right != null && min(n.right) < n.value) {
            return 0;
        }
        if (verifyBST(n.left) != 1 || verifyBST(n.right) != 1) {
             return 0;
        }
        return 1;
     }
    
    static void verifyBSTout(int x) {
        if (x == 1) {
            System.out.println ("Confirmed Binary Tree.");
        }
        else {
            System.out.println ("Not a Binary Tree.");
        }
    }
}

和我的主;

public class Main {
    public static void main(String[] args) {
        BinaryTree test = new BinaryTree();
        test.add(test.root, 7);
        test.add(test.root, 5);
        test.add(test.root, 6);
        test.add(test.root, 9);
        test.add(test.root, 3);
        test.add(test.root, 8);
        test.verifyBSTout(BinaryTree.verifyBST(test.root));
        
    }
}

我检查以确保test.root.left.value有一个值,但是当它到达实际测试时,它说n是null并抛出NullPointException。我有点困惑,因为首先,我正在传递我刚刚填充的测试,其次,verifyBST方法应该只在n!= null时使用该行,所以我不确定为什么它会因为n!= null而崩溃。
有谁知道我的错误在哪里吗?先谢谢你了。

64jmpszr

64jmpszr1#

代码中的问题是,在第一次调用addR()时,将null作为父节点传递。这会导致根节点中的父变量保持为null,并导致verifyBST()中的NullPointerException
要解决这个问题,您需要更改add()以检查父节点是否为null,并将其设置为root。
下面是更新后的代码:

public void add(Node parent, int x) {
    if (parent == null) {
        root = addR(root, x, null);
    } else {
        addR(root, x, parent);
    }
}

通过此修改,您可以使用以下命令删除对add()的第一个调用:
test.add(test.root, 7);
并将其改为:
test.add(null, 7);
这将设置根节点并避免verifyBST()中的NullPointerException

ee7vknir

ee7vknir2#

n.leftnull时,verifyBST方法正在调用verifyBST(n.left)-这会导致NPE。如果您按以下方式更改它,则NPE不会发生:

static int verifyBST(Node n) {
    if (n.left != null && max(n.left) > n.value && verifyBST(n.left) != 1) {
        return 0;
    }

    if (n.right != null && min(n.right) < n.value && verifyBST(n.right) != 1) {
        return 0;
    }

    return 1;
}

相关问题