Palindromic trees #
The eertree of a word has an imaginary root of length -1, the empty palindrome, and one
vertex for each distinct nonempty palindromic factor. A letter-labelled edge extends a word
at both ends, or creates a singleton from the imaginary root. The suffix link of a nonempty
palindrome goes to its longest proper palindromic suffix. Both roots link to the imaginary root.
This executable representation implements the graph of Rubinchik and Shur, arXiv:1506.04862v2, §2, pp. 3–5, using full words as keys. Appending a letter retains the previous tree. A longest-first suffix scan stops at the first palindrome to identify the one possible new vertex and its link. The bound counts graph vertices. The paper's time and space bounds use a different representation and search.
Word formed by a letter-labelled extension from a vertex.
The text read so far and the suffix-link table indexed by complete palindromic words. The imaginary root is implicit. Each other vertex has exactly one entry.
- text : List α
Current input prefix.
A word key represents a vertex, and the associated value is its suffix link.
The empty word's eertree already contains its two roots.
Append one letter, adding a suffix-link entry only for a previously unseen longest palindromic suffix. All earlier entries retain their links.
Build a palindromic tree one letter at a time from left to right.
Follow the suffix link of a vertex. An absent word has no link.
Look up the outgoing edge labelled a. Full word keys determine extension edges.
The word vertices are exactly the distinct palindromic factors, including the empty root.
From either root or a word vertex, a letter-labelled edge exists precisely when the extended word occurs as a palindromic factor. The extended word determines the target.
The empty word has a suffix link to the imaginary root at every prefix.
A nonempty vertex links to the longest palindromic suffix strictly shorter than itself.