Skip to main content
`Ctrl`+`K`

# skbio.tree.TreeNode.traverse

TreeNode.traverse(self_before=True, self_after=False, include_self=True)

  Traverse over tree.

  Parameters:
    self_before bool, optional

      Whether to include each node before its descendants (default: True).

    self_after bool, optional

      Whether to include each node after its descendants (default: False).

    include_self bool, optional

      Include the initial node if True (default).

  Yields:
    TreeNode

      Visited node.

  See also

  `preorder`
  `postorder`
  `pre_and_postorder`
  `levelorder`
  `tips`
  `non_tips`

  Notes

  This is a depth-first search (DFS). `self_before` and `self_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` and `tips`. 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