单词插入
(&mut self, word: &str)
| 18 | |
| 19 | // 单词插入 |
| 20 | fn insert(&mut self, word: &str) { |
| 21 | let mut node = &mut self.root; |
| 22 | // 逐个字符插入 |
| 23 | for c in word.as_bytes() { |
| 24 | let index = (c - b'a') as usize; |
| 25 | let next = &mut node.children[index]; |
| 26 | node = next.get_or_insert_with(Box::<Node>::default); |
| 27 | } |
| 28 | node.end = true; |
| 29 | } |
| 30 | |
| 31 | fn search(&self, word: &str) -> bool { |
| 32 | self.word_node(word).map_or(false, |n| n.end) |