【发布时间】:2016-11-20 08:10:38
【问题描述】:
这是一个比较大的项目,但我会尽量把所有必要的东西都放在这里。
/** Removes the record with Key k from the dictionary. It throws a
DictionaryException if the record is not in the dictionary. */
public void remove(Key k) throws DictionaryException{
deleteNode = findNode(k);
if (deleteNode == null) throw new DictionaryException("Error: Record doesn't exist in the dictionary!");
else{
//check if children are leafs
if(deleteNode.getLeftChild() == null || deleteNode.getRightChild() == null)
//set it to itself
replace = deleteNode;
else
//otherwise replace with successorNode
replace = successorNode(deleteNode);
//store left child if it exists
if (replace.getLeftChild() != null)
child = replace.getLeftChild();
//else, store right
else
child = replace.getRightChild();
//check if both nodes are null
if (child != null)
child.setParent(replace.getParent());
//else replace the node that needs to be deleted
else{
//replace left child of parent
if(replace == replace.getParent().getLeftChild())
replace.getParent().setLeftChild(child);
//else replace right
else
replace.getParent().setRightChild(child);
}
//store information of the replacing node, within the deleteNode
if (replace != deleteNode)
deleteNode.setRoot(replace.getRecord());
}
}
此方法在父对象上存在空指针错误。 我不知道如何处理它。
这是存储在 BST 中的有序字典。节点由由 (Key,data) 组成的 Records 组成,其中 Key 是 (name,type)。 Record 本质上是 ((name,type),data)。
如有需要,我可以提供更多信息。我被困在这里很长一段时间了,感谢您的帮助!
【问题讨论】:
-
欢迎来到 Stack Overflow!看来您需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果您之后仍有问题,请随时回来提供更多详细信息。
标签: java binary-search-tree binary-search removeclass ordereddictionary