繁体   English   中英

红黑树实现空指针异常

[英]Red Black Tree Implementation Null Pointer Exception

我正在尝试使用教科书的伪代码实现RBT,但是我收到了空指针异常。 我尝试添加对null的检查,但是它崩溃了,而在更下方的地方出现了另一个null。 我的猜测是,我不应该有那么多的空检查开始(否则伪代码会反映出来的)。 无论如何,以下是我相关的代码部分。 我将不胜感激,至少可以帮助您缩小问题的范围:

public class RBtree {

    public static Node root; //root of RBT

    private class Node{
        private String key; //an identifying field inducing a total ordering
        private Node left; //left child (may be NULL)
        private Node right; //right child (may be NULL)
        private Node parent; //parent node (NULL for root)
        private String color;

        //constructor 
        public Node(String key){
            this.key = key;
            left = null;
            right = null;
            color = "red";

        }

    }

    public void addNode(String word){
        Node toInsert = new Node(word);
        Node parent = null;
        Node current = root;
        while(current != null){
            //System.out.println("root = " + root + " current = " + current);
            parent = current;
            if(toInsert.key.compareTo(current.key) > 0){
                current = current.left;
            }else{
                current = current.right;
            }
        }
        toInsert.parent = parent;
        if(parent == null){
            root = toInsert;
        }else if(toInsert.key.compareTo(parent.key) > 0){
            parent.left = toInsert;
        }else{
            parent.right = toInsert;
        }
        toInsert.left = null;
        toInsert.right = null;
        toInsert.color = "red";
        RBinsertFixUp(toInsert);

    }

    public void RBinsertFixUp(Node toFix){
        Node parent = null;
        while(toFix.parent.color.equals("red")){ //CRASH NULL POINTER
            if(toFix.parent.equals(toFix.parent.parent.left)){
                parent = toFix.parent.parent.right;     
                if(parent != null){
                    // begin case#1
                    if(parent.color.equals("red")){
                        toFix.parent.color = "black";
                        parent.color = "black";
                        toFix.parent.parent.color = "red";
                        toFix = toFix.parent.parent;
                    } //end case#1
                    else if(toFix.equals(toFix.parent.right)){ 
                        toFix = toFix.parent; //case#2
                        leftRotate(toFix.parent.parent); //case#2
                    }
                    toFix.parent.color = "black"; //case#3
                    toFix.parent.parent.color = "red"; //case#3
                    rightRotate(toFix.parent.parent); //case#3
                }
            }
            else{
                parent = toFix.parent.parent.left;      
                if(parent != null){
                    // begin case#1
                    if(parent.color.equals("red")){
                        toFix.parent.color = "black";
                        parent.color = "black";
                        toFix.parent.parent.color = "red";
                        toFix = toFix.parent.parent;
                    } //end case#1
                    else if(toFix.equals(toFix.parent.left)){ 
                        toFix = toFix.parent; //case#2
                        leftRotate(toFix.parent.parent); //case#2
                    }
                    toFix.parent.color = "black"; //case#3
                    toFix.parent.parent.color = "red"; //case#3
                    rightRotate(toFix.parent.parent); //case#3
                }

            }

        }
        root.color = "black";
    }
    // left rotation
    public void leftRotate(Node toRotate){
        Node parent = toRotate.right; //set parent
        toRotate.right = parent.left; // turn parent's left subtree into toRotate's right subtree
        if(parent.left != null){
            parent.left.parent = toRotate;
        }
        parent.parent = toRotate.parent; // link toRotate's parent to parent
        if(toRotate.parent == null){
            root = parent;
        }
        else if(toRotate.equals(toRotate.parent.left)){
            toRotate.parent.left = parent;
        }
        else{
            toRotate.parent.right = parent;
        }
        parent.left = toRotate; // put toRotate on parent's left
        toRotate.parent = parent;
    }

    // right rotation
    public void rightRotate(Node toRotate){
        Node parent = toRotate.left; //set parent
        toRotate.left = parent.right; // turn parent's right subtree into toRotate's left subtree
        if(parent.right != null){
            parent.right.parent = toRotate;
        }
        parent.parent = toRotate.parent; // link toRotate's parent to parent
        if(toRotate.parent == null){
            root = parent;
        }
        else if(toRotate.equals(toRotate.parent.right)){
            toRotate.parent.right = parent;
        }
        else{
            toRotate.parent.left = parent;
        }
        parent.right = toRotate; // put toRotate on parent's right
        toRotate.parent = parent;
    }

}

主要类别:

public class RBtreeTester {

    static String dictionaryName = "dictionary.txt";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        RBtree testerTree = new RBtree();

        testerTree.addNode("hello");
        testerTree.addNode("bye");
        testerTree.addNode("hi");
        testerTree.addNode("goodbye");
        testerTree.addNode("goodmorning");
        testerTree.addNode("goodevening");

    }

}

堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at RBtree$Node.access$8(RBtree.java:10)
    at RBtree.RBinsertFixUp(RBtree.java:53)
    at RBtree.addNode(RBtree.java:47)
    at RBtreeTester.main(RBtreeTester.java:13)

仅从堆栈跟踪中调试代码比添加打印语句的调试代码更难。 您只需要记住在提交程序(如果是家庭作业)或发货(如果是工作)之前将其删除。

不过,在这种情况下,我认为堆栈跟踪具有您需要的所有信息。 如果您插入的节点是新的根节点,那么当您调用RBinsertFixUp时, parent节点将为NULL ,而RBinsertFixUp尝试做的第一件事就是访问该节点的父节点的方法。 这将导致Java退出并显示NullPointerException。

没错,由经验丰富的程序员编写的Java代码往往对NULL的检查较少。 但是,这不仅仅是算法的功能; 那是因为他们已经练习过思考到底在哪里可以在运行NULL地方运行他们的代码,并且知道只在此处进行检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM