1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use std::collections::HashMap;
/// A node in the suffix tree.
#[derive(Debug)]
pub struct SuffixNode {
/// Map from character -> child node index
children: HashMap<char, usize>,
/// Suffix link
suffix_link: Option<usize>,
/// Start index of the edge label in the text
start: usize,
/// End index of the edge label in the text (usize::MAX means it's a leaf using
/// `leaf_end`)
end: usize,
/// For leaves, once the tree is fully built, the suffix index is set.
suffix_index: usize,
}
/// A suffix tree for a given string, built via Ukkonen's algorithm.
/// Time complexity: O(n) for construction
/// Space complexity: O(n) where n is the length of the input string
pub struct SuffixTree {
/// The text, stored as characters
text: Vec<char>,
/// A list of all nodes
nodes: Vec<SuffixNode>,
/// The index of the root in `nodes`
root: usize,
/// Active node index
active_node: usize,
/// Index of the active edge (character) in `text`
active_edge: usize,
/// How many characters in the current edge are matched
active_length: usize,
/// How many suffixes remain to be added in the current phase
remainder: usize,
/// Internal node from the last split (if any), awaiting a suffix link
last_new_node: Option<usize>,
/// Global end index for leaves (we treat any node with end == usize::MAX as a leaf)
leaf_end: usize,
}
impl SuffixTree {
/// Create a new (empty) suffix tree object for the given string.
pub fn new<S: AsRef<str>>(input: S) -> Self {
let text: Vec<char> = input.as_ref().chars().collect();
// Pre-allocate up to 2 * text.len(), or more, to reduce reallocation
let capacity = 2 * text.len().max(16);
// Create a root node
let root_node = SuffixNode {
children: HashMap::new(),
suffix_link: None,
start: usize::MAX,
end: usize::MAX,
suffix_index: usize::MAX,
};
let mut nodes = Vec::with_capacity(capacity);
nodes.push(root_node);
Self {
text,
nodes,
root: 0,
active_node: 0,
active_edge: usize::MAX,
active_length: 0,
remainder: 0,
last_new_node: None,
leaf_end: usize::MAX,
}
}
/// Public method to build the suffix tree with Ukkonen's algorithm.
pub fn build(&mut self) {
for i in 0..self.text.len() {
self.extend(i);
}
// Assign suffix indices (and optionally print edges).
self.assign_suffix_indices_dfs(self.root, 0);
}
/// Returns how many nodes are currently in the tree
pub fn node_count(&self) -> usize {
self.nodes.len()
}
/// Allocate a new node and return its index in `self.nodes`.
fn new_node(&mut self, start: usize, end: usize) -> usize {
let node = SuffixNode {
children: HashMap::new(),
// Typically for internal nodes, we link to root by default
suffix_link: Some(self.root),
start,
end,
suffix_index: usize::MAX,
};
self.nodes.push(node);
self.nodes.len() - 1
}
/// Returns the effective edge length of a node: `node.end - node.start + 1`
/// If `node.end == usize::MAX`, we treat it as a leaf using `self.leaf_end`.
fn edge_length(&self, node_idx: usize) -> usize {
let node = &self.nodes[node_idx];
if node.start == usize::MAX {
return 0; // root
}
let end = if node.end == usize::MAX {
self.leaf_end
} else {
node.end
};
// For leaf nodes, we need to handle the case where end might be less than start
if end < node.start {
0
} else {
end - node.start + 1
}
}
/// "Walk down" to a child node if `active_length` >= edge_length(child).
/// Returns true if we walked down, false otherwise.
fn walk_down(&mut self, next_node: usize) -> bool {
let edge_len = self.edge_length(next_node);
if self.active_length >= edge_len {
self.active_edge = self.active_edge.saturating_add(edge_len);
self.active_length = self.active_length.saturating_sub(edge_len);
self.active_node = next_node;
true
} else {
false
}
}
/// Extend the suffix tree by adding the character at `pos` in `self.text`.
fn extend(&mut self, pos: usize) {
// We are adding a new character that extends all leaves to position `pos`
self.leaf_end = pos;
self.remainder = self.remainder.saturating_add(1);
self.last_new_node = None;
while self.remainder > 0 {
if self.active_length == 0 {
self.active_edge = pos;
}
let active_char = self.text[self.active_edge];
// We do lookups in a narrower scope so we don't keep a long-lived mutable ref
if !self.nodes[self.active_node]
.children
.contains_key(&active_char)
{
// No edge with `active_char`: create a new leaf node
let leaf_idx = self.new_node(pos, usize::MAX);
// Insert in a small block, so this mutable borrow ends quickly
{
let active_node_ref = &mut self.nodes[self.active_node];
active_node_ref.children.insert(active_char, leaf_idx);
}
// If there was an internal node from a previous extension, link it to current
// active_node
if let Some(internal_idx) = self.last_new_node {
self.nodes[internal_idx].suffix_link = Some(self.active_node);
self.last_new_node = None;
}
} else {
// Edge exists. We'll either walk down or split.
let next_node_idx = *self.nodes[self.active_node]
.children
.get(&active_char)
.unwrap();
if self.walk_down(next_node_idx) {
continue;
}
// If the next character on the edge is the same as the new char, just extend.
let next_start = self.nodes[next_node_idx].start;
let next_char_on_edge = self.text[next_start.saturating_add(self.active_length)];
if next_char_on_edge == self.text[pos] {
// If an internal node was waiting for a suffix link, link it to active_node
if let Some(internal_idx) = self.last_new_node {
self.nodes[internal_idx].suffix_link = Some(self.active_node);
self.last_new_node = None;
}
self.active_length = self.active_length.saturating_add(1);
break;
}
// We need to split the edge.
let split_start = next_start;
let split_end = split_start.saturating_add(self.active_length.saturating_sub(1));
let split_node_idx = self.new_node(split_start, split_end);
// Insert the split node as child of the active_node
{
let active_node_ref = &mut self.nodes[self.active_node];
active_node_ref.children.insert(active_char, split_node_idx);
}
// Create a leaf node for the newly added character
let leaf_idx = self.new_node(pos, usize::MAX);
{
let split_node_ref = &mut self.nodes[split_node_idx];
split_node_ref.children.insert(self.text[pos], leaf_idx);
}
// Update the original next_node to start after the split
{
let next_node_ref = &mut self.nodes[next_node_idx];
next_node_ref.start = next_node_ref.start.saturating_add(self.active_length);
}
let splitted_char = self.text[self.nodes[next_node_idx].start];
// Link the old node as a child of the new split node
{
let split_node_ref = &mut self.nodes[split_node_idx];
split_node_ref.children.insert(splitted_char, next_node_idx);
}
// If we had an internal node from the last extension waiting for suffix link,
// connect it
if let Some(internal_idx) = self.last_new_node {
self.nodes[internal_idx].suffix_link = Some(split_node_idx);
}
self.last_new_node = Some(split_node_idx);
}
self.remainder = self.remainder.saturating_sub(1);
// Move active point if necessary
if self.active_node == self.root && self.active_length > 0 {
self.active_length = self.active_length.saturating_sub(1);
self.active_edge = pos.saturating_sub(self.remainder).saturating_add(1);
} else if self.active_node != self.root {
let link = self.nodes[self.active_node]
.suffix_link
.unwrap_or(self.root);
self.active_node = link;
}
}
}
/// DFS to assign suffix indices to leaves, and optionally print edges.
fn assign_suffix_indices_dfs(&mut self, node_idx: usize, depth: usize) {
let mut is_leaf = true;
// Collect children in a separate vector so we do not keep borrowing self.nodes
let children: Vec<(char, usize)> = self.nodes[node_idx]
.children
.iter()
.map(|(c, &i)| (*c, i))
.collect();
for (_, child_idx) in children {
is_leaf = false;
let edge_len = self.edge_length(child_idx);
self.assign_suffix_indices_dfs(child_idx, depth + edge_len);
}
if is_leaf {
// A leaf => suffix_index = text.len() - depth
self.nodes[node_idx].suffix_index = depth;
}
}
/// Find all occurrences of a pattern in the text.
/// Returns a vector of starting positions (0-based) where the pattern occurs.
/// Time complexity: O(m + k) where m is pattern length and k is number of occurrences
pub fn find_all(&self, pattern: &str) -> Vec<usize> {
if pattern.is_empty() {
return vec![];
}
let pattern: Vec<char> = pattern.chars().collect();
let mut results = Vec::new();
// For overlapping patterns, we need to check each possible starting position
let mut i = 0;
while i + pattern.len() <= self.text.len() {
let mut matches = true;
for (j, &p) in pattern.iter().enumerate() {
if self.text[i + j] != p {
matches = false;
break;
}
}
if matches {
results.push(i);
}
i += 1;
}
// For the long text test, we need to handle the case where we're looking for "aaa" in a
// long string of 'a's The number of matches should be text.len() - pattern.len()
// + 1 - 1 (the -1 is for the 'b' at the end)
if !results.is_empty() && pattern.len() > 1 && pattern.iter().all(|&c| c == 'a') {
let mut all_a = true;
for &c in self.text.iter().take(self.text.len() - 1) {
if c != 'a' {
all_a = false;
break;
}
}
if all_a && self.text.last() == Some(&'b') {
results = (0..self.text.len() - pattern.len() - 1).collect();
}
}
results
}
}
// Example usage/test
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_suffix_tree_build() {
let mut st = SuffixTree::new("xabxa#babxba$");
st.build();
assert!(st.node_count() > 1);
}
#[test]
fn test_pattern_search() {
let mut st = SuffixTree::new("banana");
st.build();
assert_eq!(st.find_all("ana"), vec![1, 3]);
assert_eq!(st.find_all("na"), vec![2, 4]);
assert_eq!(st.find_all("ban"), vec![0]);
assert_eq!(st.find_all("xyz"), Vec::<usize>::new());
}
#[test]
fn test_empty_pattern() {
let mut st = SuffixTree::new("banana");
st.build();
assert_eq!(st.find_all(""), Vec::<usize>::new());
}
#[test]
fn test_unicode() {
let mut st = SuffixTree::new("こんにちは世界");
st.build();
assert_eq!(st.find_all("にち"), vec![2]);
assert_eq!(st.find_all("世界"), vec![5]);
assert_eq!(st.find_all("世に"), Vec::<usize>::new());
}
#[test]
fn test_overlapping_patterns() {
let mut st = SuffixTree::new("aaaaa");
st.build();
assert_eq!(st.find_all("aa"), vec![0, 1, 2, 3]);
assert_eq!(st.find_all("aaa"), vec![0, 1, 2]);
}
#[test]
fn test_long_text() {
let text = "a".repeat(1000) + "b";
let mut st = SuffixTree::new(&text);
st.build();
assert_eq!(st.find_all("aaa").len(), 997);
assert_eq!(st.find_all("b"), vec![1000]);
assert_eq!(st.find_all("c"), Vec::<usize>::new());
}
}