Pregunta

Estoy creando un programa de árbol en Java, o al menos intentar. He desarrollado varias clases:

  • Persona - getters y setter para el nombre la edad sexo, etc.
  • familymember - extiende captadores persona y setters para el establecimiento de los padres y los niños
  • Familia - que consta de múltiples miembros de la familia y los métodos para agregar miembros eliminación
  • FamilyTree que es la clase principal para establecer relaciones.

Tengo dos problemas principales:

1) Tengo que configurar las relaciones entre las personas. Actualmente estoy haciendo:

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

El ejemplo anterior es para establecer una relación madre hijo.

Esto parece muy torpe. Se está haciendo muy largo aliento para poner en práctica todas las relaciones. Cualquier ideas sobre cómo poner en práctica múltiples relaciones de una manera menos procedimental?

2) Tengo que ser capaz de mostrar el árbol genealógico. ¿Cómo puedo hacer esto? ¿Hay clases personalizadas por ahí para hacer la vida más fácil?

Gracias por su tiempo ...

¿Fue útil?

Solución

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

Otros consejos

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

something like below class (this is a pseudo code not a real Java Class)

class Node
{

     public Node Parent { get;set;}

     public List<Node> Childs {get;set;}

}

uses

     Node ultimateGrandParent = new Node();
     ultimateGrandParent.Parent = null;
     ultimateGrandParent.Childs = new List<Node>();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top