I created some nodes by a Node-class and added them to a tree using Treelib.
class Node(object):
def __init__(self, id, label, parent, type):
self.id = id
self.label = label
self.parent = parent
self.type = type
node = Node(id, label, parent, type)
if id == 'root':
tree.create_node(tag=id, identifier=id, data=node)
else:
tree.create_node(tag=id, identifier=id, parent=parent, data=node)
By calling tree.show() I got a great overview of the tree. Now I want to iterate through the tree and get the data of each node defined earlier. (not only a single property by tree.show(data_property=""))
Do you got any ideas of how to work with the defined data?
My final goal is to calculate the tree structure like a decision tree and I dont find a good way using Treelib so far.
First some remarks:
Node
; as also TreeLib defines aNode
class.You can iterate over the nodes with the
all_nodes_itr
method, which will give you a TreeLibNode
instance in each iteration. You can then access TreeLib attributes such asidentifier
orparent
. For your own attributes, access thedata
attribute, and then the attribute you want to see (likelabel
)Here is a simplified script: