skbio.tree.TreeNode.traverse#
- TreeNode.traverse(self_before=True, self_after=False, include_self=True)[source]#
Traverse over tree.
- Parameters:
- self_beforebool, optional
Whether to include each node before its descendants (default: True).
- self_afterbool, optional
Whether to include each node after its descendants (default: False).
- include_selfbool, optional
Include the initial node if True (default).
- Yields:
- TreeNode
Visited node.
See also
Notes
This is a depth-first search (DFS).
self_before
andself_after
determine whether a node should be visited before and after traversing its children. They are independent. If both True, each internal node (and root) will be visited twice. If neither is True, only tips will be returned.This method is a generalization of
preorder()
,postorder()
,pre_and_postorder()
andtips()
. The default mode (self_before=True, self_after=False
) is equivalent to preorder traversal.Examples
>>> from skbio import TreeNode >>> tree = TreeNode.read(["((a,b)c,(d,e)f)g;"]) >>> print(tree.ascii_art()) /-a /c-------| | \-b -g-------| | /-d \f-------| \-e
>>> for node in tree.traverse(): ... print(node.name) g c a b f d e