Created
August 8, 2017 07:12
-
-
Save hrsvrdhn/1ae71c25ef1c620c022a544d52df8928 to your computer and use it in GitHub Desktop.
Trie implementation in C++
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<iostream> | |
#include<cstdio> | |
#include<string> | |
#include<vector> | |
#include<algorithm> | |
#include<cmath> | |
#include<map> | |
using namespace std; | |
struct TrieNode | |
{ | |
map<char,TrieNode*> children; | |
int prefixes; | |
bool endofword; | |
TrieNode() | |
{ | |
prefixes=0; | |
endofword=false; | |
} | |
}; | |
void insert(TrieNode *root,string word) | |
{ | |
TrieNode *current=root; | |
for(int i=0;i<word.size();i++) | |
{ | |
char ch=word[i]; | |
TrieNode *node=current->children[ch]; | |
if(!node) | |
{ | |
node=new TrieNode(); | |
current->children[word[i]]=node; | |
} | |
current=node; | |
current->prefixes++; | |
} | |
current->endofword=true; | |
} | |
bool search(TrieNode *root,string word) | |
{ | |
TrieNode *current=root; | |
for(int i=0;i<word.size();i++) | |
{ | |
char ch=word[i]; | |
TrieNode *node=current->children[ch]; | |
if(!node) | |
return false; | |
current=node; | |
} | |
return current->endofword; | |
} | |
int count_prefix(TrieNode *root,string word) | |
{ | |
TrieNode *current=root; | |
for(int i=0;i<word.size();i++) | |
{ | |
char ch=word[i]; | |
TrieNode *node=current->children[ch]; | |
if(!node) | |
{ | |
return 0; | |
} | |
current=node; | |
} | |
return current->prefixes; | |
} | |
int main() | |
{ | |
TrieNode *root=new TrieNode(); | |
insert(root,"harshita"); | |
insert(root,"harsh"); | |
insert(root,"sharma"); | |
insert(root,"harry"); | |
string p; | |
cout<<"Enter the prefix :"; | |
cin>>p; | |
cout<<count_prefix(root,p)<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please give the code for DFS/BFS implementation using the map.
thank you in advance.