caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
800 stars 185 forks source link

Finding the nodes between two nodes #204

Closed eyildiz-ugoe closed 1 year ago

eyildiz-ugoe commented 1 year ago

I have a tree over which I'd like to print the path between two nodes. I could not find a function that does the magic, but I believe the closest is expand_tree(). However, upon calling it and trying to print the nodes, I get an error:

path = tree.expand_tree(tree.get_node(4), filter=tree.get_node(83), sorting=False)
for node in path:
    print(node)
Traceback (most recent call last):
  File "E:\question.py", line 55, in <module>
    for node in path:
  File "E:\Anaconda3\envs\3dreconst\lib\site-packages\treelib\tree.py", line 427, in expand_tree
    raise NodeIDAbsentError("Node '%s' is not in the tree" % nid)
treelib.exceptions.NodeIDAbsentError: Node 'Node(tag=4, identifier=4, data=None)' is not in the tree

What am I doing wrong?

eyildiz-ugoe commented 1 year ago

Found it.

path_to_node = tree.expand_tree(4, filter=lambda x: x.tag != 83)