Skip to content

Tree.ancestors(u) method #2706

@hyanwong

Description

@hyanwong

As I've been implementing tree traversal methods, I've commonly wanted to go up the chain of parents for a node. I think there's a good argument for a tree.parents(u) (or tree.ancestors?) method which returns an iterator which then stops when the root is reached. This is a pretty fundamental tree operation and handy to avoid messing with all that tskit.NULL stuff for newbies:

def parents(self, u):
    while self.parent(u) != tskit.NULL:
        u = self.parent(u)
        yield u

Thoughts? Worth the extra API burden?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Python APIIssue is about the Python APIenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions