자바에서 이진 트리에 대해 읽었습니다.이진 검색 트리에서 노드를 찾는 방법
return (left!= null)? left.findNode(val): null;
return (right != null)? right.findNode(val): null;
당신은 다른 방법으로 그것을 다시 작성할 수 있습니다 :
public BSTNode findNode(Comparable val){
int delta = val.compareTo(value);
// the value is less than this.value
if(delta < 0){
// if there is a leftChild, return left.findNode(val)
// there is no leftChild, so the val does not exist
// in the node, so return null
return (left!= null)? left.findNode(val): null;
}
// else if the value is greater than this.value
else if (delta > 0){
// if there is a rightChild, then return right.findNode(val)
// else, there is no rightChild, return null
return (right != null)? right.findNode(val): null;
}
// else, dela == 0, so we have found the node with that
// val, return the node
return this;
}
나는 이것이 어떻게 작동하는지 이해가 안 :이 코드를 발견?
감사
당신은 삼항 조건 연산자 또는 재귀 무엇을 이해하지 못하는 – Eran
양쪽 라인 :?'리턴 (! 왼쪽 = NULL) left.findNode을 (null); right.findNode (val) : null;'어떻게 작동하는지 모르겠다. – Joe
Java 삼항 연산자 : http://alvinalexander.com/java/edu/ pj/pj010018 –