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. 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. 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.
Asked
Active
Viewed 8,540 times
2
-
Depends on what relational lookups you want to do: do you want children for a given parent, parents for a given child, both, neither, or other? – Andy Turner Sep 14 '15 at 06:30
-
Also: can you provide a link to the other question? – Andy Turner Sep 14 '15 at 06:31
-
Thanks for your input! I wanted to produce a tree of some sort data-structure entity that would contain information about a node and possess more than 2 children nodes, unlike the BInary Tree structure. I would want to be able to access a child node via its parent and vice-versa through which a tree gui would be represented showing birth/marriage/death records of each individual person. A person obviously being represented by a single node who may have 0->2 siblings. Perhaps further implementing siblings from one parent. I would like to use a data structure other than an ArrayList. – Tj895 Sep 14 '15 at 06:35
-
Here is the link about forest data structures: http://programmers.stackexchange.com/questions/285418/name-of-data-structure-thats-tree-like-with-multiple-root-nodes – Tj895 Sep 14 '15 at 06:35
-
Please add the question link to your question. – Andy Turner Sep 14 '15 at 06:36
-
Would you not just use a many to many data-structure? You can just implement your own. Is there a specific feature of a node structure that is required or preferred? The design is very basic. – Reilas May 29 '23 at 05:13
1 Answers
0
If you want to represent a list of children, simply have a list of Person instances inside your Person class:
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, e.g. adoption. "Father" and "mother" is almost certainly too simplistic.

Andy Turner
- 137,514
- 11
- 162
- 243
-
Thank you for your suggestion, but what if I would like to use a tree structure rather than an ArrayList? That is exactly my point with a project of this kind, there may be a child from one parent who is siblings with another child from another parent i.e Mary and Sam come from one father being the same and different mothers. I am not so concerned about adoption, although its true speaking of which I didn't think about. – Tj895 Sep 14 '15 at 06:58
-
@Tj895, I believe what you would do here is remove the list from the design, and use just the two enclosing nodes. And when compiling the list, you simply point each node to the parent nodes. – Reilas May 29 '23 at 05:22