-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement_trie_prefix_tree.rs
61 lines (54 loc) · 1.61 KB
/
implement_trie_prefix_tree.rs
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
#![allow(dead_code)]
pub struct Trie {
pub is_end: bool,
pub children: [Option<Box<Trie>>; 26],
}
impl Trie {
pub fn new() -> Self {
Trie {
is_end: false,
children: Default::default(),
}
}
pub fn insert(&mut self, word: String) {
let mut node = self;
for ch in word.chars().map(|ch| (ch as u8 - 'a' as u8) as usize) {
node = node.children[ch].get_or_insert(Box::new(Trie::new()));
}
node.is_end = true;
}
pub fn search(&self, word: String) -> bool {
let mut node = self;
for ch in word.chars().map(|ch| (ch as u8 - 'a' as u8) as usize) {
match node.children[ch].as_ref() {
Some(next_node) => node = next_node,
None => return false,
}
}
node.is_end
}
pub fn starts_with(&self, prefix: String) -> bool {
let mut node = self;
for ch in prefix.chars().map(|ch| (ch as u8 - 'a' as u8) as usize) {
match node.children[ch].as_ref() {
Some(next_node) => node = next_node,
None => return false,
}
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trie() {
let mut trie = Trie::new();
trie.insert("apple".to_string());
assert_eq!(trie.search("apple".to_string()), true);
assert_eq!(trie.search("app".to_string()), false);
assert_eq!(trie.starts_with("app".to_string()), true);
trie.insert("app".to_string());
assert_eq!(trie.search("app".to_string()), true);
}
}