创建一个家谱树java

时间:2015-08-20 13:53:26

标签: java tree

我有以下结构:

Member {
    String firstName;
    String secondName;
    Member[] children;
    Member father;
}
  • 我必须在java中实现这个树;
  • 我有一个名字和一个成员的第二个名字。我需要找到从root到该节点的方法。 有谁可以帮助我吗?

这就是我所拥有的:

公共班级成员{

public List<Member> children = new ArrayList<>();
public Member father = null;
public String secondName = null;
public String firstName = null;

public Member(String secondName, String firstName) {
    this.secondName = secondName;
    this.firstName = firstName;
}

public Member(String secondName, String firstName, Member father) {
    this.secondName = secondName;
    this.firstName = firstName;
    this.father = father;
}

public List<Member> getChildren() {
    return children;
}

public void setFather(Member father) {
    this.father = father;
    father.addChild(this);

}

public void addChild(String secondName, String firstName) {
    Member child = new Member(secondName, firstName);
    child.setFather(this);
    this.children.add(child);
}

public void addChild(Member child) {
    child.setFather(this);
    this.children.add(child);
}

public String getSecondName() {
    return this.secondName;
}

public String getFirstName() {
    return this.firstName;
}

public void setSecondName(String secondName) {
    this.secondName = secondName;
}

public void setPrenume(String firstName) {
    this.firstName = firstName;
}

public boolean isRoot() {
    return (this.father == null);
}

public void deleteFather() {
    this.father = null;
}

}

2 个答案:

答案 0 :(得分:3)

您的结构类似于我在我的应用程序中的ona。我通过创建通用walker来解决这个问题,它从根向下走三个,并使用访问者模式为我提供步行结果。

如果您将其转换为您的问题,它将如下所示:

public class SimpleWalker<T>{

    private Visitor<T> visitor;


    public SimpleWalker(Visitor<T> visitor) {
        this.visitor= visitor;
    }

    public void walk(Member node) {


        if (visitor.visit(node)) {
            for (Member  child : node.children) {
                walk(child);
            }
        }
        visitor.leave(node);

    }

    public T getResult() {
        return visitor.getResult();
    }

}

然后是访客界面

public interface Visitor<T> {
    boolean visit(Member   node);
    void leave(Member   node);
    T getResult();
}

并且实现将如此

public class Pathfinder implements Visitor<List<Member>> {
        final private String firstname, secondname;//passed by constructor
        boolean found = false;
        List<Member> path = new ArrayList<>();

        public boolean visit(Member node) {
            if (node.firstname.equals(firstname)
                    && node.secondname.equals(secondname)) {
                found = true;
                return false;
            }
            return true;
        }

        public void leave(Member node) {
            if (found){
                path.add(0, node);
            }
        }

        public List<Member> getResult() {
            return path;
        }
    }

这个解决方案的优点是,无论你想在树上做什么,我们找到元素,计算某人后代的数量,你可以使用walker,你需要做的就是创建新的访问者。

答案 1 :(得分:0)

如果您想使用Java swing创建一个Family Tree,那么这个Github项目可能有所帮助:

https://github.com/r-deleon/familyTree

它使用yFiles for Java库..