Dyck words #
A Dyck word is a sequence consisting of an equal number n of symbols of two types such that
for all prefixes one symbol occurs at least as many times as the other.
If the symbols are ( and ) the latter restriction is equivalent to balanced brackets;
if they are U = (1, 1) and D = (1, -1) the sequence is a lattice path from (0, 0) to (0, 2n)
and the restriction requires the path to never go below the x-axis.
This file defines Dyck words and constructs their bijection with rooted binary trees,
one consequence being that the number of Dyck words with length 2 * n is catalan n.
Main definitions #
DyckWord: a list ofUs andDs with as manyUs asDs and with every prefix having at least as manyUs asDs.DyckWord.semilength: semilength (half the length) of a Dyck word.DyckWord.firstReturn: for a nonempty word, the index of theDmatching the initialU.
Main results #
DyckWord.equivTree: equivalence between Dyck words and rooted binary trees. See the docstrings ofDyckWord.toTreeandDyckWord.ofTreefor details.DyckWord.equivTreesOfNumNodesEq: equivalence between Dyck words of length2 * nand rooted binary trees withninternal nodes.DyckWord.card_dyckWord_semilength_eq_catalan: there arecatalan nDyck words of length2 * nor semilengthn.
Implementation notes #
While any two-valued type could have been used for DyckStep, a new enumerated type is used here
to emphasise that the definition of a Dyck word does not depend on that underlying type.
Named in analogy to Bool.dichotomy.
A Dyck word is a list of DyckSteps with as many Us as Ds and with every prefix having
at least as many Us as Ds.
The underlying list
- count_D_le_count_U (i : โ) : List.count DyckStep.D (List.take i โself) โค List.count DyckStep.U (List.take i โself)
Instances For
Equations
Instances For
Dyck words form an additive cancellative monoid under concatenation, with the empty word as 0.
Equations
The only Dyck word that is an additive unit is the empty word.
Equations
The first element of a nonempty Dyck word is U.
The last element of a nonempty Dyck word is D.
Prefix of a Dyck word as a Dyck word, given that the count of Us and Ds in it are equal.
Equations
Instances For
Suffix of a Dyck word as a Dyck word, given that the count of Us and Ds in the prefix
are equal.
Equations
Instances For
Nest p in one pair of brackets, i.e. x becomes (x).
Equations
Instances For
A property stating that p is nonempty and strictly positive in its interior,
i.e. is of the form (x) with x a Dyck word.
Equations
Instances For
p.firstReturn is 0 if p = 0 and the index of the D matching the initial U otherwise.
Equations
Instances For
The left part of the Dyck word decomposition,
inside the U, D pair that firstReturn refers to. insidePart 0 = 0.
Equations
Instances For
The right part of the Dyck word decomposition,
outside the U, D pair that firstReturn refers to. outsidePart 0 = 0.
Equations
Instances For
Partial order on Dyck words: p โค q if a (possibly empty) sequence of
insidePart and outsidePart operations can turn q into p.
Equations
Convert a Dyck word to a binary rooted tree.
f(0) = nil. For a nonzero word find the D that matches the initial U,
which has index p.firstReturn, then let x be everything strictly between said U and D,
and y be everything strictly after said D. p = x.nest + y with x, y (possibly empty)
Dyck words. f(p) = f(x) โณ f(y), where โณ (defined in Mathlib/Data/Tree/Basic.lean) joins two
subtrees to a new root node.
Equations
Instances For
Convert a binary rooted tree to a Dyck word.
g(nil) = 0. A nonempty tree with left subtree l and right subtree r
is sent to g(l).nest + g(r).
Equations
Instances For
Equivalence between Dyck words and rooted binary trees.
Equations
Instances For
Alias of DyckWord.numNodes_toTree.
Equivalence between Dyck words of semilength n and rooted binary trees with
n internal nodes.
Equations
Instances For
Equations
There are catalan n Dyck words of semilength n (or length 2 * n).
Extension for the positivity tactic: p.firstReturn is positive if p is nonzero.