Skip to content

Instantly share code, notes, and snippets.

@Se7soz
Created May 30, 2020 01:44
Show Gist options
  • Save Se7soz/44ee93f4aae87eb8b087dc089ca54b66 to your computer and use it in GitHub Desktop.
Save Se7soz/44ee93f4aae87eb8b087dc089ca54b66 to your computer and use it in GitHub Desktop.
struct TrieNode {
public:
void insert(const string& word, int i) {
if (i >= word.size()) {
return;
}
if (children[word[i]] == NULL) {
children[word[i]] = new TrieNode();
}
children[word[i]]->insert(word, i+1);
if (i == word.size()-1) {
children[word[i]]->endOfWord = true;
}
}
TrieNode* search(const string& word, int i) {
if (i == word.size() || children[word[i]] == NULL) {
return NULL;
}
if (i == word.size()-1) {
return children[word[i]];
}
return children[word[i]]->search(word, i+1);
}
bool endOfWord;
TrieNode* children[512];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment