Skip to main content
`Ctrl`+`K`

# skbio.tree.TreeNode.ancestors

TreeNode.ancestors(include_self=False)

  Return all ancestral nodes from self back to the root.

  Parameters:
    include_self bool, optional

      Whether to include the initial node in the path (default: False).

  Returns:
    list of TreeNode

      The path from self toward the 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

      >>> tip = tree.find('a')
      >>> [node.name for node in tip.ancestors()]
      ['c', 'g']
      >>> [node.name for node in tip.ancestors(include_self=True)]
      ['a', 'c', 'g']