【发布时间】:2016-11-16 08:07:10
【问题描述】:
我需要在树中循环以获取所有可能的路径,我的代码中的问题是我只获得了第一条路径!
在图中,有 2 条路径 t 处理: 1-2-3-4-5-6 和 1-2-3-7-8 ,但我无法同时获得,我刚刚检索到 1- 2-3-4-5-6!
我的代码:
主要:
for (String key : synset.keySet()) { // looping in a hash of Concept and it's ID
System.out.println("\nConcept: " + key + " " + synset.get(key));
List<Concept> ancts = myOntology.getConceptAncestors(myOntology.getConceptFromConceptID(synset.get(key))); // this function retreives the root of any node.
for (int i = 0; i < ancts.size(); i++) {
System.out.print(ancts.get(i).getConceptId() + " # ");
System.out.print(getChilds(ancts.get(i).getConceptId()) + " -> "); // here, the recursive function is needed to navigate into childs..
}
System.out.println("");
}
推荐。功能:
public static String getChilds(String conId)
{
List<Concept> childs = myOntology.getDirectChildren(myOntology.getConceptFromConceptID(conId)); // get all childs of a node
if(childs.size() > 0)
{
for (int i = 0; i < childs.size(); i++) {
System.out.print( childs.size() + " ~ " + childs.get(i).getConceptId() + " -> ");
return getChilds(childs.get(i).getConceptId());
}
}
else
return "NULL";
return "final";
}
【问题讨论】:
-
问题在于您只提供了我们需要帮助您的部分信息。如果不了解您的树是如何真正构建的(在内部表示),我们就无法说出您的算法为什么不正确。所以请退后一步,转到帮助中心并阅读有关stackoverflow.com/help/mcve 的信息...您知道遍历树的方法有很多种吗? en.wikipedia.org/wiki/Tree_traversal
-
我已经清楚地说明了要求..
-
因为
getChilds方法返回String。一个节点有两个以上子节点时的问题 -
您已经说明了要求。您没有提供帮助您修复错误的信息。我们不是在这里神奇地弄清楚代码您没有显示如何不足以满足您的要求。
标签: java recursion tree ontology wordnet