Untitled
unknown
plain_text
3 years ago
1.2 kB
9
Indexable
#include <iostream>
#include <unordered_map>
using namespace std;
class Node {
public:
char data; // storing the data
bool isWordEnding; // checking the terminal state of the word
unordered_map<char, Node*> m;
Node(char d){
data = d;
isWordEnding = false;
}
};
class Trie {
Node * root;
public:
Trie() {
root = new Node('\0');
}
// search
bool search(string word){
Node * temp = root;
for(char ch: word){
if(temp->m.count(ch) == 0){
return false;
}
temp = temp->m[ch];
}
if (temp->isWordEnding == true) return true;
else return false;
}
// insert
void insert(string word){
Node * temp = root;
for(char ch: word){
// if root is already pointing to the character
// the character is not yet inserted
// i have not inserted any h character
if(temp->m.count(ch) == 0) {
// create a new node
Node * n = new Node(ch); /// i am creating a e node
temp->m[ch] = n; // i am linking the character with e node
}
temp = temp->m[ch]; // i am at e
}
temp->isWordEnding = true;
}
};
int main() {
// your code goes here
cout << "hello" << endl;
return 0;
}Editor is loading...