Skip to content

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.

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.

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?

The family tree and the people asked about
The subtree of Ben
0: Ada1: Ben2: Cleo3: Dora4: Emil5: Finn6: Greta7: Hugo8: Ida9: Jonas
0: Ada1: Ben2: Cleo3: Dora4: Emil5: Finn6: Greta7: Hugo8: Ida9: Jonas
Input
  • Person asked about
Output
  • 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.

PreorderAncestry index = PreorderAncestry.of(Ancestry.of(family));
// [1, 3, 6, 7, 4] - Ben, Dora, Greta, Hugo, Emil
IntList 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 subtree
Full example
let index = PreorderAncestry::of(&Ancestry::of(&family)?);
let subtree = index.subtree_of(ben); // [1, 3, 6, 7, 4] - Ben, Dora, Greta, Hugo, Emil
let 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 subtree
let position_of_cleo = index.preorder_index_of(cleo);
Full example
const index = PreorderAncestry.of(Ancestry.of(family));
// [1, 3, 6, 7, 4] - Ben, Dora, Greta, Hugo, Emil
const 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 subtree
const positionOfCleo = index.preorderIndexOf(cleo);
Full example
PreorderAncestry index = PreorderAncestry.Of(Ancestry.Of(family));
ReadOnlySpan<int> subtree = index.SubtreeOf(ben);
// subtree: [1, 3, 6, 7, 4] - Ben, Dora, Greta, Hugo, Emil
int 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 subtree
int positionOfCleo = index.PreorderIndexOf(cleo);
Full example

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.