From 1c6e96e7ddaca0fd0f8295ac5531c161d14501cb Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 06:58:55 +0200 Subject: Merging upstream version 1.7.0. Signed-off-by: Daniel Baumann --- treelib/tree.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'treelib/tree.py') diff --git a/treelib/tree.py b/treelib/tree.py index 7d92297..1cc9ac8 100644 --- a/treelib/tree.py +++ b/treelib/tree.py @@ -929,7 +929,7 @@ class Tree(object): print("Tree is empty") if stdout: - print(self._reader) + print(self._reader.encode("utf-8")) else: return self._reader @@ -1128,3 +1128,42 @@ class Tree(object): print(f.getvalue()) f.close() + + @classmethod + def from_map(cls, child_parent_dict, id_func=None, data_func=None): + """ + takes a dict with child:parent, and form a tree + """ + tree = Tree() + if tree is None or tree.size() > 0: + raise ValueError("need to pass in an empty tree") + id_func = id_func if id_func else lambda x: x + data_func = data_func if data_func else lambda x: None + parent_child_dict = {} + root_node = None + for k, v in child_parent_dict.items(): + if v is None and root_node is None: + root_node = k + elif v is None and root_node is not None: + raise ValueError("invalid input, more than 1 child has no parent") + else: + if v in parent_child_dict: + parent_child_dict[v].append(k) + else: + parent_child_dict[v] = [k] + if root_node is None: + raise ValueError("cannot find root") + + tree.create_node(root_node, id_func(root_node), data=data_func(root_node)) + queue = [root_node] + while len(queue) > 0: + parent_node = queue.pop() + for child in parent_child_dict.get(parent_node, []): + tree.create_node( + child, + id_func(child), + parent=id_func(parent_node), + data=data_func(child), + ) + queue.append(child) + return tree -- cgit v1.2.3