MRCA() calculates the last common ancestor of specified nodes.

MRCA(x1, x2, ancestors)

Arguments

x1, x2

Integer specifying index of leaves or nodes whose most recent common ancestor should be found.

ancestors

List of ancestors for each node in a tree. Perhaps produced by ListAncestors().

Value

MRCA() returns an integer specifying the node number of the last common ancestor of x1 and x2.

Details

MRCA() requires that node values within a tree increase away from the root, which will be true of trees listed in Preorder. No warnings will be given if trees do not fulfil this requirement.

Author

Martin R. Smith (martin.smith@durham.ac.uk)

Examples

tree <- BalancedTree(7)

# Verify that node numbering increases away from root
plot(tree)
nodelabels()


# ListAncestors expects a tree in Preorder
tree <- Preorder(tree)
edge <- tree$edge
ancestors <- ListAncestors(edge[, 1], edge[, 2])
MRCA(1, 4, ancestors)
#> [1] 9

# If a tree must be in postorder, use:
tree <- Postorder(tree)
edge <- tree$edge
ancestors <- lapply(seq_len(max(edge)), ListAncestors,
                    parent = edge[, 1], child = edge[, 2])