У меня есть дерево, сгенерированное на python, и я просто хочу добавить нового дочернего элемента к родительскому элементу. Хотя дочерние элементы дочернего элемента no не пусты, кажется, что дочерний узел становится дочерним по отношению к родительскому элементу и самому себе.
def __init__(self):
self.child_nodes = []
self.data = None
...
self.parent = None
def init_tree(self, data, child_nodes=[], ...):
self.child_nodes = child_nodes
self.data = data
...
def add_child_node(self, node):
ancestors = self.get_ancestors()
if node is not self and node not in ancestors:
self.child_nodes.append(node)
node.parent = self
def get_ancestors(self):
ancestor = self.parent
ancestors = [ancestor]
try:
while ancestor.parent != None:
ancestors.append(ancestor)
parent = self.parent
ancestor = parent.get_parent()
return ancestors
# Node is root. Root's parent is None
except AttributeError:
return []
def my_funct(self):
...
child_node = RandomWalkTree()
child_node.init_tree(data=data)
self.add_child_node(child_node)
my_funct приводит к дочернему узлу с дочерним узлом, содержащим себя рекурсивно.Какие моменты я пропускаю?