为什么 BST 的 findNode 方法中的这一行被标记为 "dead code"?
Why is this line in findNode method of BST being marked "dead code"?
我正试图在 java 中实现一个 findNode 方法作为二叉搜索树的一部分。
public Node findNode(int findkey){
Node tempNode5 = root;
while(findkey != tempNode5.key){
if(tempNode5 != null){ // if the tempNode5 is not null
// and the node to be found hasn't been found yet
// then we will go to the leftChild or rightChild
// depending on the key
if(findkey < tempNode5.key){
tempNode5 = tempNode5.leftChild;
}
else tempNode5 = tempNode5.rightChild;
}
else return null; // this is the line that Eclipse marks "dead code"
// if the tempNode5 reaches null, it means the node to be found
// was not found in the BST, in which case I want to return null
}
return tempNode5; // if the while loop has exited and reached this line it means that
// the node *has* been found, so I want to return it
}
Eclipse 将行 "else return null;" 标记为死代码。我需要处理在树中找不到节点的情况,否则 while 循环将永远 运行,所以我需要类似的东西,当找不到节点时 return null。
感谢任何帮助:)
看看这些行:
while(findkey != tempNode5.key){
if(tempNode5 != null){
如果 tempNode5
为 null,while
将抛出 NullPointerException
,因此不会执行任何后续行。
因此,如果 tempNode5
不为空,控件只会进入 while
循环,从而使 if..else
变得多余。
我正试图在 java 中实现一个 findNode 方法作为二叉搜索树的一部分。
public Node findNode(int findkey){
Node tempNode5 = root;
while(findkey != tempNode5.key){
if(tempNode5 != null){ // if the tempNode5 is not null
// and the node to be found hasn't been found yet
// then we will go to the leftChild or rightChild
// depending on the key
if(findkey < tempNode5.key){
tempNode5 = tempNode5.leftChild;
}
else tempNode5 = tempNode5.rightChild;
}
else return null; // this is the line that Eclipse marks "dead code"
// if the tempNode5 reaches null, it means the node to be found
// was not found in the BST, in which case I want to return null
}
return tempNode5; // if the while loop has exited and reached this line it means that
// the node *has* been found, so I want to return it
}
Eclipse 将行 "else return null;" 标记为死代码。我需要处理在树中找不到节点的情况,否则 while 循环将永远 运行,所以我需要类似的东西,当找不到节点时 return null。
感谢任何帮助:)
看看这些行:
while(findkey != tempNode5.key){
if(tempNode5 != null){
如果 tempNode5
为 null,while
将抛出 NullPointerException
,因此不会执行任何后续行。
因此,如果 tempNode5
不为空,控件只会进入 while
循环,从而使 if..else
变得多余。