Palindrome tree
This article needs additional citations for verification. (February 2022) |
Palindrome tree | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Type | Tree | ||||||||||||||||||||
Invented | 2015 | ||||||||||||||||||||
Invented by | Mikhail Rubinchik, Arseny M. Shur | ||||||||||||||||||||
|
In
Description

Like most trees, a palindrome tree consists of
Operations
Add
Since a palindrome tree follows an online construction, it maintains a pointer to the last palindrome added to the tree. To add the next character to the palindrome tree, add(x)
first checks if the first character before the palindrome matches the character being added, if it does not, the suffix links are followed until a palindrome can be added to the tree. Once a palindrome has been found, if it already existed in the tree, there is no work to do. Otherwise, a new vertex is added with a link from the suffix to the new vertex, and a suffix link for the new vertex is added. If the length of the new palindrome is 1, the suffix link points to the root of the palindrome tree that represents a length of −1.
# S -> Input String
# x -> position in the string of the character being added
def add(x: int) -> bool:
"""Add character to the palindrome tree."""
while True:
if x - 1 - current.length >= 0 and S[x - 1 - current.length] == S[x]:
break
current = current.suffix
if current.add[S[x]] is not None:
return False
suffix = current
current = Palindrome_Vertex()
current.length = suffix.length + 2
suffix.add[S[x]] = current
if current.length == 1:
current.suffix = root
return True
while True:
suffix = suffix.suffix
if x - 1 - suffix.length >= 0 and S[x - 1 - suffix.length] == S[x]:
current.suffix = suffix.add[S[x]]
return True
Joint trees
Finding palindromes that are common to multiple strings or unique to a single string can be done with additional space where is the number of strings being compared. This is accomplished by adding an array of length to each vertex, and setting the flag to 1 at index if that vertex was reached when adding string . The only other modification needed is to reset the current pointer to the root at the end of each string. By joining trees in such a manner the following problems can be solved:
- Number of palindromes common to all strings
- Number of unique palindromes in a string
- Longest palindrome common to all strings
- The number of palindromes that occur more often in one string than others
Complexity
Time
Constructing a palindrome tree takes time, where is the length of the string and is the size of the alphabet. With calls to add(x)
, each call takes
add(x)
, the cost of moving up the tree more than once is 'paid for' by an equal number of calls to add(x)
when moving up the tree did not occur.
Space
A palindrome tree takes space: At most vertices to store the sub-palindromes and two roots, edges, linking the vertices and suffix edges.
Space–time tradeoff
If instead of storing only the add edges that exist for each palindrome an array of length edges is stored, finding the correct edge can be done in constant time reducing construction time to while increasing space to , where is the number of palindromes.
References
- arXiv:1506.04862v1.
- S2CID 41095273.
- S2CID 14871164.