diff --git a/trie/Trie.cpp b/trie/Trie.cpp new file mode 100644 index 00000000..fbc6af00 --- /dev/null +++ b/trie/Trie.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + +using namespace std; + +struct node{ + char data; + bool end; + node* children[26]; + + node(){ + for(int i=0;i<26;i++){ + children[i]=NULL; + } + } + + node(char data){ + this->data = data; + for(int i=0;i<26;i++){ + children[i]=NULL; + } + } + + friend ostream& operator<<(ostream& os, const node& n){ + os< *words){ + if(root->end){ + words->push_back(prefix); + } + + for(char c='a'; c<='z'; c++){ + if(root->children[c-'a']!=NULL){ + dfs(root->children[c-'a'], prefix+c, words); + } + } + } + + public: + Trie(){ + this->root = new node(); + } + + void add(string word){ + node* temp = this->root; + for(char c:word){ + if(temp->children[c-'a']==NULL){ + temp->children[c-'a'] = new node(c); + } + temp = temp->children[c-'a']; + } + temp->end=true; + } + + bool find(string word){ + node* temp = this->root; + for(char c:word){ + if(temp==NULL){ + return false; + } + temp = temp->children[c-'a']; + } + if(temp==NULL){ + return false; + } + return temp->end; + } + + void lookup_node(string word){ + node* temp = this->root; + for(char c:word){ + if(temp==NULL){ + return; + } + temp = temp->children[c-'a']; + } + if(temp==NULL){ + return; + } + cout<<*temp; + } + + vector search(string prefix){ + node* temp = this->root; + vector results; + for(char c:prefix){ + if(temp->children[c-'a']==NULL){ + return results; + } + temp=temp->children[c-'a']; + } + if(temp==NULL){ + return results; + } + dfs(temp, prefix, &results); + return results; + } +}; + +int main(void){ + Trie t; + + t.add("abc"); + t.add("abcdef"); + t.add("def"); + for(auto w:t.search("ab")){ + cout<