Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logic for family tree program

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...

like image 559
dr85 Avatar asked Dec 26 '10 18:12

dr85


2 Answers

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).

like image 194
Carl Manaster Avatar answered Oct 21 '22 00:10

Carl Manaster


Any ideas on how to implement multiple relationships in a less procedural way?

Yes, you can represent the relationships themselves as objects. Any two people can have zero or more relationships.

Years ago I worked on a police records system that did this more generally for associations between any two people in its master name index.

Relationships may be directed. Mother ---is-mother-of--> Child.

Relationships may be hierarchical. A mother isa parent.

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?

Yes, there is existing code that supports display of graphs. I personally had a good experience working with the prefuse visualization toolkit.

You may find the prefuse treeview of interest; try clicking on the nodes in this example. (However, if you are intending your software for use by families other than your own, a tree may be insufficient.)

like image 1
Andy Thomas Avatar answered Oct 21 '22 00:10

Andy Thomas