Number of nodes (or equivalent) under a node #2525
-
As well as having a postorder array It is often useful in phylogenetic algorithms to also have the total number of nodes under each element of the postorder array (or an equivalent number like the index into the postorder array to skip to in order to bypass the subtree). For example, this can be used to create a "nested set" representation of a tree, or could be used to iterate over the postorder array but skip certain subtrees. Is there a way of getting this info somehow, without traversing through each subtree in turn? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You can make the array in a single pass through the postorder array, right? total_nodes = np.zeros_like(tree.postorder)
for u in tree.postorder:
for v in tree.children(u):
total_nodes[u] += total_nodes[v]
total_nodes[u] += 1 Or such like? |
Beta Was this translation helpful? Give feedback.
-
If this is a commonly used quantity we could consider adding it to the tree arrays - |
Beta Was this translation helpful? Give feedback.
-
It's not worth it I think, certainly not worth storing for every tree. There's a whole bunch of related quantities you might want to sum post-order wise, so this would be better off as tutorial fodder. |
Beta Was this translation helpful? Give feedback.
You can make the array in a single pass through the postorder array, right?
Or such like?