skbio.tree.TreeNode.pre_and_postorder#

TreeNode.pre_and_postorder(include_self=True)[source]#

Perform traversal over tree, visiting nodes before and after.

Parameters:
include_selfbool, optional

Include the initial node if True (default).

Yields:
TreeNode

Visited node.

Notes

Pre- and post-order traversal visits each node before and after traversing all children of the node. Therefore, each internal node (and root) is visited twice. It is a depth-first search (DFS). The overall direction of traversal is from root to tips then back to root.

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.pre_and_postorder():
...     print(node.name)
g
c
a
b
c
f
d
e
f
g