-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
176 lines (142 loc) · 4.03 KB
/
main.rs
File metadata and controls
176 lines (142 loc) · 4.03 KB
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
mod exercises;
use std::collections::HashMap;
fn main() {
println!("\n*** Chapter 17 ***\n");
let mut trie = Trie::new();
trie.insert("ace");
trie.insert("act");
trie.insert("bad");
trie.insert("bake");
trie.insert("bat");
trie.insert("batter");
trie.insert("cab");
trie.insert("cat");
trie.insert("catnap");
trie.insert("catnip");
dbg!(&trie);
dbg!(trie.search("bat"));
dbg!(trie.search("batman"));
dbg!(trie.collect_words());
dbg!(trie.autocomplete("cat"));
//// Exercises
println!("\n*** Exercises ***\n");
trie.traverse();
println!("autocorrect(): {:?}", trie.autocorrect("catnar"));
println!("autocorrect(): {:?}", trie.autocorrect("acc"));
}
#[derive(Debug)]
struct Trie {
root: TrieNode,
}
#[derive(Debug)]
struct TrieNode {
children: HashMap<char, TrieNode>,
}
impl Trie {
fn new() -> Self {
Self {
root: TrieNode::new(),
}
}
fn insert(&mut self, word: &str) {
let mut current = &mut self.root;
for ch in word.chars() {
current = current.children.entry(ch).or_insert(TrieNode::new());
}
current.children.insert('*', TrieNode::new());
}
fn search(&self, word: &str) -> Option<&TrieNode> {
let mut current = &self.root;
for ch in word.chars() {
if let Some(child) = current.children.get(&ch) {
current = child;
} else {
return None;
}
}
Some(current)
}
fn collect_words(&self) -> Vec<String> {
let mut words = Vec::new();
self.root.collect_words("", &mut words);
words
}
fn autocomplete(&self, prefix: &str) -> Vec<String> {
let mut words = Vec::new();
if let Some(node) = self.search(prefix) {
node.collect_words("", &mut words);
}
words
}
}
impl TrieNode {
fn new() -> Self {
Self {
children: HashMap::new(),
}
}
fn collect_words(&self, word: &str, words: &mut Vec<String>) {
for (key, child) in self.children.iter() {
if *key == '*' {
words.push(word.to_string());
} else {
let mut new_word = word.to_string();
new_word.push(*key);
child.collect_words(&new_word, words);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_insert() {
let mut trie = Trie::new();
assert!(trie.root.children.is_empty());
trie.insert("bat");
assert_eq!(trie.root.children.len(), 1);
trie.insert("batter");
assert_eq!(trie.root.children.len(), 1);
trie.insert("ace");
assert_eq!(trie.root.children.len(), 2);
}
#[test]
fn test_search() {
let mut trie = Trie::new();
assert!(trie.search("cat").is_none());
trie.insert("cat");
assert!(trie.search("cat").is_some());
assert!(trie.search("bat").is_none());
trie.insert("batter");
assert!(trie.search("bat").is_some());
assert!(trie.search("batter").is_some());
}
#[test]
fn test_collect_words() {
let mut trie = Trie::new();
let words = ["bake", "bat", "batter"];
for word in words.iter() {
trie.insert(word);
}
let collected = trie.collect_words();
assert_eq!(collected.len(), words.len());
for word in words.iter() {
assert!(collected.contains(&word.to_string()));
}
}
#[test]
fn test_autocomplete() {
let mut trie = Trie::new();
let words = ["cat", "cater", "bake", "bat", "batter"];
for word in words.iter() {
trie.insert(word);
}
let completions = trie.autocomplete("bat");
let expected = vec!["", "ter"];
assert_eq!(completions.len(), expected.len());
for word in expected.iter() {
assert!(completions.contains(&word.to_string()));
}
}
}