6.5 List a node and all its descendants
You get the subtree of a node, its size, and the preorder of the whole forest.
Problem
Section titled “Problem”You want a node of a hierarchy and everything below it, as a list. The node with all its descendants is called its subtree. Often you want an order in which every node comes before its descendants, such as the order of the source code or of a listing. You need this to list:
- all statements inside a block of code;
- all files in a folder and its subfolders;
- all employees of a department;
- all descendants of a person.
Example
Section titled “Example”The example graph family models a family tree that records one
parent per person. It has an edge from each parent to each
child. Who are Ben and his descendants? In which order does the preorder list the whole family? At
which position does it list Cleo?
- Person asked about
- Subtree of Ben
In the picture after, the subtree of Ben is green: Ben and his descendants. Cleo is the other
person the example asks about, for her position in the preorder.
Solution
Section titled “Solution”PreorderAncestry index = PreorderAncestry.of(Ancestry.of(family));// [1, 3, 6, 7, 4] - Ben, Dora, Greta, Hugo, EmilIntList subtree = index.subtreeOf(ben);int size = index.subtreeSizeOf(ben); // 5 - Ben included
// The whole family in preorder, and where Cleo stands in it.IntList order = index.preorder();// order: [0, 1, 3, 6, 7, 4, 2, 5, 8, 9] - Ada, then the subtree of Ben, then that of// Cleo
int positionOfCleo = index.preorderIndexOf(cleo); // 6 - after Ada and Ben's subtreelet index = PreorderAncestry::of(&Ancestry::of(&family)?);let subtree = index.subtree_of(ben); // [1, 3, 6, 7, 4] - Ben, Dora, Greta, Hugo, Emillet size = index.subtree_size_of(ben); // 5 - Ben included
// The whole family in preorder, and where Cleo stands in it.let order = index.preorder();// order: [0, 1, 3, 6, 7, 4, 2, 5, 8, 9] - Ada, then the subtree of Ben, then that of// Cleo
// 6 - after Ada and Ben's subtreelet position_of_cleo = index.preorder_index_of(cleo);const index = PreorderAncestry.of(Ancestry.of(family));// [1, 3, 6, 7, 4] - Ben, Dora, Greta, Hugo, Emilconst subtree = index.subtreeOf(ben);const size = index.subtreeSizeOf(ben); // 5 - Ben included
// The whole family in preorder, and where Cleo stands in it.const order = index.preorder;// order: [0, 1, 3, 6, 7, 4, 2, 5, 8, 9] - Ada, then the subtree of Ben, then that of// Cleo
// 6 - after Ada and Ben's subtreeconst positionOfCleo = index.preorderIndexOf(cleo);PreorderAncestry index = PreorderAncestry.Of(Ancestry.Of(family));ReadOnlySpan<int> subtree = index.SubtreeOf(ben);// subtree: [1, 3, 6, 7, 4] - Ben, Dora, Greta, Hugo, Emilint size = index.SubtreeSizeOf(ben); // 5 - Ben included
// The whole family in preorder, and where Cleo stands in it.ImmutableArray<int> order = index.Preorder;// order: [0, 1, 3, 6, 7, 4, 2, 5, 8, 9] - Ada, then the subtree of Ben, then that// of Cleo
// 6 - after Ada and Ben's subtreeint positionOfCleo = index.PreorderIndexOf(cleo);The preorder lists the roots in ascending order of their
node IDs, and the children of each node in the same way. Here, it lists
Ada, then the subtree of Ben, then the subtree of Cleo. So each subtree takes a range of
consecutive positions. The subtree of Ben takes the positions 1 to 5, and
PreorderAncestry.subtreeOfPreorderAncestry::subtree_ofPreorderAncestry.subtreeOfPreorderAncestry.SubtreeOf returns this range of the preorder without copying
it. Cleo comes at position 6, after Ada and the subtree of Ben.
Good to know
Section titled “Good to know”- Numbered in preorder already? If the node IDs were assigned in preorder, the subtree of a node
is a range of node IDs. An example is a syntax tree numbered in source order, in which each node
comes before the nodes it contains. The range starts at the ID of the node itself, and
PreorderAncestry.subtreeSizeOfPreorderAncestry::subtree_size_ofPreorderAncestry.subtreeSizeOfPreorderAncestry.SubtreeSizeOfreturns how many IDs it holds. - Data by position. In an array of data indexed by the position that
PreorderAncestry.preorderIndexOfPreorderAncestry::preorder_index_ofPreorderAncestry.preorderIndexOfPreorderAncestry.PreorderIndexOfreturns, every subtree is a range of consecutive entries. A sum over a subtree, for example, then comes from the prefix sums of the array. - Dominance.
PreorderAncestry.ofPreorderAncestry::ofPreorderAncestry.ofPreorderAncestry.OfofAncestry.ofAncestry::of_dominatorsAncestry.ofAncestry.Ofdoes the same for the dominator tree of a control-flow graph. There, the subtree of a block holds every block it dominates. See Build the dominator tree and ask it questions. - Not a forest? In a DAG (directed acyclic graph), the descendants of a node are the nodes it reaches. A family with both parents recorded is such a graph. See Find everything a node reaches.
API used
Section titled “API used”Ancestry.of(Graph)Ancestry::ofAncestry.of(forest: Graph)Ancestry.Of(Graph)PreorderAncestry.of(Ancestry)PreorderAncestry::ofPreorderAncestry.ofPreorderAncestry.OfPreorderAncestry.subtreeOf(int)PreorderAncestry::subtree_ofPreorderAncestry.subtreeOfPreorderAncestry.SubtreeOfPreorderAncestry.subtreeSizeOf(int)PreorderAncestry::subtree_size_ofPreorderAncestry.subtreeSizeOfPreorderAncestry.SubtreeSizeOfPreorderAncestry.preorder()PreorderAncestry::preorderPreorderAncestry.preorderPreorderAncestry.PreorderPreorderAncestry.preorderIndexOf(int)PreorderAncestry::preorder_index_ofPreorderAncestry.preorderIndexOfPreorderAncestry.PreorderIndexOfAncestry.of(Dominators)Ancestry::of_dominatorsAncestry.of(dominators: Dominators)Ancestry.Of(Dominators)