def drop_node_from_tree(self, node: Node): while self.is_node_on_tree(node): for child in node.get_children(): if self.is_node_on_tree(child): self.drop_node_from_tree(child) else: self._node_on_tree[node.count_node_level()].pop( node.get_name_full(), None) pass
def is_node_on_tree(self, node: Node) -> bool: is_on_tree = self._root.__eq__(node.get_root()) is_on_tree = is_on_tree and (self._root.__eq__(node.get_root())) is_on_tree = is_on_tree and (node.count_node_level() in self._node_on_tree) is_on_tree = is_on_tree and ( node.get_name_full() in self._node_on_tree[node.count_node_level()].keys()) return is_on_tree
def test_get_name_full(self): name_root = 'root' name_child = 'child' root = Node(name_root) child = root.create_child(name_child) self.assertEqual(name_root, root.get_name()) self.assertEqual(name_root, root.get_name_full()) self.assertEqual(name_child, child.get_name()) self.assertEqual(name_root + Node.JOINER_NAME + name_child, child.get_name_full()) pass
def add_node_to_tree(self, node: Node): if not self._root.__eq__(node.get_root()): raise ValueError('cannot add node from other tree') node_level = node.count_node_level() while not self.is_node_on_tree(node): parent_node = node.get_parent() if self.is_node_on_tree(parent_node): # add target node to the tree if node_level not in self._node_on_tree: self._node_on_tree[node_level] = {} self._node_on_tree[node_level][node.get_name_full()] = node # update target parent node on tree parent_node_level = parent_node.count_node_level() self._node_on_tree[parent_node_level][ parent_node.get_name_full()].add_child(node) else: self.add_node_to_tree(node.get_parent()) pass