caesar0301 / treelib

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

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

Closed salman0149 closed 1 year ago

salman0149 commented 3 years ago

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

unikevin commented 3 years ago

Now you can use: expand_tree(sorting=True)

frederik-elwert commented 3 years ago

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

frederik-elwert commented 3 years ago

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

HollowMan6 commented 3 years ago

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.

salman0149 commented 3 years ago

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

salman0149 commented 2 years ago

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

christian-krieg commented 2 years ago

Hey guys! Without having a look into existing pull requests, I also implemented this functionality in https://github.com/caesar0301/treelib/pull/188 and https://github.com/caesar0301/treelib/pull/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 1 year ago

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 :(