caesar0301/treelib

When i create a Tree , its not maintaining the order which in i created the children when i invoke show method

salman0149 opened this issue · 8 comments

from treelib import Node, Tree
tree = Tree()
tree.create_node('Students', 'Students', parent=None)
Node(tag=Students, identifier=Students, data=None)
tree.create_node('Ben', 'Ben',parent='Students')
Node(tag=Ben, identifier=Ben, data=None)
tree.create_node('Annie', 'Annie',parent='Students')
Node(tag=Annie, identifier=Annie, data=None)
tree.show()
Students
├── Annie
└── Ben

As you see eventhough i created Ben Node first as child it is displaying Annie first, is there an argument in show() or in create_method() which i can pass to maintain the order when i call show method

Now you can use: expand_tree(sorting=True)

@unikevin: Shouldn’t this be expand_tree(sorting=False)? It’s a bit confusing, but from what I gathered, sorting=True means that sorting should happen based on some sort key, while sorting=False means it does not sort the nodes but maintains insertion order.

Also, I guess it would be nice if the same parameter was available on the show() method, because I also cannot find a way to display the tree while maintaining insertion order.

@salman0149 I think it should be possible to pass a custom counter as a data property and then sort by that key in show().

Hi! I've submitted a PR #180. If it's merged, you can use show(sorting=False) to display the tree while maintaining insertion order.

Thanks very much , sorry for the late reply let me try this out, but still the PR is not merged

On a seperate node when i pass tree.show(key=False) , it preserves the order of insertion.

Hey guys! Without having a look into existing pull requests, I also implemented this functionality in #188 and #189. Unlike @HollowMan6, I removed legacy ordering; Is there any reason to keep it? In my opinion, legacy ordering is rather a bug than a feature, causing undocumented side effects: It basically renders the key parameter ineffective.

guyla commented

if key is none: key is defined as "return node", which misses the point.

The way to go for me was

tree.show(key = lambda x : True)

key=False didn't work :(