6.3 Find the lowest common ancestor of two nodes
You find the lowest common ancestor of two nodes: the deepest node that is an ancestor of both.
Problem
Section titled “Problem”You have two nodes in a hierarchy and want the first node they share on the way up to the top. Two nodes in the same tree have the root as a common ancestor, but you want the deepest one. It is called the lowest common ancestor. It is the last node that the paths from the root to the two nodes have in common. You need this to find:
- the innermost element of a syntax tree that encloses two expressions;
- the smallest folder that contains two files;
- the lowest manager who is responsible for two employees;
- the most recent common ancestor of two people in a family tree that records one parent per 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. What is the lowest common ancestor of the second cousins Greta and Ida? What is it for
Greta and her uncle Emil, for the siblings Hugo and Greta, and for Dora and her son
Hugo?
- Person asked about
- Lowest common ancestor
The picture after shows the results of all four questions: Ada, Ben and Dora. Dora is also
one of the people asked about. Ada is the lowest common ancestor of Greta and Ida: the paths
from the root to the two of them have only Ada in common.
Solution
Section titled “Solution”Ancestry ancestry = Ancestry.of(family);int commonOfSecondCousins = ancestry.lowestCommonAncestorOf(greta, ida); // 0 - Adaint commonOfUncleAndNiece = ancestry.lowestCommonAncestorOf(greta, emil); // 1 - Benint commonOfSiblings = ancestry.lowestCommonAncestorOf(hugo, greta); // 3 - Dora// 3 - Dora herselfint commonOfMotherAndSon = ancestry.lowestCommonAncestorOf(dora, hugo);let ancestry = Ancestry::of(&family)?;// Some(0) - Adalet common_of_second_cousins = ancestry.lowest_common_ancestor_of(greta, ida);// Some(1) - Benlet common_of_uncle_and_niece = ancestry.lowest_common_ancestor_of(greta, emil);// Some(3) - Doralet common_of_siblings = ancestry.lowest_common_ancestor_of(hugo, greta);// Some(3) - Dora herselflet common_of_mother_and_son = ancestry.lowest_common_ancestor_of(dora, hugo);const ancestry = Ancestry.of(family);// 0 - Adaconst commonOfSecondCousins = ancestry.lowestCommonAncestorOf(greta, ida);// 1 - Benconst commonOfUncleAndNiece = ancestry.lowestCommonAncestorOf(greta, emil);// 3 - Doraconst commonOfSiblings = ancestry.lowestCommonAncestorOf(hugo, greta);// 3 - Dora herselfconst commonOfMotherAndSon = ancestry.lowestCommonAncestorOf(dora, hugo);Ancestry ancestry = Ancestry.Of(family);// 0 - Adaint commonOfSecondCousins = ancestry.LowestCommonAncestorOf(greta, ida);// 1 - Benint commonOfUncleAndNiece = ancestry.LowestCommonAncestorOf(greta, emil);// 3 - Doraint commonOfSiblings = ancestry.LowestCommonAncestorOf(hugo, greta);// 3 - Dora herselfint commonOfMotherAndSon = ancestry.LowestCommonAncestorOf(dora, hugo);Ada is the lowest common ancestor of Greta and Ida. Ben is that of Greta and her uncle
Emil, and Dora is that of the siblings Hugo and Greta. A node is its own ancestor, so the
lowest common ancestor of Dora and her son Hugo is Dora herself.
Good to know
Section titled “Good to know”- A node is its own ancestor, so the lowest common ancestor of a node and one of its descendants is the node itself. One node is therefore an ancestor of another exactly if it is the lowest common ancestor of the two. See Test many times whether one node is an ancestor of another.
- Different trees. In a forest, two nodes in different trees have no common ancestor, and the
result is
-1None-1-1. - Dominance.
Ancestry.ofAncestry::of_dominatorsAncestry.ofAncestry.Ofbuilds the same index for the dominator tree of a control-flow graph. There, the lowest common ancestor of two blocks is the lowest block that dominates both. See Build the dominator tree and ask it questions.
API used
Section titled “API used”Ancestry.of(Graph)Ancestry::ofAncestry.of(forest: Graph)Ancestry.Of(Graph)Ancestry.lowestCommonAncestorOf(int, int)Ancestry::lowest_common_ancestor_ofAncestry.lowestCommonAncestorOfAncestry.LowestCommonAncestorOfAncestry.of(Dominators)Ancestry::of_dominatorsAncestry.of(dominators: Dominators)Ancestry.Of(Dominators)