Created
May 21, 2021 09:04
-
-
Save sauravgpt/391353f78f66df5f158c090ffef40222 to your computer and use it in GitHub Desktop.
Trie Implementation from Scratch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
// Considering only small alphabets | |
class TrieNode { | |
public: | |
TrieNode* children[26]; | |
bool endOfWord; | |
int count; | |
TrieNode() { | |
count = 0; | |
endOfWord = false; | |
for (int i = 0; i < 26; i++) | |
children[i] = nullptr; | |
} | |
}; | |
void insert(TrieNode* root, string& word) { | |
for (char c : word) { | |
int index = c - 'a'; | |
if (root->children[index] == nullptr) { | |
root->children[index] = new TrieNode(); | |
} | |
root->count += 1; | |
root = root->children[index]; | |
} | |
root->endOfWord = true; | |
} | |
bool check(TrieNode* root, string& word) { | |
for (char c : word) { | |
int index = c - 'a'; | |
if (root->children[index] == nullptr) return false; | |
root = root->children[index]; | |
} | |
return root->endOfWord == true; | |
} | |
int countPrefix(TrieNode* root, string& prefix) { | |
for (char c : prefix) { | |
int index = c - 'a'; | |
if (root->children[index] == nullptr) return 0; | |
root = root->children[index]; | |
} | |
return root->count; | |
} | |
int main() { | |
vector<string> words = { "apple", "banana", "ban", "ball" }; | |
TrieNode* root = new TrieNode(); | |
for (auto& word : words) insert(root, word); | |
while (1) { | |
string word; cin >> word; | |
cout << countPrefix(root, word) << endl; | |
} | |
// ba -> banana, ball, ban -> 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment