lxtree

Package to draw tree structures (tree linux command style)
Basic usage
Build the tree structure using TreeNode
s. A TreeNode
can contain other TreeNode
s
to represent the tree. When builded you car get a string representation of it,
or get string of a sub-tree using some child as root.
Code samples
from lxtree import TreeNode
root = TreeNode('root')
root.children = [TreeNode('branch1'), TreeNode('branch3')]
root.insert_child(TreeNode('branch2'), 1)
root[0].append_child(TreeNode('branch11'))
root[0][0] = [TreeNode('leaf111'), TreeNode('leaf112')]
root[1].set_children(TreeNode('leaf21'), TreeNode('leaf22'), TreeNode('leaf23'))
print(root)
Creating all structure at once
root = TreeNode('root').set_children(
TreeNode('branch1').append_child(
TreeNode('branch11').set_children(
TreeNode('leaf111'),
TreeNode('leaf112')
)
),
TreeNode('branch2').set_children(
TreeNode('leaf21'),
TreeNode('leaf22'),
TreeNode('leaf23')
),
TreeNode('branch3')
)
print(root)
Creating the tree from a dict
tree_data = {
'root': {
'branch1': {
'branch11': {
'leaf111': None,
'leaf112': None
}
},
'branch2': {
'leaf21': None,
'leaf22': None,
'leaf23': None
},
'branch3': None
}
}
print(TreeNode.tree_from_dict(tree_data))
Output
