caesar0301 / treelib

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

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

Closed tonyliangli closed 4 years ago

tonyliangli commented 4 years ago

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!

leonardbinet commented 4 years ago

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?

tonyliangli commented 4 years ago

Thanks for the reply, I would like a list of nodes at a given depth. Is expand_tree with filter the only way?

Tony Li

tonyliangli@163.com | On 03/02/2020 14:36, Léonard Binet wrote:

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?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

leonardbinet commented 4 years ago

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))
tonyliangli commented 4 years ago

Got it. Thanks a lot.

Tony Li

tonyliangli@163.com | On 03/09/2020 10:37, Léonard Binet wrote:

Sorry for late reply, you can use filter_nodes method:

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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.