joowani / binarytree

Python Library for Studying Binary Trees
http://binarytree.readthedocs.io
MIT License
1.81k stars 171 forks source link

Question about node index #35

Closed Roland-djee closed 2 years ago

Roland-djee commented 2 years ago

Hello,

A quick question if that is the place to ask, is it possible to access the index of an individual node ?

I can't seem to find an easy way to do that.

Thanks !

joowani commented 2 years ago

Hi @Roland-djee,

The short answer is no. Index calculation is always done on-the-fly from the root node (the node you are indexing from). Nodes cannot have "absolute" indexes since the they are relative to the root node you pick. But I suppose we can add a method like binarytree.Node.get_index_from_root(other) that would either return -1 or raise an error if other is not an ancestor, else calculate and return the index.

joowani commented 2 years ago

I added get_index function in release 6.4.0 that should almost cover your use case.

Roland-djee commented 2 years ago

Thanks !