View source: R/tree_ancestors.R
| MRCA | R Documentation |
MRCA() calculates the last common ancestor of specified nodes.
MRCA(x1, x2, ancestors)
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 |
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.
MRCA() returns an integer specifying the node number of the last
common ancestor of x1 and x2.
Martin R. Smith (martin.smith@durham.ac.uk)
Other tree navigation:
AncestorEdge(),
CladeSizes(),
DescendantEdges(),
EdgeAncestry(),
EdgeDistances(),
ListAncestors(),
MatchEdges(),
NDescendants(),
NodeDepth(),
NodeNumbers(),
NodeOrder(),
RootNode()
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)
# 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])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.