def add_edge(self, edge): gstats = [] before = GrowthStatistic(self, edge.parent, edge.child) if edge.parent in self.nodelist and edge.child in self.nodelist: gstats = self.__child_and_parent_exist(edge) elif edge.parent in self.nodelist: self.__only_parent_exists(edge) elif edge.child in self.nodelist: gstats = self.__only_child_exists(edge) else: self.__create_new_edge(edge) after = GrowthStatistic(self, edge.parent, edge.child) return [{'before': before.to_dict(), 'after': after.to_dict(), 'is_attachment_point': True if gstats else False}] + gstats
def __subtree_migration(self, from_, to_, from_tree=None, to_tree=None): """ :param int from: The name of the node serving as the root of the subtree being migrated. :param int to: The name of the parent node the subtree is being attached to. """ subtree_root = self.nodelist[int(from_)] new_parent = self.nodelist[int(to_)] growth_statistics = [] subtree_root.set_parent(new_parent) new_parent.set_child(subtree_root) to_update = deque([subtree_root]) updates = [] while to_update: node = to_update.popleft() updates.append((node.parent, node)) for child in node.children: to_update.append(child) befores = [] for parent, child in updates: for grandchild in child.children: before = GrowthStatistic(self, grandchild.parent.name, grandchild.name) befores.append(before) for parent, child in updates: child.set_tree(to_tree) child.set_parent(parent) for grandchild in child.children: grandchild.set_parent(child) afters = [] for parent, child in updates: for grandchild in child.children: after = GrowthStatistic(self, grandchild.parent.name, grandchild.name) afters.append(after) for before, after in zip(befores, afters): growth_statistics.append({'is_attachment_point': False, 'before': before.to_dict(), 'after': after.to_dict()}) if from_tree.total_nodes <= 0: self.delete_tree(from_tree) return growth_statistics