All nodes in preorder: the roots in ascending order, and the children of each node in ascending
order; each node once (Java: preorder()).
It is this index's own array, not a copy, and must not be modified (Java: an unmodifiable view).
Time: O(1).
Returns whether node is an ancestor of descendant: whether it lies on the path from the
root of its tree to descendant. A node is its own ancestor.
Time: O(1).
node ID in 0..nodeCount-1
node ID in 0..nodeCount-1
true if node is an ancestor of descendant
Returns the lowest common ancestor of a and b, or -1 if they are in different trees.
The lowest common ancestor is the deepest node that is an ancestor of both. As a node is its
own ancestor, the result is a if a is an ancestor of b.
Time: O(log nodeCount).
node ID in 0..nodeCount-1
node ID in 0..nodeCount-1
lowest common ancestor, or -1
Returns the position of node in PreorderAncestry.preorder.
Data in an array indexed by preorder position has every subtree in a contiguous range. Prefix sums over such an array, for example, give the sum over any subtree in O(1).
Time: O(1).
node ID in 0..nodeCount-1
position in 0..nodeCount-1
Returns the nodes of the subtree of node, in preorder, starting with node.
The result is a view of this index's own array, not a copy, and must not be modified (Java: an unmodifiable view).
Time: O(1).
node ID in 0..nodeCount-1
nodes of the subtree
StaticofReturns a PreorderAncestry for the forest of ancestry.
The result shares the arrays of ancestry instead of copying them. The preorder is computed
anew on every call, not cached: keep the result to use it more than once. If ancestry already
is a PreorderAncestry, returns it.
Time: O(nodeCount), or O(1) if already a PreorderAncestry.
Memory: +12 x nodeCount bytes for the result; while it runs, 8 x nodeCount bytes more; none
if already a PreorderAncestry.
the index to add the preorder to
the index with the preorder
Returns the plain index of a forest or of a dominator tree, without the preorder. This is Ancestry.of, inherited as Java's static methods are.
An Ancestry that also holds the preorder of its forest, for an ancestor test in O(1) and the nodes of every subtree.
PreorderAncestry.of builds it from an index and shares the arrays of that index. In preorder, every subtree is a contiguous range: the subtree of
nodetakes the positionspreorderIndexOf(node)..preorderIndexOf(node) + subtreeSizeOf(node) - 1of PreorderAncestry.preorder. So PreorderAncestry.isAncestorOf is a range test, and PreorderAncestry.subtreeOf returns a view of that range. Here, the basic blocks of the methodloadthatbodydominates, from thedominatorsofload:The preorder visits the roots in ascending order, and the children of each node in ascending order. If the node IDs were assigned in this order, as is usual for a syntax tree numbered in source order, the preorder is
0..nodeCount-1, and the subtree ofnodeisnode..node + subtreeSizeOf(node) - 1.Instances are immutable.
Memory: 12 x nodeCount bytes on top of the Ancestry it extends.