简体   繁体   中英

java n-ary tree depth

here is my tree node class:

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;
    }
}
}

each node have its parent node and child node. But when I execute program, it give me stackoverflowerror in both recursive function (CLD and DIT). Could anyone tell me why they loop infinitely? Thanks.

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());



}
}

Without knowing if I am setting the tree up correctly or really testing that much, this works for me. The main issue was you were using the classes recuDIT/CLD and not the Generalization object g in the method. So you were constantly looping through the same list recursively without moving on from the first index. I also changed your for each loop to a for loop because it was easier for me to see and debug.

Other than that, this gives me 3 for the length to root and length to leaf which I believe is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM