skbio.tree.TreeNode.preorder#

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

Perform preorder traversal over tree.

Parameters:
include_selfbool, optional

Include the initial node if True (default).

Yields:
TreeNode

Visited node.

Notes

Preorder traversal visits each node followed by traversing each of its children in order. It is also known as NLR (node - left - right). It is a depth-first search (DFS). The overall direction of traversal is from root to tips.

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