meder omuraliev
meder omuraliev

Reputation: 186662

Data structure for a family tree with multiple partners and siblings?

I have a very basic family tree structure but I need to figure out how to make it support multiple partners and siblings without as much redundancy.

The base of the entire tree is the person that's creating the tree.

Consider this very simple structure:

{
    "name": "Me",
    "dob": "1988",
    "parents": [
        {
            "name": "Gina Carano",
            "dob": "1967"
        },
        {
            "name": "Genghis Khan",
            "dob": "1961"
        }
    ],
    "children": [
        {
            "name": "Tim",
            "dob": "1992"
        }
    ]
}

This works nicely but what if I discovered I had a half sibling named Judy (Genghis Khan loved the ladies) and a full sibling named Brian and expanded it to this?

{
    "name": "Me",
    "dob": "1988",
    "parents": [
        {
            "name": "Gina Carano",
            "dob": "1967"
        },
        {
            "name": "Genghis Khan",
            "dob": "1961"
        }
    ],
    "children": [
        {
            "name": "Tim",
            "dob": "1992"
        }
    ],
    "siblings": [
        {
            "name": "Judy",
            "dob": "1987",
            "parents": [
                {
                    "name": "Courtney Carano",
                    "dob": "1965"
                },
                {
                    "name": "Genghis Khan",
                    "dob": "1961"
                }
            ]
        },
        {
            "name": "Brian",
            "dob": "1988",
            "parents": [
                {
                    "name": "Gina Carano",
                    "dob": "1967"
                },
                {
                    "name": "Genghis Khan",
                    "dob": "1961"
                }
            ]
        }
    ]
}

This does map my 2 newfound siblings but now I have a bit of redundancy in my data, as Genghis Khan is in 3 different places. I could potentially create a one level list such as this:

[
    { "id": "1", "name": "Me", "dob": "1988", "parents": [2,3], "siblings": [4,5] },
    { "id": "2", "name": "Genghis Khan", "dob": "1961", "children": [1,4,5] },
    { "id": "3", "name": "Gina Carano", "dob": "1967", "children": [1] },
    { "id": "4", "name": "Tim", "dob": "1992", "parents" : [2,3] },
    { "id": "5", "name": "Judy", "dob": "1987", "parents": [2,6] },
    { "id": "6", "name": "Courtney Carano", "dob": "1965", "children": [5] }
]

Would this work out the same way without as much redundancy? And are there any foreseeable circumstances in which there would be any limitations in terms of mapping out multiple partners with children?

Note: I figure if I keep the initial structure, I'd have to add id keys to properly identify that Genghis Khan is the same in all 3 instances.

My end goal is mapping a pedigree tree (probably in d3.js) that is visually going to be in this manner, with a line in the middle between partners going to their children.

So with the dataset above, I'm trying to render:

enter image description here

Upvotes: 4

Views: 3019

Answers (1)

Tom Morris
Tom Morris

Reputation: 10540

Almost all genealogy systems have IDs for people, so I wouldn't worry about adding/requiring that.

The traditional way of doing this is to have a Family node type as well as a Person node type. This allows multiple marriages and also gives you a place to connect information like marriage date, marriage place, etc.

person[ { "id": "p1", "name": "Me", "dob": "1988", "parents": "f3" }, { "id": "p2", "name": "Genghis Khan", "dob": "1961", "parents": "f1", "spouse_families": ["f2", "f3"] }, { "id": "p3", "name": "Gina Carano", "dob": "1967", "spouse_families" : ["f3"] }, { "id": "p4", "name": "Brian", "dob": "1992", "parents" : "f3" }, { "id": "p5", "name": "Judy", "dob": "1987", "parents": "f2" }, { "id": "p6", "name": "Courtney Carano", "dob": "1965", "spouse_families": ["f2"] }, {"id": "p7", "name": "Mother of Ghengis"}, {"id": "p8", "name": "Father of Ghengis"}, ]

family[ {"id":"f1","marriage date":"", "parents": ["p7", "p8"],"children":["p2"]}, {"id":"f2","marriage date":"", "parents": ["p6", "p2"],"children":["p5"]}, {"id":"f3","marriage date":"", "parents": ["p3","p2"],"children":["p1", "p4"]}, ]

This gives you a place to connect all the parents and children together without redundancy and lots of special casing. (Note: I corrected "Tim" to "Brian" in the data structure to match the graphic.)

Upvotes: 2

Related Questions