简体   繁体   English

使用数据结构在 Java 中实现家谱的最佳方法

[英]Best way of implementing a family tree in Java using a data structure

https://softwareengineering.stackexchange.com/questions/285418/name-of-data-structure-thats-tree-like-with-multiple-root-nodes https://softwareengineering.stackexchange.com/questions/285418/name-of-data-structure-thats-tree-like-with-multiple-root-nodes

I stumbled upon the above where someone answered a question regarding implementing a tree that has > 2 nodes and I just wanted to get someone's thoughts on how best to implement a family tree with parent nodes which have more than two children nodes.我偶然发现了上面有人回答了关于实现具有> 2个节点的树的问题,我只是想了解如何最好地实现具有两个以上子节点的父节点的家谱。 I had looked in Binary Trees, but since they can only have two children nodes decided to research elsewhere.我看过二叉树,但由于它们只能有两个子节点,因此决定在其他地方进行研究。 I also looked into using a Forest Data Structure to implement a Family Genealogy Tree that consist of multiple nodes with 0-multiple children.我还研究了使用森林数据结构来实现由具有 0 个多个子节点的多个节点组成的家族谱系树。 Possibly may use a Forest Tree, however from most representations that I searched up on and found, it looks similar to a disjointed set, but I do not want a parent node to already be pre-destined to have no children.可能会使用森林树,但是从我搜索并发现的大多数表示中,它看起来类似于一个脱节的集合,但我不希望父节点已经注定没有孩子。 I hope it makes sense what I am saying.我希望我所说的有意义。 Any advice or comments anyone is able to offer would be much appreciated.任何人能够提供的任何建议或意见将不胜感激。

If you want to represent a list of children, simply have a list of Person instances inside your Person class:如果你想代表一个孩子的列表,只需在你的 Person 类中有一个 Person 实例的列表:

class Person {
  List<Person> children;
  Person father;
  Person mother;
}

Then you can add as many Persons as you want to the children list.然后,您可以将任意数量的人员添加到子列表中。

I also added father and mother fields here to allow you to navigate to parents.我还在此处添加了父亲和母亲字段,以便您导航到父母。

One tip: Be very thorough with your modelling.一个提示:对你的建模非常彻底。 Familial relations have some easy to miss cases, eg adoption.家庭关系有一些容易漏掉的案例,比如收养。 "Father" and "mother" is almost certainly too simplistic. “父亲”和“母亲”几乎可以肯定是过于简单化了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM