Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
salman0149 opened this issue Oct 12, 2020 · 8 comments · Fixed by #180

Comments

@salman0149
Copy link

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
Copy link

unikevin commented May 2, 2021

Now you can use: expand_tree(sorting=True)

@frederik-elwert
Copy link

@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
Copy link

@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
Copy link
Contributor

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
Copy link
Author

salman0149 commented Jun 30, 2021

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

@salman0149
Copy link
Author

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

@christian-krieg
Copy link

christian-krieg commented Nov 23, 2021

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
Copy link

guyla commented Jul 26, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants