ELI5: Explain Like I'm 5

Suffix tree

Okay kiddo, let me explain what a suffix tree is. Imagine you have a very long word, like "bananarama". Now, you want to find all the places where the word "ana" occurs in this long word. It could be at the beginning, middle or end of the word. So, how do you find all the occurrences of "ana" in "bananarama"? This is where the suffix tree comes in.

A suffix tree is a special data structure that stores all the suffixes of a given string in a tree-like structure. I know that might sound confusing, so let me break it down for you. When we say "suffix" we mean a sequence of characters at the end of a string. For example, in the word "banana", the suffixes are "a", "na", "ana", "nana", "anana", and "banana".

So, a suffix tree is a tree that contains all the suffixes of a given string. For our example, the suffix tree for "bananarama" would look something like this:

|- a
| |- na
| | |- ra
| | | |- ma
| | |- ra
| | |- ma
| |
| |- ra
| |- ma
|
|- na
|- ra
| |- ma
|
|- ma

Here, the edges represent the characters in the string "bananarama". Each node represents a substring that occurs in the original string. The root node represents the empty string. The terminal nodes (nodes without any children) represent the suffixes of the string in the leaf nodes.

So, why is this useful? Well, once we have built a suffix tree, we can use it to quickly search for a substring in the original string. We start at the root node and follow the edges corresponding to the characters of the substring we are searching for. If we reach a terminal node, it means the substring is present in the original string! The path from the root to the terminal node gives us the position(s) of the substring in the original string.

I hope that makes sense, kiddo! Let me know if you have any more questions.
Related topics others have asked about: