#include <iostream> #include <string> using namespace std; struct TrieNode { TrieNode* children[26]; bool isEndOfWord; TrieNode() { for (int i = 0; i < 26; i++) children[i] = nullptr; isEndOfWord = false; } }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(string word) { TrieNode* node = root; for (char c : word) { int index = c - 'a'; if (!node->children[index]) node->children[index] = new TrieNode(); node = node->children[index]; } node->isEndOfWord = true; } bool search(string word) { TrieNode* node = root; for (char c : word) { int index = c - 'a'; if (!node->children[index]) return false; node = node->children[index]; } return node->isEndOfWord; } }; int main() { Trie trie; trie.insert("apple"); trie.insert("app"); cout << boolalpha; cout << trie.search("app") << endl; cout << trie.search("apple") << endl; cout << trie.search("ap") << endl; return 0; }
The words "app" and "apple" are inserted. Searching "app" and "apple" returns true because both are inserted as complete words. Searching "ap" returns false because it is a prefix but not a complete word in the Trie.
Each non-null child pointer corresponds to a possible next character that can follow the current prefix represented by the node.
#include <iostream> #include <string> using namespace std; struct TrieNode { TrieNode* children[26]; bool isEndOfWord; TrieNode() { for (int i = 0; i < 26; i++) children[i] = nullptr; isEndOfWord = false; } }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(string word) { TrieNode* node = root; for (char c : word) { int index = c - 'a'; if (!node->children[index]) node->children[index] = new TrieNode(); node = node->children[index]; } node->isEndOfWord = true; } bool search(string word) { TrieNode* node = root; for (char c : word) { int index = c - 'a'; node = node->children[index]; } return node->isEndOfWord; } }; int main() { Trie trie; trie.insert("cat"); cout << boolalpha << trie.search("car") << endl; return 0; }
The search function does not check if the child pointer is null before moving to it. For "car", the 'r' child pointer is null, so accessing it causes a segmentation fault.
All three words are inserted fully, so searching any of them returns true.
#include <iostream> #include <string> using namespace std; struct TrieNode { TrieNode* children[26]; bool isEndOfWord; TrieNode() { for (int i = 0; i < 26; i++) children[i] = nullptr; isEndOfWord = false; } }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(string word) { TrieNode* node = root; for (char c : word) { int index = c - 'a'; if (!node->children[index]) node->children[index] = new TrieNode(); node = node->children[index]; } node->isEndOfWord = true; } bool startsWith(string prefix) { TrieNode* node = root; for (char c : prefix) { int index = c - 'a'; if (!node->children[index]) return false; node = node->children[index]; } return true; } }; int main() { Trie trie; trie.insert("hello"); trie.insert("helium"); cout << boolalpha; cout << trie.startsWith("hel") << endl; cout << trie.startsWith("hey") << endl; cout << trie.startsWith("hello") << endl; cout << trie.startsWith("helloo") << endl; return 0; }
"hel" is a prefix of both "hello" and "helium", so true.
"hey" is not a prefix of any word, so false.
"hello" is a prefix (and a full word), so true.
"helloo" is longer than any inserted word, so false.