Java 获取树结构中所有可能的路径
我需要在树中循环以获得所有可能的路径,代码中的问题是我只获得第一条路径 例如: 在图中,有两条路径t句柄:1-2-3-4-5-6和1-2-3-7-8,但我无法同时获得这两条路径,我刚刚检索到1-2-3-4-5-6 我的代码: 大体上:Java 获取树结构中所有可能的路径,java,recursion,tree,ontology,wordnet,Java,Recursion,Tree,Ontology,Wordnet,我需要在树中循环以获得所有可能的路径,代码中的问题是我只获得第一条路径 例如: 在图中,有两条路径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 + "
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("");
}
for(字符串键:synset.keySet()){//在概念及其ID的散列中循环
System.out.println(“\n接受:“+key+”+synset.get(key));
List ancts=myOntology.getConceptOncess(myOntology.getConceptFromConceptID(synset.get(key));//此函数检索任何节点的根。
对于(int i=0;i”;//这里,需要递归函数来导航到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";
}
公共静态字符串getChilds(字符串conId)
{
List childs=myOntology.getDirectChilds(myOntology.getConceptFromConceptID(conId));//获取节点的所有子节点
如果(childs.size()>0)
{
对于(int i=0;i”);
返回getChilds(childs.get(i).getConceptId());
}
}
其他的
返回“NULL”;
返回“最终”;
}
可能getChilds()中的此代码段存在问题:
for (int i = 0; i < childs.size(); i++) {
System.out.print( childs.size() + " ~ " + childs.get(i).getConceptId() + " -> ");
return getChilds(childs.get(i).getConceptId());
}
for(int i=0;i”);
返回getChilds(childs.get(i).getConceptId());
}
for循环的不能起作用,它总是返回getChilds(childs.get(0.getConceptId())代码>
可能这不是您想要的。可能getChilds()中的此代码段存在问题:
for (int i = 0; i < childs.size(); i++) {
System.out.print( childs.size() + " ~ " + childs.get(i).getConceptId() + " -> ");
return getChilds(childs.get(i).getConceptId());
}
for(int i=0;i”);
返回getChilds(childs.get(i).getConceptId());
}
for循环的不能起作用,它总是返回getChilds(childs.get(0.getConceptId())代码>
也许这不是你想要的。一个简单的方法
您所需要的只是一个树遍历和一点自定义代码
- 有一个名为tempPath的列表。可以将其作为参数或全局变量
- 进行树遍历(如按顺序)。无论何时,只要您在一个节点上,都会将该节点添加到tempPath列表的末尾,当您处理完该节点后,就会从tempPath的末尾删除该节点
- 无论何时遇到一个叶,都有一条完整的从根到叶的路径,该路径包含在tempPath中。您可以将此列表值打印或复制到另一个数据结构中
一个简单的方法
您所需要的只是一个树遍历和一点自定义代码
- 有一个名为tempPath的列表。可以将其作为参数或全局变量
- 进行树遍历(如按顺序)。无论何时,只要您在一个节点上,都会将该节点添加到tempPath列表的末尾,当您处理完该节点后,就会从tempPath的末尾删除该节点
- 无论何时遇到一个叶,都有一条完整的从根到叶的路径,该路径包含在tempPath中。您可以将此列表值打印或复制到另一个数据结构中
我没有看到足够的代码来使用您定义的类。所以我开始写我自己的工作方案
在以下代码中,使用递归解决问题:
public class TreeNode {
private String id;
private TreeNode parent;
private List<TreeNode> children;
public TreeNode(String id) {
this.id = id;
this.children = new LinkedList<>();
}
public void addChild(TreeNode child) {
this.children.add(child);
child.setParent(this);
}
public List<TreeNode> getChildren() {
return Collections.unmodifiableList(this.children);
}
private void setParent(TreeNode parent) {
this.parent = parent;
}
public TreeNode getParent() {
return this.parent;
}
public String getId() {
return this.id;
}
}
public class TreePaths {
private static List<List<TreeNode>> getPaths0(TreeNode pos) {
List<List<TreeNode>> retLists = new ArrayList<>();
if(pos.getChildren().size() == 0) {
List<TreeNode> leafList = new LinkedList<>();
leafList.add(pos);
retLists.add(leafList);
} else {
for (TreeNode node : pos.getChildren()) {
List<List<TreeNode>> nodeLists = getPaths0(node);
for (List<TreeNode> nodeList : nodeLists) {
nodeList.add(0, pos);
retLists.add(nodeList);
}
}
}
return retLists;
}
public static List<List<TreeNode>> getPaths(TreeNode head) {
if(head == null) {
return new ArrayList<>();
} else {
return getPaths0(head);
}
}
}
这将打印出:
0-1-3
0-1-4
0-2-5
0-2-6
0-2-7-8
0-2-7-9
注意:上面的内容对于手动测试已经足够了,但是应该修改测试函数,以便为正确的自动单元测试执行正确的断言。我没有看到足够的代码来使用您定义的类。所以我开始写我自己的工作方案
在以下代码中,使用递归解决问题:
public class TreeNode {
private String id;
private TreeNode parent;
private List<TreeNode> children;
public TreeNode(String id) {
this.id = id;
this.children = new LinkedList<>();
}
public void addChild(TreeNode child) {
this.children.add(child);
child.setParent(this);
}
public List<TreeNode> getChildren() {
return Collections.unmodifiableList(this.children);
}
private void setParent(TreeNode parent) {
this.parent = parent;
}
public TreeNode getParent() {
return this.parent;
}
public String getId() {
return this.id;
}
}
public class TreePaths {
private static List<List<TreeNode>> getPaths0(TreeNode pos) {
List<List<TreeNode>> retLists = new ArrayList<>();
if(pos.getChildren().size() == 0) {
List<TreeNode> leafList = new LinkedList<>();
leafList.add(pos);
retLists.add(leafList);
} else {
for (TreeNode node : pos.getChildren()) {
List<List<TreeNode>> nodeLists = getPaths0(node);
for (List<TreeNode> nodeList : nodeLists) {
nodeList.add(0, pos);
retLists.add(nodeList);
}
}
}
return retLists;
}
public static List<List<TreeNode>> getPaths(TreeNode head) {
if(head == null) {
return new ArrayList<>();
} else {
return getPaths0(head);
}
}
}
这将打印出:
0-1-3
0-1-4
0-2-5
0-2-6
0-2-7-8
0-2-7-9
注意:上述内容对于手动测试已经足够,但是应该修改测试功能,以便为正确的自动单元测试执行正确的断言。问题在于,您只提供了我们需要帮助您的部分信息。如果不了解您的树是如何构建的(在内部表示),我们就无法解释您的算法为何不正确。所以请退后一步,转到帮助中心,阅读关于。。。你知道穿越树木有很多方法吗?我已经清楚地说明了需求。因为getChilds
方法返回String
。当一个节点有两个以上的子节点时的问题您已经说明了要求。您没有提供帮助您修复错误的信息。我们在这里不是要神奇地发现你没有显示的代码对于你的需求来说是不够的。问题是你只提供了我们需要帮助你的部分信息。如果不了解您的树是如何构建的(在内部表示),我们就无法解释您的算法为何不正确。所以请退后一步,转到帮助中心,阅读关于。。。你知道穿越树木有很多方法吗?我已经清楚地说明了需求。因为getChilds
方法返回String
。当一个节点有两个以上的子节点时的问题您已经说明了要求。您没有提供帮助您修复错误的信息。我们在这里不是要神奇地发现,您没有显示的代码对于您的需求来说是不够的。是的,这就是我所想的,但还有什么其他选择呢?恐怕我不能根据你的代码给你提供建议,因为我不理解你的代码,因为你只提供了一点你的代码。但是关于获得所有可能路径的需求,有