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

not change the node or branch order when create a tree #74

Closed jinwuxia closed 7 years ago

jinwuxia commented 7 years ago

hi,

Your program works very well. But I have another related problem to ask for you.

I create a tree. The script is the following: tree = Tree() tree.create_node("HH", "hh") tree.create_node("JJ", "jj", parent='hh') tree.create_node("AA", "aa", parent='hh') tree.show() The tree will show like this: HH --- AA --- JJ

But, Node JJ should be created before AA, as the code expects. I expect that the tree branching order is consistent with the order that the node is created.
The expected result is: HH --- JJ --- AA

Then, how could I achieve that? Do your program provide this function?

Thanks a lot for your help.

c0fec0de commented 7 years ago

The Tree stores the nodes in a dictionary, which does not preserve the order. On tree output the child nodes are sorted by their id. http://anytree.readthedocs.io/ might implement what you are looking for.

jinwuxia commented 7 years ago

Still based on your treelib, I use an increased tag to represent the node id, and it works. Thanks for your advice.