caesar0301 / treelib

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

reversed paths_to_leaves to build a tree from a list #116

Open stevenlis opened 5 years ago

stevenlis commented 5 years ago

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?

caesar0301 commented 5 years ago

@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.

sftrwo commented 5 years ago

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

mattseymour commented 4 years ago

@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.