| 24 | } |
| 25 | |
| 26 | void insert(std::string word) { |
| 27 | TrieNode* node = root; |
| 28 | for (char c : word) { |
| 29 | // For each character in the word, if it's not a child of |
| 30 | // the current node, create a new TrieNode for that |
| 31 | // character. |
| 32 | if (node->children.find(c) == node->children.end()) { |
| 33 | node->children[c] = new TrieNode(); |
| 34 | } |
| 35 | node = node->children[c]; |
| 36 | } |
| 37 | // Mark the last node as the end of a word. |
| 38 | node->isWord = true; |
| 39 | } |
| 40 | |
| 41 | bool search(std::string word) { |
| 42 | TrieNode* node = root; |