Creating a java family tree

I have the following structure:

Member { String firstName; String secondName; Member[] children; Member father; } 
  • I need to implement this tree in java;
  • I have a member name and middle name. I need to find the path from root to this node. Can someone help me please?

This is what I have:

public class Member {

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

}

+4
source share
2 answers

Your structure is similar to ona, which I have in my application. I solve this problem by creating a common walker that descends three from the root and uses the visitor pattern to provide me with the result of the walk.

If you convert it to your problem, it will look like this:

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

and then the visitor interface

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

and the implementation will look like this:

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

The advantage of this solution is that you want to do something in the tree, for example, we find an element, count the number of descendants of someone, you can use a walker, all you have to do is create a new visitor.

+3
source

This Github project can help if you want to create a family tree using Java swing:

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

It uses the yFiles library for Java.

0
source

All Articles