caesar0301/treelib

What is the most efficient way to get all nodes in a certain depth

tonyliangli opened this issue · 4 comments

Hi,

Thanks for the great lib. I would like to know how to get all nodes in a certain depth. I tried expand_tree with mode width, but i can only get nodes. I have to use level() to check the level. Please tell me if there's a better way. Thanks!

Hi @tonyliangli , can you precise what is the expected output?
Do you want list of nodes at a given depth or do you want a tree truncated at a given depth?

Sorry for late reply, you can use filter_nodes method:

depth_2_nodes = list(my_tree.filter_nodes(lambda x: my_tree.depth(x) == 2))