Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Lemma 3 — the height-hhh ancestor of vvv has number 2h+1⌊sym(v)/2h+1⌋+2h2^{h+1}\lfloor \mathrm{sym}(v)/2^{h+1}\rfloor + 2^h2h+1⌊sym(v)/2h+1⌋+2h

Proved
HarelTarjan.SymOrder.lemma3_ancestor_number

by mikedeng1 · Sep 27, 2026 · Mathlib 0df444a (Lean v4.33.1)

binary-treenumber-theoryp2o-batch-p100bp2o-gran-per-chapterp2o-plan-paperp2o-v1trees

Let TTT be the complete binary tree of depth ddd, numbered in symmetric order. If vvv is a vertex and hhh is a height (h≤dh \le dh≤d) such that h≥h(v)h \ge h(v)h≥h(v), then vvv has an ancestor of height hhh, and the ancestor uuu of vvv of height hhh has number

sym(u)=2h+1⌊sym(v)/2h+1⌋+2h.\mathrm{sym}(u) = 2^{h+1} \left\lfloor \mathrm{sym}(v) / 2^{h+1} \right\rfloor + 2^h.sym(u)=2h+1⌊sym(v)/2h+1⌋+2h.

Lemma 3 is the arithmetic behind the depth algorithm: an ancestor is found from the number of vvv by clearing low-order bits.

Formalization Note "hhh is a height" is made explicit as h≤dh \le dh≤d. The existence of the ancestor, presupposed by "the ancestor", is stated as a conjunct. The floor is natural-number division.

Preamble
import Mathlib
import Definitions.Def_HarelTarjan_SymOrder_Tree
import Definitions.Def_HarelTarjan_SymOrder_Sym
Formal statement
namespace HarelTarjan.SymOrder

/-- Lemma 3 (p. 342): if `v` is a vertex and `h` is a height (`h ≤ d`) such that `h ≥ h(v)`, then
`v` has an ancestor of height `h`, and the ancestor of `v` of height `h` has number
`2^{h+1} ⌊sym(v) / 2^{h+1}⌋ + 2^h` (`/` is floor division on `ℕ`). -/
theorem lemma3_ancestor_number {d : ℕ} (v : Vertex d) (h : ℕ) (hvh : height v ≤ h)
    (hhd : h ≤ d) :
    (∃ u : Vertex d, IsAncestor u v ∧ height u = h) ∧
      ∀ u : Vertex d, IsAncestor u v → height u = h →
        sym u = 2 ^ (h + 1) * (sym v / 2 ^ (h + 1)) + 2 ^ h := by sorry

end HarelTarjan.SymOrder
Source
Harel, Tarjan, Fast Algorithms for Finding Nearest Common Ancestors, SIAM J. Comput. 13 (1984), p. 342, Lemma 3
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

This theorem gives the number of an ancestor at a given height. Let d∈Nd\in\mathbb Nd∈N and let TdT_dTd​ be the set of boolean sequences of length ≤d\le d≤d. The height is h(v)=d−∣sv∣h(v)=d-|s_v|h(v)=d−∣sv​∣, and uuu is an ancestor of vvv when sus_usu​ is a prefix of svs_vsv​, equality allowed. The number sym⁡(v)\operatorname{sym}(v)sym(v) counts the u′∈Tdu'\in T_du′∈Td​ with κ(u′)≤lexκ(v)\kappa(u')\le_{\mathrm{lex}}\kappa(v)κ(u′)≤lex​κ(v). The key κ\kappaκ replaces false by 000 and true by 222 and appends 111, and the order is lexicographic with a proper prefix counting as smaller.

The theorem takes a vertex vvv and a natural number hhh with

h(v)≤h≤d.h(v)\le h\le d.h(v)≤h≤d.

It then asserts two things:

  1. Some ancestor uuu of vvv has height hhh.
  2. Every ancestor uuu of vvv with height hhh satisfies
sym⁡(u)=2h+1⌊sym⁡(v)2h+1⌋+2h.\operatorname{sym}(u)=2^{h+1}\left\lfloor\frac{\operatorname{sym}(v)}{2^{h+1}}\right\rfloor+2^h.sym(u)=2h+1⌊2h+1sym(v)​⌋+2h.

Degenerate cases:

  • If h=h(v)h=h(v)h=h(v), the ancestor is vvv itself. The formula then gives back sym⁡(v)\operatorname{sym}(v)sym(v), provided sym⁡(v)\operatorname{sym}(v)sym(v) is an odd multiple of 2h2^h2h.
  • If h=dh=dh=d, the ancestor is the root.
  • For d=0d=0d=0, only h=0h=0h=0 with the root is allowed.
  • The hypotheses are always satisfiable, for example with h=h(v)h=h(v)h=h(v).
Human review
  • Endorsed by Shuze Chen · Sep 27, 2026

    Confirmed by the moderator at approval.

  • Endorsed by mikedeng1 · Sep 27, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me