treelib icon indicating copy to clipboard operation
treelib copied to clipboard

reversed paths_to_leaves to build a tree from a list

Open stevenlis opened this issue 5 years ago • 3 comments

The function of paths_to_leaves returns the following list

[['harry', 'jane', 'diane', 'mary'],
 ['harry', 'jane', 'mark'],
 ['harry', 'jane', 'diane', 'george', 'jill'],
 ['harry', 'bill']]

But what if I have such a list and want to construct a tree. If there a reversed paths_to_leaves function?

If not, is there a workaround?

stevenlis avatar Jun 17 '19 15:06 stevenlis

@StevenLi-DS I thinks it is not hard to do that if you are not concerned about the children order of each non-leaf nodes. You can implement this algorithms easily by traversing the returned list from paths_to_leaves.

caesar0301 avatar Aug 14 '19 07:08 caesar0301

https://blog.csdn.net/u012111465/article/details/82908601 https://blog.csdn.net/u012111465/article/details/82908552

sftrwo avatar Sep 03 '19 04:09 sftrwo

@StevenLi-DS can simply do... something like:

paths =tree.paths_to_leaves()

# [['a','b', 'c'], ['a', 'b', 'd'], ['a', 'b', 'e']]

[l.reverse() for l in paths]

# paths now equals [['c', 'b', 'a'], ['d', 'b', 'a'], ['e', 'b', 'e']]

This will reverse the contents of each of the lists returned.

mattseymour avatar Oct 09 '19 13:10 mattseymour