Version: 0.6.22
6. Prefix and Suffix Trees#
- Lecture:
Lecture 5.6
(slides)- Objectives:
Understand what is a prefix and what is a suffix tree
- Concepts:
Insertion, deletion and search into a Prefix-tree
- Implementation:
Scala PrefixTree.scala
As a last example of tree data structures, we shall look at prefix and suffix trees. These are data structures used to speed up text search. especially full-text search and auto completion in text editors.
Auto-completion means that editor suggests possible endings for what you have started to type. For instance, Fig. 6.13 shows an example of “default completion”, where the editor make suggestions based on words already in the document.
Full-text search, is what happen for instance when you press
Ctrl-F, which prompt you for a some text, and highlights all the occurrences in the page. This is often available in text editors, web browser, or IDEs.
Prefixes vs. Suffixes
What are prefixes and suffixes? Intuitively, we can say the are just “beginning” and “ending” of sequences. More formally, given a sequence of characters \(s = (c_1, c_2, \dots, c_n)\) any shorted sequence \(s' = (c_1, \dots, c_k)\) where \(0 \leq k \leq n\) is a prefix of \(S\). For instance, the word “algorithm” has the following prefixes (ordered by length):
Note that the set of prefixes includes the empty sequence and the complete word itself. Formally, we denote that fact that a word \(w\) admits a prefix \(p\) by \(p \preceq w\).
Similarly, suffixes are possible endings. Formally, and sequence \(s' = (c_k, \dots, c_n)\) is a suffix of \(s\). The word algorithm therefore admits the following suffixes:
6.1. Prefix Trees#
How can we implement such auto-completion mechanisms efficiently? What we need is kind of Map ADT that could give us all the word that starts by the “prefix” the user has typed. For instance, given a set of words already typed, one could index them by prefix, using a hash table.
See also
📖 Goodrich, M. T., Tamassia, R., & Goldwasser, M. H. (2014). Data structures and algorithms in Java. John Wiley & Sons. Chap. 13
The conventional solution is to use a prefix tree, also known as trie. A prefix tree is not an ADT per se, but it extends the Set ADT and generally exposes the following operations:
- tries.contains(t: Trie, w: Word) Boolean#
Check whether the prefix tree \(t\) contains the given word \(w\).
- tries.prefix_search(t: Trie, p: Word) Set[Word]#
Returns the subset \(S\) of all the words from \(t\) that starts with the given prefix \(p\).
- Pre-conditions:
None
- Post-conditions:
(A1) \(S\) is the very subset of words from the prefix tree that start with the given prefix \(p\). No other words in the tree starts with \(p\) and S does not contain any other word than those from the tree.
\[S = \mathit{prefix\_search}(t, p) \implies S = \{ w \in \mathcal{A}^* \:|\: contains(t, w) \land p \preceq w \}\]
- tries.add(t: Trie, w: Word) Trie#
Add a new word in the prefix tree \(t\).
- Pre-conditions:
None
- Post-conditions:
(A2) The word is \(w\) is necessary in the resulting prefix tree, that is:
\[t' = add(t, w) \Longleftrightarrow contains(t', w)\]
- tries.remove(t: Trie, w: word) Trie#
Remove the given word \(w\) from the the prefix tree \(t\).
- Pre-conditions:
None
- Post-conditions:
(A3) The word that is removed is not anymore in the result prefix tree, that is:
\[t' = remove(t, w) \Longleftrightarrow \neg \, contains(t', w)\]
6.1.1. Basic Structure#
A prefix tree, is an \(n\)-ary tree meant to contains a set of words. Every node in the tree contains a symbol from the alphabet of these words. The root contains a special symbol \(\varepsilon\), which represents the empty symbol. The leaves carries another special symbol, \(\bot\), which denotes the end of a word. This way every path from the root to a leaf node represents a word starting from \(\varepsilon\) and ending with \(\bot\).
Fig. 6.14 shows a prefix tree built over the words: ‘apart’, ‘apartment’, ‘ape’, ‘apear’, ‘apple’, ‘apply’, ‘apricot’, ‘april’.
Fig. 6.14 A prefix tree built on the set of eight words \(S = \{'apart', 'apartment', 'ape', \, 'apear',\, 'apple', 'apply',\, 'apricot',\, 'april' \}\)#
Each leaf is annotated with the word that can be read following the path from the root to that leaf. Similarly, the path that leads to an internal node represent a prefix and its descend leaves are all the words that start with that prefix.
In a sense, a node somehow represents the characters that have been typed so far by the user. On the root for instance, the user hasn’t typed anything yet and all the words are possible. As we move down along a specific path, we start narrowing down the set of possible worda. In Fig. 6.14, in Node 2, we know the user has typed an “a”, in Node 3, we know the suer has typed the prefix “ap”, in Node 4, the prefix “app”, and so on and so forth.
Why Use Special Characters?
The special characters \(\varepsilon\) and \(\bot\) allow handling words that are also a prefix of another longer word, such as ‘apart’ which is a prefix of ‘apartment’. Without the special \(\bot\) character, we would have no way to know that apart is also a valid word in the set.
Implementation in Scala
Let see how we could implement this. Prefix tree is a data structure where recursion makes things easier.
First we need to define the “facade” of our PrefixTree as
follows. Here we are just declaring the operation we have listed in
our ADT. I added an operation size for convenience: The size is
simply the number of words that the prefix tree contains. We will
see later how to implement these operations.
class PrefixTree:
def add(word: String): Unit =
throw new Error("Not implemented")
def contains(word: String): Boolean =
throw new Error("Not implemented")
def prefixSearch(prefix: String): Set[String] =
throw new Error("Not implemented")
def remove(word: String): Unit =
throw new Error("Not implemented")
def words: Set[String] =
throw new Error("Not implemented")
def size: Int = words.size
Now we define some more classes to represent the nodes of our prefix tree. I choose to use two separate classes for leaves and branches as follows.
First I defined the interface for an node in general, regardless whether it is a leaf or a branch. Node simply expose the same operations as our ADT. I then add two subclasses that implements the leaves, and branches, respectively.
abstract class Node(val symbol: Char):
def contains(word: String): Boolean
def add(word: String): Node
def words: Set[String]
def prefixSearch(prefix: String): Set[String]
def remove(word: String): Option[Node]
class Leaf(symbol: Char) extends Node(symbol)
class Branch(symbol: Char, children: Map[Char, Node]) extends Node(symbol)
6.1.2. Compressed Prefix Tree#
This default tree structure is not really efficient, because it consumes a lot memory: There are many nodes that have only one child. To reduce the memory overhead, prefix trees are compressed as follows.
We place more than one symbol in each node, and merge those nodes that have only one child, consequently. Fig. 6.15 shows the compressed version of the prefix tree initially shown on Fig. 6.14. Now, all nodes but the leaves have two or more children.
Fig. 6.15 A compressed prefix tree, equivalent to the one shown on Fig. 6.14#
6.1.3. Membership#
To check whether a given word \(w=(c_1,\dots,c_k)\) exists in our prefix tree, we use the following procedure (assuming a non-compressed prefix tree):
We start at the root of the tree, which becomes our “current node” \(n\).
If Node \(n\) is a branch
If the word \(w\) is empty, we return true only if Node \(n\) has a child whose symbol is \(\bot\). This child is a leaf, which marks the end of a word.
Else, if Node \(n\) has a child node \(c\) whose symbol is \(c_1\) (the first character)
We remove that first character from Word \(w\)
Node \(c\) becomes our current node and we continue at Step 2.
Else, we return false.
Otherwise (Node \(n\) is a leaf), we return true only if the given word \(w\) is empty (i.e., \(w=''\)).
Figure Fig. 6.16 illustrates how this procedure operates. It shows how the search for “ape” at the root node, reduces to searching for “pe” on the child, which reduces to searching for “e” on the grandchild, which reduces to searching for the empty word.
Fig. 6.16 Searching for the word “ape” in the prefix tree initially shown on Fig. 6.14, shortened for the sake conciseness.#
Implementation in Scala
We implement the case where the node is a branch (see Step 2) in
our Branch class as follows. In Scala (and some other languages)
the first character of a string is named “head”, and the rest is
named “tail”. The constant END stands for the symbol
\(\bot\).
1class Branch(symbol: Char, val children: Map[Char, Node]) extends Node(symbol):
2
3 def contains(word: String): Boolean =
4 if word.isEmpty then
5 children.contains(END)
6 else
7 children.get(word.head) match
8 case None => false
9 case Some(child) => child.contains(word.tail)
The case where the node is a leaf (see Step 3) goes as follows:
1class Leaf extends Node(END):
2
3 def contains(word: String): Boolean =
4 word.isEmpty
Finally, we complete our PrefixTree with the following
implementation:
1class PrefixTree:
2
3 private var root: Node = Branch(ROOT, Map())
4
5 def contains(word: String): Boolean =
6 root.contains(word)
- Why Is This Correct?
Every word stored in the tree is spelled by exactly one path, from the root down to a leaf \(\bot\). Besides, the children of a node all carry distinct symbols, so, at each step, there is at most one child to follow: The procedure follows the only path that may spell \(w\). It reaches the end of that path (i.e., the word is empty) and finds a \(\bot\) leaf if and only if \(w\) was added to the tree. In particular, a prefix of a stored word is not found (unless it was added too): There is a path for it, but it does not end with a \(\bot\) leaf.
How Efficient Is This?
Let us consider searching for a word \(w=(c_1,\ldots,c_n)\). How much time does it require? In the best case, we deduce straight in the root node that the word \(w\) is not in the tree, because the root has no children whose symbol matches its character, \(c_1\). Besides, if branch nodes are implemented using hashing, that local search runs in \(O(1)\). If it were implemented with a linear search, it would run in \(O(|\mathcal{A}|)\) where \(\mathcal{A}\) represents the alphabet over which the words are built.
What is the worse case? The worst case occurs when we search for the longest word that exists in the tree. This longest word is stored along the longest branch of the tree, and, in that case, we have to check every node along that long branch. The would give us a worst case runtime that is linear to the length of the word \(w\), that is \(O(n)\). Again if searching for specific child is implemented using a local search, then, the search would run in \(O(n \cdot |\mathcal{A}|)\)
As for the memory, the recursive implementation I showed above would require at \(O(n)\) memory, as each node would place a new call to progress further down the tree.
6.1.4. Prefix Search#
To find all the words that start with a given prefix \(p=(c_1, \dots, c_n)\), we navigate the tree from the root according to the characters in the given prefix, and then we collect all the words stored below the node we have reached. This is what makes auto-completion possible.
We start at the root of the tree, which becomes our “current node” \(n\).
If the prefix \(p\) is empty, then every word stored below Node \(n\) starts with \(p\). We return all of them (see below how to collect them).
Otherwise, if Node \(n\) has a child node \(c\) whose symbol is \(c_1\) (the first character of the prefix)
We remove that first character from the prefix \(p\).
We search below Node \(c\), which becomes our current node, and we continue at Step 2. Each word found is then preceded by the symbol of Node \(c\).
Else (there is no such child), no word starts with \(p\), and we return the empty set.
To collect all the words stored below a node, we go through all of its children. Each path that leads to a leaf \(\bot\) spells a word: Every word below a node is the symbol of one of its children, followed by one of the words stored below that child. A leaf has only one word below it, the empty word.
For example, to find the words that start with “apa” in the prefix tree shown on Fig. 6.14, we follow the nodes “a”, “p” and “a”. Below that last node, there are two words, “rt” and “rtment”, so the words that start with “apa” are “apart” and “apartment”.
Implementation in Scala
The search itself is a method of our Branch class. It follows the
first character of the prefix, and prepends the symbol of the child to
each word found below it. When the prefix is empty, we collect all the
words stored below that node.
1class Branch(symbol: Char, val children: Map[Char, Node]) extends Node(symbol):
2
3 def prefixSearch(prefix: String): Set[String] =
4 if prefix.isEmpty then
5 words
6 else
7 children.get(prefix.head) match
8 case None => Set()
9 case Some(child) =>
10 child.prefixSearch(prefix.tail).map(rest => child.text + rest)
Collecting the words is done as follows. The text of a branch is its
symbol, whereas the text of a leaf is empty, because \(\bot\)
is not part of any word.
1class Branch(symbol: Char, val children: Map[Char, Node]) extends Node(symbol):
2
3 def text: String = symbol.toString
4
5 def words: Set[String] =
6 val result = for
7 child <- children.values
8 rest <- child.words
9 yield child.text + rest
10 result.toSet
11
12class Leaf extends Node(END):
13
14 def text: String = ""
15
16 def words: Set[String] =
17 Set("")
A leaf can also be reached when the prefix is not empty yet, in which case, no word can start with that prefix:
1class Leaf extends Node(END):
2
3 def prefixSearch(prefix: String): Set[String] =
4 if prefix.isEmpty then Set("") else Set()
The PrefixTree simply delegates to its root:
1class PrefixTree:
2
3 def prefixSearch(prefix: String): Set[String] =
4 root.prefixSearch(prefix)
- How Efficient Is This?
Finding the node that matches the prefix \(p\) takes at most \(|p|\) steps, one per character, as for the membership. Collecting the words requires visiting all the nodes below that node. Each of these nodes belongs to the path of at least one of the words that are returned, so the number of nodes visited is at most the total length of the words found. The search therefore runs in \(O(|p| + m)\), where \(m\) is the total length of the words returned. It does not depend on how many other words are stored in the tree, which is what makes prefix trees efficient for auto-completion.
6.1.5. Insertion#
To add a word \(w=(c_1,\dots,c_k)\) to a prefix tree, we follow the path that spells \(w\), and we create the nodes that are missing.
We start at the root of the tree, which becomes our “current node” \(n\).
If the word \(w\) is empty, we add a leaf \(\bot\) to the children of Node \(n\), unless it already has one. We are done.
Otherwise
If Node \(n\) has no child whose symbol is \(c_1\) (the first character), we create one: A new branch with the symbol \(c_1\) and no children.
We remove the first character from the word \(w\), the child whose symbol is \(c_1\) becomes our current node, and we continue at Step 2.
For example, adding “apes” to a tree that contains only “ape” follows the nodes “a”, “p” and “e”, which already exist. Node “e” has no child “s”, so we create it, and we then add a leaf \(\bot\) below it. The tree now contains both “ape” and “apes”. Adding a word that is already in the tree changes nothing, because all the nodes already exist.
Implementation in Scala
Our nodes are immutable: Instead of modifying a node, add
returns a new node, which is a copy of the previous one with the
updated children. In Scala, children + (symbol -> node) is a copy
of the children map, where symbol now leads to node.
1class Branch(symbol: Char, val children: Map[Char, Node]) extends Node(symbol):
2
3 def add(word: String): Node =
4 if word.isEmpty then
5 Branch(symbol, children + (END -> Leaf()))
6 else
7 val child = children.getOrElse(word.head, Branch(word.head, Map()))
8 Branch(symbol, children + (word.head -> child.add(word.tail)))
The PrefixTree replaces its root with the updated one:
1class PrefixTree:
2
3 def add(word: String): Unit =
4 root = root.add(word)
- How Efficient Is This?
Adding a word \(w\) visits at most one node per character, plus the leaf. If branch nodes are implemented using hashing, each step runs in \(O(1)\), and the insertion runs in \(O(|w|)\). It does not depend on the number of words already in the tree. With a linear search among the children, we would get \(O(|w| \cdot |\mathcal{A}|)\).
6.1.6. Deletion#
To remove a word \(w=(c_1,\dots,c_k)\) from a prefix tree, we follow the path that spells \(w\), remove its leaf \(\bot\), and then we clean up the nodes that are no longer useful.
We start at the root of the tree, which becomes our “current node” \(n\).
If the word \(w\) is empty, we remove the leaf \(\bot\) from the children of Node \(n\) (if there is one).
Otherwise, if Node \(n\) has a child \(c\) whose symbol is \(c_1\) (the first character)
We remove the first character from the word \(w\), and we remove the remaining word from Node \(c\) (we continue at Step 2, with \(c\) as our current node).
Once this is done, if Node \(c\) has no children left, we remove it from the children of Node \(n\).
Else (there is no such child), the word is not in the tree, and nothing changes.
The clean-up of Step 3.b is important, because nodes are shared between words. We can only remove a node if it is not on the path of another word, that is, if it has no children left. For example, if the tree contains “app” and “apple”, removing “app” only removes its leaf \(\bot\): The second “p” still has a child “l”, which is needed by “apple”. By contrast, removing “apple” removes the leaf \(\bot\) and then the nodes “e” and “l”, which have no children left. It stops at the second “p”, which still has a leaf \(\bot\) for “app”.
Implementation in Scala
Again, remove returns a new node, without the word. In Scala,
children - symbol is a copy of the children map, without
symbol. A branch without children is a “dead end”, which the
parent removes (see Line 11).
1class Branch(symbol: Char, val children: Map[Char, Node]) extends Node(symbol):
2
3 def remove(word: String): Node =
4 if word.isEmpty then
5 Branch(symbol, children - END)
6 else
7 children.get(word.head) match
8 case None => this
9 case Some(child) =>
10 val updatedChild = child.remove(word.tail)
11 if updatedChild.isDeadEnd then
12 Branch(symbol, children - word.head)
13 else
14 Branch(symbol, children + (word.head -> updatedChild))
15
16 def isDeadEnd: Boolean =
17 children.isEmpty
A leaf has nothing to remove, and it is never a dead end, because it marks the end of a word:
1class Leaf extends Node(END):
2
3 def remove(word: String): Node =
4 this
5
6 def isDeadEnd: Boolean =
7 false
Finally, the PrefixTree replaces its root with the updated one:
1class PrefixTree:
2
3 def remove(word: String): Unit =
4 root = root.remove(word)
- How Efficient Is This?
As for the insertion, removing a word \(w\) visits at most one node per character, and cleaning up happens along the same path. The deletion runs in \(O(|w|)\), if branch nodes are implemented using hashing.
6.2. Suffix Trees#
A prefix tree is great when we know how a word starts, as in
auto-completion. But a full-text search, like Ctrl-F, is different:
The text we look for may appear anywhere, in the middle of a word, or
across several words. How can a prefix tree help with that?
6.2.1. The Text Index ADT#
Let us first state what we want. We have a text, which we will search many times, for all sorts of fragments (a fragment is simply a short \(Text\), such as a word, or part of a word). For each fragment, we want to know where it occurs in the text. A text index is a structure that answers this question. It has only two operations: One to build the index of a given text, and one to find a fragment in that text.
- text_index.create(t: Text) TextIndex#
Build an index for the given text \(t\).
- Preconditions:
None
- Postconditions:
The resulting index answers questions about the text \(t\).
- text_index.find(i: TextIndex, f: Text) Sequence[Position]#
Return the positions where the given fragment \(f\) occurs in the text of the index \(i\). The first character of the text has position 0. For instance, the fragment “ana” occurs twice in the text “banana”, at positions 1 and 3, so the result is the sequence \((1, 3)\). By contrast, the fragment “x” does not occur, so the result is the empty sequence.
- Preconditions:
None
- Postconditions:
The result contains a position \(p\) if, and only if, the fragment \(f\) starts at position \(p\) in the text.
The positions are sorted in increasing order.
The result is empty if, and only if, the fragment does not occur in the text.
Some details are worth noticing. Occurrences may overlap: In “banana”, the two occurrences of “ana” share the letter “a” at position 3. The empty fragment occurs everywhere, even at the very end of the text, so that, in “banana”, it is found at positions 0 to 6.
How could we implement this? The simplest idea is to do nothing when we create the index, and, every time we are asked to find a fragment, to scan the whole text from the beginning, comparing the fragment with the text at each position. This works, but it is slow if the text is long and we search it many times. A suffix tree does a bit of work upfront, to make every search fast.
6.2.2. Suffixes to the Rescue#
The trick is to notice that any occurrence of a fragment is the beginning of some suffix of the text. Consider the text “banana”. The fragment “nan” occurs in it, and indeed, if we cut away the first two letters (“ba”), what remains, “nana”, starts with “nan”. Whatever the fragment, we can always cut the text just before it, and the fragment is then a prefix of what remains.
A suffix tree is therefore nothing more than a prefix tree that stores all the suffixes of a text. The suffixes of “banana” are:
Once we have inserted these seven words in a prefix tree, finding a fragment in the text is just a prefix search: The fragment occurs in the text if, and only if, there is at least one suffix that starts with it.
But where does it occur? We need to know where each suffix starts in the text. This is easy: A suffix that is \(k\) characters long, in a text of \(n\) characters, starts at position \(n - k\).
Let us look for “ana” in “banana”. Two suffixes start with “ana”: “anana” and “ana”. The text has 6 characters, so “anana” (5 characters) starts at position \(6 - 5 = 1\), and “ana” (3 characters) starts at position \(6 - 3 = 3\). We have found the two occurrences, at positions 1 and 3.
Implementation in Scala
Our SuffixTree simply uses a PrefixTree, which we fill
with all the suffixes of the text, from the whole text down to the
empty one. In Scala, text.drop(start) is what remains of the text
when we skip the first start characters.
1class SuffixTree(text: String):
2
3 private val suffixes = PrefixTree()
4
5 for start <- 0 to text.length do
6 suffixes.add(text.drop(start))
To find a fragment, we search the suffixes that start with it, and we compute where each of them starts in the text, as explained above:
1class SuffixTree(text: String):
2
3 def find(fragment: String): Seq[Int] =
4 val matchingSuffixes = suffixes.prefixSearch(fragment)
5 val positions = matchingSuffixes.map(suffix => text.length - suffix.length)
6 positions.toSeq.sorted
- How Efficient Is This?
Once the tree is built, searching does not depend on how long the text is: We only walk down the tree, following the fragment, as we do in any prefix search. Our code is a bit wasteful, though. When we only want to know whether the fragment occurs, it still collects all the matching suffixes, whereas we could stop as soon as we have followed the whole fragment in the tree. That would take \(O(|f|)\) steps for a fragment \(f\).
Building the tree is the expensive part. A text of \(n\) characters has \(n + 1\) suffixes, which are, on average, \(n/2\) characters long, so the tree stores about \(n^2/2\) symbols, and takes about as long to build. This is fine for a short text, but hopeless for a book, or a genome with billions of characters.
Fortunately, there are better ways. We can compress the tree, as we did for prefix trees, and store positions in the text, instead of copying the suffixes. There are even clever algorithms that build such a compressed suffix tree in time proportional to \(n\) (Ukkonen’s algorithm is the best known). They are beyond the scope of this course, but the idea is the one we have just seen: A suffix tree is a prefix tree of all the suffixes.
