Skip to content

6.4 Test many times whether one node is an ancestor of another

You test whether a node is an ancestor of another, in logarithmic time with the ancestor index and in constant time with the index with the preorder.

You want to know whether one node lies above another in a hierarchy, and you ask that very often. In a tree, a node that lies above another is called its ancestor. An analysis may ask this for every pair of nodes it looks at. That can be millions of times, so each answer should be cheap. You need this to find out:

  • whether an expression lies inside a given loop or block of a program;
  • whether an employee belongs to a department, directly or through one of its sub-departments;
  • whether a file lies in a folder or in one of its subfolders;
  • whether a person descends from another.

The example graph family models a family tree that records one parent per person. It has an edge from each parent to each child. Is Ben an ancestor of Hugo? Is Cleo? Is Hugo himself?

The family tree and the people asked about
The people asked about and the other ancestors of Hugo
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
Context
  • Other ancestor of Hugo

The ancestors of Hugo are the nodes on the path from the root to him, including Hugo himself. In the picture after, Ben and Hugo are two of them and Cleo is not. Ada and Dora are the other ancestors, whom the example does not ask about.

Ancestry ancestry = Ancestry.of(family);
// true - his grandfather
boolean benIsAncestor = ancestry.isAncestorOf(ben, hugo);
// false - his great-aunt
boolean cleoIsAncestor = ancestry.isAncestorOf(cleo, hugo);
// true - his own ancestor
boolean hugoIsAncestor = ancestry.isAncestorOf(hugo, hugo);
// For many tests, the index with the preorder answers each one in constant time.
PreorderAncestry index = PreorderAncestry.of(ancestry);
boolean fastBenIsAncestor = index.isAncestorOf(ben, hugo); // true
Full example
let ancestry = Ancestry::of(&family)?;
let ben_is_ancestor = ancestry.is_ancestor_of(ben, hugo); // true - his grandfather
let cleo_is_ancestor = ancestry.is_ancestor_of(cleo, hugo); // false - his great-aunt
let hugo_is_ancestor = ancestry.is_ancestor_of(hugo, hugo); // true - his own ancestor
// For many tests, the index with the preorder answers each one in constant time.
let index = PreorderAncestry::of(&ancestry);
let fast_ben_is_ancestor = index.is_ancestor_of(ben, hugo); // true
Full example
const ancestry = Ancestry.of(family);
const benIsAncestor = ancestry.isAncestorOf(ben, hugo); // true - his grandfather
const cleoIsAncestor = ancestry.isAncestorOf(cleo, hugo); // false - his great-aunt
const hugoIsAncestor = ancestry.isAncestorOf(hugo, hugo); // true - his own ancestor
// For many tests, the index with the preorder answers each one in constant time.
const index = PreorderAncestry.of(ancestry);
const fastBenIsAncestor = index.isAncestorOf(ben, hugo); // true
Full example
Ancestry ancestry = Ancestry.Of(family);
// true - his grandfather
bool benIsAncestor = ancestry.IsAncestorOf(ben, hugo);
// false - his great-aunt
bool cleoIsAncestor = ancestry.IsAncestorOf(cleo, hugo);
// true - his own ancestor
bool hugoIsAncestor = ancestry.IsAncestorOf(hugo, hugo);
// For many tests, the index with the preorder answers each one in constant time.
PreorderAncestry index = PreorderAncestry.Of(ancestry);
bool fastBenIsAncestor = index.IsAncestorOf(ben, hugo); // true
Full example

Ben is the grandfather of Hugo and so an ancestor of him. Cleo is his great-aunt and no ancestor of him. Hugo is his own ancestor. The index with the preorder gives the same answers, each in constant time instead of logarithmic time.