treelib
treelib copied to clipboard
reversed paths_to_leaves to build a tree from a list
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?
@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
.
https://blog.csdn.net/u012111465/article/details/82908601 https://blog.csdn.net/u012111465/article/details/82908552
@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.