插入二进制搜索树(Java)时的空指针

时间:2014-09-20 21:04:48

标签: java data-structures nullpointerexception binary-tree

我试图为练习创建一个随机二叉搜索树,但是当我运行程序时,我无法插入,而我得到的只是空指针异常。我以为我把所有东西都准确地放了出来,我无法弄清楚这一点。我需要调整什么才能使项目实际插入树中?任何帮助将不胜感激,如果我需要澄清任何内容或者您是否想要解释我为什么做某事,请告诉我。这是我的相关代码:

public class RandomBSTSymbolTable<K extends Comparable<K>, V> 
    implements SymbolTable<K,V>{

    static Random rng = new Random(1234);
    private Node root;

    public RandomBSTSymbolTable() {root = null;}

    private class Node {
        private K key; // line 16
        private V val;
        Node left, right;
        int N;

        public Node(K k, V v) {
            key = k;
            val = v;
            N = 1;
        }
    }

    private Node rotateLeft(Node tree) {
        Node x = tree.right;
        tree.right = x.left;
        x.left = tree;
        x.N = 1 + x.left.N + x.right.N;
        x.left.N = 1 + x.left.right.N + x.left.left.N;
        root = x;
        return x;
    }
    private Node rotateRight(Node tree) {
        Node x = tree.left;
        tree.left = x.right;
        x.right = tree;
        x.N = 1 + x.left.N + x.right.N;
        x.right.N = 1 + x.right.left.N + x.right.right.N;
        root = x;
        return x;
    }
    public void insert(K key, V val) {
        root = insertRandom(root, key, val); // line 47
    }

    private Node insertRoot(Node tree, K key, V val) {
        int cmp = key.compareTo(tree.key); // line 51
        if(cmp < 0) {
            tree.left = insertRoot(tree.left, key, val);
            tree = rotateRight(tree);
        }
        else {
            tree.right = insertRoot(tree.right, key, val); // line 57
            tree = rotateLeft(tree);
        }
        return tree;
    }

    public int size(Node tree) {
        return (tree == null) ? 0 : tree.N;
    }


    private Node insertRandom(Node tree, K key, V val) {
        if (tree == null) return new Node(key, val);
        if(rng.nextDouble()*(tree.N+1) < 1.0)
            return insertRoot(tree, key, val); // line 71
        int cmp = key.compareTo(tree.key);
        if(cmp == 0) {
            tree.key = key; tree.val = val;
        } else if (cmp < 0) {
            tree.left = insertRandom(tree.left, key, val);
            tree.N = 1 + tree.left.N + size(tree.right);
        } else {
            tree.right = insertRandom(tree.right, key, val); // line 79
            tree.N = 1 + size(tree.left) + size(tree.right);
        }
        return tree;
    }

    public V search(K key) {
        return searchAux(root, key);
    }

    private V searchAux(Node tree, K key) {
        if (tree == null)
            return null;
        int cmp = key.compareTo(tree.key);
        if (cmp == 0)
            return tree.val;
        return (cmp < 0) ? searchAux(tree.left, key) : searchAux(tree.right, key);
    }

public static void main(String args[]) {
    RandomBSTSymbolTable<Integer,Integer> symtab = new RandomBSTSymbolTable<>();
    for(int i = 0; i < 100; i++) {
        symtab.insert(i, i); // line 117
    }

    }
}

以下是我尝试运行程序时遇到的错误:

Exception in thread "main" java.lang.NullPointerException
    at RandomBSTSymbolTable$Node.access$0(RandomBSTSymbolTable.java:16)
    at RandomBSTSymbolTable.insertRoot(RandomBSTSymbolTable.java:51)
    at RandomBSTSymbolTable.insertRoot(RandomBSTSymbolTable.java:57)
    at RandomBSTSymbolTable.insertRandom(RandomBSTSymbolTable.java:71)
    at RandomBSTSymbolTable.insertRandom(RandomBSTSymbolTable.java:79)
    at RandomBSTSymbolTable.insertRandom(RandomBSTSymbolTable.java:79)
    at RandomBSTSymbolTable.insert(RandomBSTSymbolTable.java:47)
    at RandomBSTSymbolTable.main(RandomBSTSymbolTable.java:117)

0 个答案:

没有答案