Inserts a word into the trie
(word string)
| 19 | |
| 20 | // Inserts a word into the trie |
| 21 | func (this *Trie) Insert(word string) { |
| 22 | curr := this.root |
| 23 | for i := 0; i < len(word); i++ { |
| 24 | slot := word[i] - 'a' |
| 25 | |
| 26 | // If the child doesn't exist, create it |
| 27 | if curr.children[slot] == nil { |
| 28 | curr.children[slot] = &trieNode{ |
| 29 | char: word[i], |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Advance curr to the slot |
| 34 | curr = curr.children[slot] |
| 35 | } |
| 36 | |
| 37 | // Mark the last node as the end of an inserted word |
| 38 | curr.end = true |
| 39 | } |
| 40 | |
| 41 | // Returns true if the word is in the trie |
| 42 | func (this *Trie) Search(word string) bool { |