ArticleslgStudy

computer science

Palindrome tree

Palindrome tree is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Palindrome tree rather than just read about it. In short: In computer science a palindrome tree, also called an EerTree, is a type of search tree, that allows for fast access to all palindromes contained in a string. They can be used to solve the longest palindromic substring, the k-factorization problem (can a given string be divided into exactly k palindromes), palindromic length of a string (what is the minimum number of palindromes needed to construct the string), and…

Palindrome tree — main illustration
Palindrome tree — illustration

Key takeaways

  • Palindrome tree belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Palindrome tree to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Palindrome tree from memory before moving on to harder problems.

Reference excerpt

In computer science a palindrome tree, also called an EerTree, is a type of search tree, that allows for fast access to all palindromes contained in a string. They can be used to solve the longest palindromic substring, the k-factorization problem (can a given string be divided into exactly k palindromes), palindromic length of a string (what is the minimum number of palindromes needed to construct the string), and finding and counting all distinct sub-palindromes. Palindrome trees do this in an online manner, that is it does not require the entire string at the start and can be added to character by character.

Description

Like most trees, a palindrome tree consists of vertices and directed edges. Each vertex in the tree represents a palindrome (e.g. 'tacocat') but only stores the length of the palindrome, and each edge represents either a character or a suffix. The character edges represent that when the character is appended to both ends of the palindrome represented by the source vertex, the palindrome in the destination vertex is created (e.g. an edge labeled 't' would connect the source vertex 'acoca' to the destination vertex 'tacocat'). The suffix edge connects each palindrome to the largest palindrome suffix it possesses (in the previous example 'tacocat' would have a suffix edge to 't', and 'atacocata' would have a suffix link to 'ata'). Where palindrome trees differ from regular trees, is that they have two roots (as they are in fact two separate trees). The two roots represent palindromes of length −1, and 0. That is, if the character 'a' is appended to both roots the tree will produce 'a' and 'aa' respectively. Since each edge adds (or removes) an even number of characters, the two trees are only ever connected by suffix edges.

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.

Joint trees Finding palindromes that are common to multiple strings or unique to a single string can be done with O ( n ∗ i ) {\displaystyle O(n*i)} additional space where i {\displaystyle i} is the number of strings being compared. This is accomplished by adding an array of length i {\displaystyle i} to each vertex, and setting the flag to 1 at index i {\displaystyle i} if that vertex was reached when adding string i {\displaystyle i} . 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 O ( n log ⁡ σ ) {\displaystyle O(n\log {\sigma })} time, where n {\displaystyle n} is the length of the string and σ {\displaystyle \sigma } is the size of the alphabet. With n {\displaystyle n} calls to add(x), each call takes O ( log ⁡ σ ) {\displaystyle O(\log {\sigma })} amortized time. This is a result of each call to add(x) increases the depth of the current vertex (the last palindrome in the tree) by at most one, and searching all possible character edges of a vertex takes O ( log ⁡ σ ) {\displaystyle O(\log {\sigma })} time. By assigning the cost of moving up and down the tree to each call to 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 O ( n ) {\displaystyle O(n)} space: At most n + 2 {\displaystyle n+2} vertices to store the sub-palindromes and two roots, n {\displaystyle n} edges, linking the vertices and n + 2 {\displaystyle n+2} suffix edges.

Space–time tradeoff If instead of storing only the add edges that exist for each palindrome an array of length σ {\displaystyle \sigma } edges is stored, finding the correct edge can be done in constant time reducing construction time to O ( n + p ∗ σ ) {\displaystyle O(n+p*\sigma )} while increasing space to O ( p ∗ σ ) {\displaystyle O(p*\sigma )} , where p {\displaystyle p} is the number of palindromes.

References

Worked examples

Example 1 — a first encounter with Palindrome tree

Start with the simplest possible case. Write down what Palindrome tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Palindrome tree before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Palindrome tree ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Palindrome tree

In research
Palindrome tree appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Palindrome tree in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Palindrome tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics Trees (data structures), so understanding it makes those chapters shorter.
In everyday life
Look for Palindrome tree outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Palindrome tree” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Palindrome tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Palindrome tree means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Palindrome tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Palindrome tree in simple terms?

In computer science a palindrome tree, also called an EerTree, is a type of search tree, that allows for fast access to all palindromes contained in a string. They can be used to solve the longest palindromic substring, the k-factorization problem (can a given string be divided into exactly k palin…

Why does Palindrome tree matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Palindrome tree?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Palindrome tree.

Tags

  • Trees (data structures)

Keep exploring