我有一个使用内部二叉树作为数据结构的类。我有一个奇怪的问题。我递归地寻找一个特定的节点。如果找到该节点,我希望代码返回对它的引用。问题:返回后它总是空的!但为什么呢?
private HeapNode getHeapNode(double key) {
//ELEMENT IS NOT AVAILABLE HERE, NULL-Pointer, ALWAYS
return getHeapNodeRec(key, rootHeapNode);
}
private HeapNode getHeapNodeRec(double key, HeapNode curr) {
if (curr == null) {
return null;
}
if (curr.getKey() == key) {
//ELEMENT IS AVAILABLE HERE, I can access its data!
return curr;
}
else {
getHeapNodeRec(key, curr.getLeft());
getHeapNodeRec(key, curr.getRight());
}
return null;
}
答案 0 :(得分:1)
你获得null
的原因是因为你在退出后退出了节点:
else {
getHeapNodeRec(key, curr.getLeft());
getHeapNodeRec(key, curr.getRight());
}
return null;
您搜索左侧和右侧节点,但在获得该搜索结果后,您不保留结果,只返回null
,一直传播null
根调用。
你应该做的是:
else {
HeapNode leftResult = getHeapNodeRec(key, curr.getLeft());
if (leftResult != null) {
return leftResult;
}
HeapNode rightResult = getHeapNodeRec(key, curr.getRight());
if (rightResult != null) {
return rightResult;
}
}
答案 1 :(得分:0)
您需要返回递归获取的值,如此。
sql_context(spark_context).sql.read.json(s3path)