scikit-bio is back in active development! Check out our announcement of revitalization.

skbio.tree.TreeNode.traverse#

TreeNode.traverse(self_before=True, self_after=False, include_self=True)[source]#

Return iterator over descendants.

This is a depth-first traversal. Since the trees are not binary, preorder and postorder traversals are possible, but inorder traversals would depend on the data in the tree and are not handled here.

Parameters:
self_beforebool

includes each node before its descendants if True

self_afterbool

includes each node after its descendants if True

include_selfbool

include the initial node if True

`self_before` and `self_after` are independent. If neither is `True`,
only terminal nodes will be returned.
Note that if self is terminal, it will only be included once even if
`self_before` and `self_after` are both `True`.
Yields:
TreeNode

Traversed node.

Examples

>>> from skbio import TreeNode
>>> tree = TreeNode.read(["((a,b)c);"])
>>> for node in tree.traverse():
...     print(node.name)
None
c
a
b