java 家谱程序的逻辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4534988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logic for family tree program
提问by dr85
I am creating a family tree program in Java, or at least trying to. I have developed several classes:
我正在用 Java 创建一个家谱程序,或者至少尝试这样做。我开发了几个类:
- 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.
- Person - 名字性别年龄等的吸气剂和吸气剂
- FamilyMember - 扩展用于设置父母和孩子的 Person getter 和 setter
- 家庭 - 由多个家庭成员和添加删除成员的方法组成
- FamilyTree 是设置关系的主要类。
I have two main problems:
我有两个主要问题:
1) I need to set the relationships between people. Currently I am doing:
1)我需要设置人与人之间的关系。目前我正在做:
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?
2)我必须能够显示家谱。我怎样才能做到这一点?是否有任何自定义类可以让生活更轻松?
Thanks for your time...
谢谢你的时间...
采纳答案by Carl Manaster
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:
关于绘制结构,如果显示超过 2 代,则很难避免冲突(线交叉)。因此,如果您的应用程序允许您将其减少到两个,那就太好了。我已经编写了许多使用这种表示的程序,无论是垂直的:
or horizontally:
或水平:
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 justsetting 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).
同时设置 B 的母亲和 A 的孩子似乎是多余的——而冗余会导致错误——选择一个。哪一个?好吧,当你设置 B 的母亲(A 的性别)时,会有更多信息,我们知道任何人都需要正好有两个父母,而不是 0 或更多的孩子。所以我倾向于只设置 B 的母亲;您总是可以通过遍历所有内容来找出其父项等于相关个体的集合,从而找出任何个体的子项。并且实际存储母亲和父亲的关系(相对于简单的父母关系)可能会减少重复(假设您正在存储个人的性别)。
回答by Andy Thomas
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?
2)我必须能够显示家谱。我怎样才能做到这一点?是否有任何自定义类可以让生活更轻松?
Yes, there is existing code that supports display of graphs. I personally had a good experience working with the prefuse visualization toolkit.
是的,有支持图形显示的现有代码。我个人在使用prefuse 可视化工具包方面有很好的经验。
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.)
您可能会找到感兴趣的树状视图;尝试单击此示例中的节点。(但是,如果您打算将您的软件供您自己以外的家庭使用,那么一棵树可能是不够的。)
回答by TalentTuner
something like below class (this is a pseudo code not a real Java Class)
类似于下面的类(这是一个伪代码,而不是真正的 Java 类)
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>();