I am trying to do a recursive function in Python to get all parents starting from a given child - eg. if I want to find all parents starting from A - A has parents B and C, B has parents D and E, C has parents F and G, so the function should return set: {B,C,D,E,F,G} I have a class GenTree, where I have all people saved in self.people (instances, not names) and a class Person, which has method get_parents() to either return False or a tuple of parents (2 instances of class Person)
Here is the whole file with the classes and methods:
class Person:
def __init__(self, name, gender, education, father = False, mother = False):
self.name = name
self.gender = gender
self.education = education
self.father = father
self.mother = mother
self.children = []
def add_parent(self, inst):
if inst.gender=="m":
self.father = inst
else:
self.mother = inst
def add_child(self, inst):
self.children.append(inst)
def has_parent(self):
return True if self.father or self.mother else False
def get_parents(self):
if self.has_parent():
if self.father and self.mother: return self.father, self.mother
if self.father and not self.mother: return self.father
if self.mother and not self.father: return self.mother
else:
return ()
class GenTree:
def __init__(self):
self.people = {}
def load_from_file(self, file_name):
data = open(file_name, "r")
people = {}
reading = "person"
for line in data:
line = line.rstrip()
reading = "fam" if line=="" else reading
if reading=="person":
thisInfo = line.split(";")
thisName = thisInfo[0]
thisGender = thisInfo[1]
thisEd = thisInfo[2]
self.people[thisName] = Person(thisName, thisGender, thisEd)
else:
if line == "":
continue
thisInfo = line.split("=>")
for i in range(len(thisInfo)):
thisInfo[i] = thisInfo[i].rstrip()
thisInfo[i] = thisInfo[i].strip(" ")
self.people[thisInfo[1]].add_parent(self.people[thisInfo[0]])
self.people[thisInfo[0]].add_child(self.people[thisInfo[1]])
def get_all_parents(self, child_name):
child = self.people[child_name]
parents = child.get_parents()
if parents:
for parent in parents:
return parents + self.get_all_parents(parent.name)
return parents
g = GenTree()
g.load_from_file("data_a")
print([i.name for i in g.get_all_parents('Katka')])
and here is the file with given data:
Ales;m;c
Alexandr;m;c
Anna;f;h
Dana;f;e
Daniela;f;u
David;m;u
Hana;f;h
Jana;f;u
Jarda;m;c
Jindra;m;u
Jirka;m;u
Jitka;f;h
Juraj;m;u
Karel;m;e
Katka;f;c
Lenka;f;h
Leon;m;h
Leona;f;c
Leos;m;e
Lida;f;e
Ludmila;f;h
Magdalena;f;c
Matej;m;u
Michaela;f;h
Michal;m;e
Patricia;f;h
Petr;m;h
Richard;m;e
Sasa;f;u
Stefan;m;h
Tereza;f;h
Tomas;m;e
Vaclav;m;e
Vojtech;m;c
Zdena;f;e
Zdenek;m;h
Ales => Zdenek
Tereza => Zdenek
Alexandr => Vojtech
Zdena => Vojtech
David => Anna
Sasa => Anna
Jarda => Daniela
Patricia => Daniela
Vojtech => Jindra
Daniela => Jindra
Zdenek => Jirka
Anna => Jirka
Vaclav => Juraj
Michaela => Juraj
Ales => Ludmila
Tereza => Ludmila
Ludmila => Magdalena
Juraj => Magdalena
Juraj => Tomas
Anna => Tomas
Zdenek => Lida
Sasa => Lida
Lida => Leona
Zdenek => Leona
Tomas => Stefan
Anna => Stefan
Tomas => Karel
Leona => Karel
Tomas => Leos
Leona => Leos
Tomas => Lenka
Leona => Lenka
Juraj => Matej
Lenka => Matej
Juraj => Leon
Anna => Leon
Juraj => Richard
Lenka => Richard
Richard => Petr
Ludmila => Petr
Petr => Michal
Dana => Michal
Stefan => Dana
Anna => Dana
Michal => Hana
Lida => Hana
Michal => Jana
Lida => Jana
Petr => Jitka
Dana => Jitka
Jirka => Katka
Dana => Katka
Now, "print([i.name for i in g.get_all_parents('Katka')])" should return:
{'Jirka', 'Dana', 'Zdenek', 'Anna', 'Stefan', 'Ales', 'Tereza', 'David', 'Sasa', 'Tomas', 'Juraj', 'Vaclav', 'Michaela'}
but it returns
['Jirka', 'Dana', 'Zdenek', 'Anna', 'Ales', 'Tereza']
But it always does "self.get_all_parents(parent.name)" ONLY for the first one in the for loop Also, I don't know how to return it as a set (I tried to do get_all_parents(self, child_name, allparents=set()), but it doesn't seem to reset the allparents set with every call of function)