I have stumbled upon a problem whilst trying to walk my binary tree constructed using AnyNode from anytree.
First, I need to say that each node has a specific id associated with it, so that the root node has an id=0, its children id=1 and id=2. Then the children of id=1 are id=3 and id=4 and so on. I need to be able to walk the tree from top to bottom, check whether a condition is fulfilled on each node, and if it is, save the node and remove all its children from the walking process.
The way I do this is by making a list of the ids using
list1=[node.id for node in LevelOrderIter(root)]
listfar=[]
and then
i=0
while i<len(list1):
....
if condition is fulfilled for a node named e.g parent:
list2=[node.id for node in LevelOrderIter(parent)]
listfar.append(list2)
list1=list(set(list1).difference(list2))
list1.sort()
i=i-1
i=i+1
The reason I have used node.id instead of just node is so that I can sort after the removal, since it doesn't keep my order. The main problem here is that LevelOrderIter takes a lot of time to run for my tree (since the tree has 21 levels). Also, I have not used a walk module since I need to do this for all the nodes in each level, not just from a node to a leaf.
For my case, it takes 1 minute to walk the entire tree and I need it to drop down to the order of a second at most.
Is there a module in anytree that produces the same list of node.ids with LevelOrderIter of the descendants of a node fast? If there isn't one, but there is a module that does that for the entire nodes (not just node.id) is there a way I can keep the order of the elements of list1 when removing the nodes?