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

[Survey] Enable node-wide ranking in tree.show() #163

Open caesar0301 opened 3 years ago

caesar0301 commented 3 years ago

This is a proposed feature by Fahri Basegmez. I want to know if any others require this feature. I have a better implementation of a node ordering based on data property which could be customized by uses a lot.

Quoting the email:

I am using your treelib library and like it a lot. When I print a tree using tree.show() >sometimes I would like to control the way nodes are ordered. To be able to do that I modified >the source in a few places as shown below.

In node.py

Line 46 def init(self, tag=None, identifier=None, expanded=True, data=None, rank=0):

added the following line to Node. init () self.rank = rank

In tree.py

Line 214 if key is None: def key(node): return node.rank

Line 366 def create_node(self, tag=None, identifier=None, parent=None, data=None, rank=0):

Line 371 (in create_node() node = self.node_class(tag=tag, identifier=identifier, data=data, rank=rank)

If a user never defines a rank then tree.show() behaves like it currently does. By adding ranks >to the desired nodes, print order of the nodes can be manipulated.

If you think this could be a useful modification please don't hesitate to use these changes.

Best regards,

Fahri Basegmez

P.S. print_rank or display_rank may be a better name than rank? Feel free to use whatever you >like.

In addition to these, a "use_rank = T/F" argument can be added to tree.show method.

salman0149 commented 3 years ago

I feel this would be a good feature addition ,since i was also looking for a similar use case in which the nodes should be shown in the creation order rather than alphabeticaly.

antipodite commented 2 years ago

Yes, this would be a useful feature for my use case