js-symbol-tree
js-symbol-tree copied to clipboard
Level (or depth) of a node
When I use the Iterator, I would want to know the level of a node. Is there a (low complexity) way to have the level of a node?
Level: "The level of a node is defined as: 1 + the number of edges between the node and the root." Depth: "The depth of a node is the number of edges from the tree's root node to the node." (from Wikipedia)
Would be also interesting to have other characteristics about the tree:
- is Branch node (or is Internal node)
- Height of node
- Height of tree
- Depth
There already is a method to figure out if something is an internal vs external node: tree.hasChildren(obj)
.
Figuring out the depth/level could be sped up by using a cache. On a static tree it would then average out to O(1)
. This is similar to how tree.index(obj)
works (the number of preceding siblings). I think figuring out the height could be done in a similar way. But i'll have to see when i have some time to add this