我试图使用教科书伪代码实现RBT,但我得到一个空指针异常。我尝试添加对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)
答案 0 :(得分:0)
仅调试来自堆栈跟踪的代码比调试已添加print statements的代码更难。您只需要记住在提交程序之前删除它们(如果它是作业)或运送它(如果它的工作正常。)
在这种情况下,我认为堆栈跟踪具有您需要的所有信息。如果您插入的节点是新的根节点,则当您调用parent
时,其NULL
将为RBinsertFixUp
,并且RBinsertFixUp
尝试执行的第一件事就是访问节点父节点的方法。这将导致Java以NullPointerException退出。
你是对的,由经验丰富的程序员编写的Java代码往往对NULL
的检查较少。这不仅仅是算法的一个特征;那是因为他们已经练习了在运行代码时遇到NULL
的确切位置,并知道只在那里放置支票。