I need help with recursive representation of a family tree. Here is the data:
children_and_parents = {
"Mary": ["Patricia", "Lisa"],
"Patricia": ["Barbara", "Helen", "Maria"],
"Maria": ["Keren", "Carol"],
"Barbara": ["Betty"]
}
I need to mention that the values are objects, so I need to call them children_and_parents["Maria"].child
to get ['Patricia', 'Lisa']
.
The recursive program I currently have:
def draw_family_tree(person, level=0):
if person in children_and_parents:
for i in range (len(children_and_parents[person].child)):
print (" "*level, person)
return draw_family_tree(children_and_parents[person].child[i], level+3)
What it's currently doing is:
Mary
Patricia
Barbara
but the result should be something like:
Mary
Patricia
Barbara
Betty
Helen
Maria
Keren
Carol
Lisa
I'm stuck at the beginning of the program. If someone would be willing to help I would really appreciate it.
Rough code: https://repl.it/repls/BlondCavernousExponents