1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
from treelib import Tree, Node tree = Tree() tree.show() print(tree.identifier)
tree.create_node(tag='Node-5', identifier='node-5', data=5) tree.create_node(tag='Node-10', identifier='node-10', parent='node-5', data=10) tree.create_node('Node-15', 'node-15', 'node-10', 15) tree.show()
node = Node(tag='Node-A', identifier='node-A', data='A') tree.add_node(node, parent='node-5') tree.show() """ Node-5 └── Node-10 └── Node-15 Node-5 ├── Node-10 │ └── Node-15 ├── Node-A """
print(node) print('node id: ', node.identifier) print('node tag:', node.tag) print('node data:', node.data) print('node is leaf: ', node.is_leaf()) print('node is root: ', node.is_root())
print('tree len: ', len(tree)) print('tree size:', tree.size())
print('tree depth:', tree.depth()) print('node-20 depth:', tree.depth(node='node-20')) print('node-20 level:', tree.level('node-20')) print('tree leaves:', tree.leaves()) print(tree.paths_to_leaves())
print('tree nodes:', tree.nodes) print(tree.all_nodes()) for node in tree.all_nodes_itr(): print(node)
print(tree.to_dict()) print(tree.to_json()) tree.to_graphviz() tree.save2file('demo_tree.tree')
|