Skip to main content
`Ctrl`+`K`

# skbio.tree.TreeNode.preorder

TreeNode.preorder(include_self=True)

  Perform preorder traversal over tree.

  Parameters:
    include_self bool, optional

      Include the initial node if True (default).

  Yields:
    TreeNode

      Visited node.

  See also

  `traverse`
  `postorder`
  `pre_and_postorder`
  `levelorder`

  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