gpt4 book ai didi

java n叉树的深度

转载 作者:行者123 更新时间:2023-12-01 11:44:45 25 4
gpt4 key购买 nike

这是我的树节点类:

public class Generalization extends Class_object {
private List<Generalization> superClasses;
private List<Generalization> subClasses;

public boolean isRoot() {
return superClasses.size() == 0;
}

public boolean isLeaf() {
return subClasses.size() == 0;
}

// path length to root
public String getDIT() {
return Integer.toString(recuDIT(this));
}

public int recuDIT(Generalization g) {
if (g.isRoot())
return 0;
else {
int maxLength = 0;
for (Generalization gen : superClasses) {
maxLength = Math.max(maxLength, recuDIT(gen));
}
return maxLength + 1;
}
}

// path length to leaf
public String getCLD() {
return Integer.toString(recuCLD(this));
}

public int recuCLD(Generalization g) {
if (g.isLeaf())
return 0;
else {
int maxLength = 0;
for (Generalization gen : subClasses) {
maxLength = Math.max(maxLength, recuCLD(gen));
}
return maxLength + 1;
}
}
}

每个节点都有其父节点和子节点。但是当我执行程序时,它在两个递归函数(CLD 和 DIT)中给出了 stackoverflowerror 。谁能告诉我为什么它们无限循环?谢谢。

最佳答案

public class Generalization {
private List<Generalization> superClasses;
private List<Generalization> subClasses;

public Generalization(){
superClasses = new ArrayList<Generalization>();
subClasses = new ArrayList<Generalization>();
}

public boolean isRoot() {
return superClasses.size() == 0;
}

public boolean isLeaf() {
return subClasses.size() == 0;
}

// path length to root
public String getDIT() {
return Integer.toString(recuDIT(this));
}

public int recuDIT(Generalization g) {
if (g.isRoot())
return 0;
else {
int maxLength = 0;
for(int i = 0 ; i < g.superClasses.size(); i++){
maxLength = Math.max(maxLength, recuDIT(g.superClasses.get(i)));
}
return maxLength + 1;
}
}

// path length to leaf
public String getCLD() {
return Integer.toString(recuCLD(this));
}

public int recuCLD(Generalization g) {
if (g.isLeaf())
return 0;
else {
int maxLength = 0;
for(int i = 0 ; i < g.subClasses.size(); i++){
maxLength = Math.max(maxLength, recuCLD(g.subClasses.get(i)));
}
return maxLength + 1;
}
}

public static void main(String[] args){
Generalization root = new Generalization();

Generalization ch1 = new Generalization();
Generalization ch2 = new Generalization();

root.subClasses.add(ch1);
root.subClasses.add(ch2);

Generalization gc1 = new Generalization();
Generalization gc2 = new Generalization();
Generalization gc3 = new Generalization();

ch2.superClasses.add(root);
ch2.subClasses.add(gc1);
ch2.subClasses.add(gc2);

ch1.subClasses.add(gc3);
ch1.superClasses.add(root);

Generalization ggc1 = new Generalization();

gc3.subClasses.add(ggc1);
gc3.superClasses.add(ch1);

gc2.superClasses.add(ch2);
gc1.superClasses.add(ch2);

ggc1.superClasses.add(gc3);

System.out.println(ggc1.getDIT());
System.out.println(root.getCLD());



}
}

在不知道我是否正确设置树或是否真正测试了那么多的情况下,这对我有用。主要问题是您在方法中使用类 recuDIT/CLD 而不是泛化对象 g 。因此,您不断地递归地循环访问同一个列表,而不从第一个索引继续。我还将您的 for every 循环更改为 for 循环,因为它对我来说更容易查看和调试。

除此之外,这给了我 3 的根部长度和叶子长度,我认为这是正确的。

关于java n叉树的深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29242174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com