Skip to content

6.2 Find the parent and the depth of each node

You build an ancestor index that gives the parent and the depth of every node of a forest.

You have a hierarchy and want to know where each node sits in it: which node is directly above it, and how far below the top it lies. The node directly above a node is called its parent. The number of edges from the root down to a node is called its depth. You want both for every node, in one index that also answers questions about ancestors. You need this to find:

  • the enclosing element of a node in a syntax tree, and how deeply it is nested;
  • the manager of an employee, and the level of management;
  • the folder of a file, and how deeply it is nested.

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 is the parent of Greta, and in which generation is she? Who is the parent of Ada? And in which generation is each of the others?

The family tree and the people asked about
The generations and the parent of Greta
0: Ada1: Ben2: Cleo3: Dora4: Emil5: Finn6: Greta7: Hugo8: Ida9: Jonas
0: Ada1: Ben2: Cleo3: Dora4: Emil5: Finn6: Greta7: Hugo8: Ida9: Jonasgeneration 3generation 2generation 1generation 0
Input
  • Person asked about
Output
  • Generation
  • Parent

In the picture after, the frames show the generation of every person. The generation is the depth that the index returns. Dora is the parent of Greta. Ada is the root and has no parent.

Ancestry ancestry = Ancestry.of(family);
int parent = ancestry.parentOf(greta); // 3 - Dora
int generation = ancestry.depthOf(greta); // 3 - three generations below Ada
int parentOfAda = ancestry.parentOf(ada); // -1 - Ada is the root
// The generation of every person, by node ID.
int[] generations = new int[family.nodeCount()];
for (int person = 0; person < generations.length; person++) {
generations[person] = ancestry.depthOf(person);
}
// generations: [0, 1, 1, 2, 2, 2, 3, 3, 3, 3] - Ada in 0, Greta, Hugo, Ida and
// Jonas in 3
Full example
let ancestry = Ancestry::of(&family)?;
let parent = ancestry.parent_of(greta); // Some(3) - Dora
let generation = ancestry.depth_of(greta); // 3 - three generations below Ada
let parent_of_ada = ancestry.parent_of(ada); // None - Ada is the root
// The generation of every person, by node ID.
let generations: Vec<u32> = family
.nodes()
.map(|person| ancestry.depth_of(person))
.collect();
// generations: [0, 1, 1, 2, 2, 2, 3, 3, 3, 3] - Ada in 0, Greta, Hugo, Ida and
// Jonas in 3
Full example
const ancestry = Ancestry.of(family);
const parent = ancestry.parentOf(greta); // 3 - Dora
const generation = ancestry.depthOf(greta); // 3 - three generations below Ada
const parentOfAda = ancestry.parentOf(ada); // -1 - Ada is the root
// The generation of every person, by node ID.
const generations = new Int32Array(family.nodeCount);
for (let person = 0; person < generations.length; person++) {
generations[person] = ancestry.depthOf(person);
}
// generations: [0, 1, 1, 2, 2, 2, 3, 3, 3, 3] - Ada in 0, Greta, Hugo, Ida and
// Jonas in 3
Full example
Ancestry ancestry = Ancestry.Of(family);
int parent = ancestry.ParentOf(greta); // 3 - Dora
int generation = ancestry.DepthOf(greta); // 3 - three generations below Ada
int parentOfAda = ancestry.ParentOf(ada); // -1 - Ada is the root
// The generation of every person, by node ID.
int[] generations = new int[family.NodeCount];
for (int person = 0; person < generations.Length; person++)
{
generations[person] = ancestry.DepthOf(person);
}
// generations: [0, 1, 1, 2, 2, 2, 3, 3, 3, 3] - Ada in 0, Greta, Hugo, Ida and
// Jonas in 3
Full example

Dora is the parent of Greta, who is three generations below Ada. Ada is the root and has no parent, so Ancestry.parentOfAncestry::parent_ofAncestry.parentOfAncestry.ParentOf returns -1None-1-1 for her. The depths of all the people are the generations of the picture after.