The wavelet tree is a succinct data structure used to store strings in compressed space. It generalizes the rank and select operations originally defined on bitvectors to arbitrary alphabets. The data structure was introduced in 2003 by Grossi, Gupta, and Vitter in the context of representing compressed suffix arrays, and has since found application in numerous areas including full-text indexing, data compression, string processing, and computational geometry.
Overview
A wavelet tree is built by recursively partitioning the alphabet into pairs of subsets. At each node of the tree, a bitvector stores, for each position of the input string, whether the symbol at that position belongs to one subset or the other. The leaves of the tree correspond to individual symbols of the alphabet. The name derives from an analogy with the wavelet transform for signals, which recursively decomposes a signal into low-frequency and high-frequency components.
Properties
Let Σ be a finite alphabet with σ = |Σ|. By using succinct dictionaries at each node, a string s ∈ Σ* can be stored in |s|H₀(s) + o(|s| log σ) bits, where H₀(s) is the order-0 empirical entropy of s. If the tree is balanced, the operations access, rank_q, and select_q can each be supported in O(log σ) time.
Access
Given a position i in the string, the access operation retrieves the character at that position. Starting from the root, the algorithm checks the bitvector at position i: if the bit is 0, the character lies in the left child's alphabet subset, and the algorithm recurses to the left child with an updated position; if the bit is 1, it recurses to the right child. Since the rank operation on a bitvector can be computed in O(1) using succinct dictionaries, access requires O(log σ) time on a balanced tree.
Rank
The rank_q(i) operation counts the number of occurrences of character q in the prefix up to position i. The algorithm navigates the tree based on which alphabet partition q belongs to, using rank operations on the bitvectors to determine the corresponding position in the child node.
Select
The select_q(j) operation finds the position of the j-th occurrence of character q. The algorithm starts at the leaf corresponding to q and traverses upward, using select operations on the bitvectors to compute the position at each parent node.
Extensions
Several extensions to the wavelet tree have been developed:
- Multiary nodes: Using multiary nodes instead of binary nodes reduces the height of the tree.
- Dynamic wavelet trees: The structure can be made dynamic, supporting insertions and deletions at arbitrary positions, enabling the implementation of dynamic FM-indexes.
- Wavelet Trie: A generalization that uses the trie structure on an alphabet of strings to allow dynamic tree modifications and changes to the underlying alphabet.
- Wavelet Matrix: A variant that arranges the bitvectors in a matrix format, which can be advantageous for certain applications.
Applications
Wavelet trees have been applied in compressed full-text indexes (such as the FM-index), document retrieval, computational geometry (representing grids of points), and as a building block for other succinct data structures.