Logic for family tree program

dr85 picture dr85 · Dec 26, 2010 · Viewed 11k times · Source

I am creating a family tree program in Java, or at least trying to. I have developed several classes:

  • Person - getters and setter for name gender age etc
  • FamilyMember - extends Person getters and setters for setting parents and children
  • Family - which consists of multiple family members and methods for adding removing members
  • FamilyTree which is the main class for setting relationships.

I have two main problems:

1) I need to set the relationships between people. Currently I am doing:

FamilyMember A, FamilyMember B
B.setMother(A);
A.setChild(B);

The example above is for setting a mother child relationship.

This seems very clunky. It's getting very long winded to implement all relationships. Any ideas on how to implement multiple relationships in a less procedural way?

2) I have to be able to display the family tree. How can I do this? Are there any custom classes out there to make life easier?

Thanks for your time...

Answer

Carl Manaster picture Carl Manaster · Dec 26, 2010

With respect to drawing the structure, it is hard to avoid collisions (lines crisscrossing) if you have more than 2 generations displayed. So if your application permits you to keep it down to two, that's great. I've written a number of programs that use this kind of a representation, either vertically:

alt text

or horizontally:

alt text

If you need more generations displayed at once, you'll need to come up with other representations, and they may start to get pretty sparse just so that you can show everyone in the same generation at the same level.

With respect to how to represent the relationships as data structures - well, it's messy. The simplest, cleanest thing is that any two individuals who are respectively mother and father of the same individual are "married". But how do you want to represent multiple partners, step-children and the like? That's hard to answer without knowing more about just what your program is supposed to do. Maybe your data set doesn't have these complications. If it does, though, it's better to think through the tricky cases first - the simple representations don't lend themselves to easy extension to cover the hard cases.

Draw (by hand) a few of the hardest cases you anticipate; that will suggest what kind of data you need to record, and how to organize it. The choices you make as you draw (who comes first, what symbols and text to use at each node, etc.) will inform your data structure decisions.

Setting both B's mother and A's child seems redundant - and redundancy leads to errors - pick one. Which one? Well, there's more information when you set B's mother (A's gender) and we know any individual will need exactly two parents, versus a 0-or-more number of children. So I would tend to go with just setting B's mother; you can always find out the children of any individual by iterating over all to pick out the set whose parent is equal to the individual in question. And actually storing Mother & Father relationships (versus simple Parent relationships) may reduce duplication (assuming you are storing gender with the individuals).