From fb93c8ba1360fdba4b0ee8522a26cdf5df5b5aac Mon Sep 17 00:00:00 2001 From: Amit7088 <91862957+Amit7088@users.noreply.github.com> Date: Sat, 9 Oct 2021 09:42:07 +0530 Subject: [PATCH] Create Trie.cpp --- DSA/DSA-CPP/Trie/Trie.cpp | 67 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 DSA/DSA-CPP/Trie/Trie.cpp diff --git a/DSA/DSA-CPP/Trie/Trie.cpp b/DSA/DSA-CPP/Trie/Trie.cpp new file mode 100644 index 00000000..23ea24e0 --- /dev/null +++ b/DSA/DSA-CPP/Trie/Trie.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +struct trie{ + struct trie* child[26]; + bool is_end; + trie(){ + memset(child,0,sizeof(child)); + is_end=false; + } +}; + +struct trie* root; +//inserts a word to the trie +void insert(string s){ + struct trie* temp=root; + //traverses over each character + //if the character already exists then it simply iterates over + //otherwise creates a new node and inserts the character + for(char c: s){ + if(!temp->child[c-'a']) + temp->child[c-'a']=new trie; + temp=temp->child[c-'a']; + } + //sets the last letter's boolean value to true + temp->is_end=true; +} +//returns true if the word exists, false otherwise +bool check(string s){ + struct trie* temp=root; + //iterates over the character of the word + for(char c: s){ + //if at any point the char of the word being check is not found it return false + if(!temp->child[c-'a']) + return false; + temp=temp->child[c-'a']; + } + //returns the last letters boolean value + return temp->is_end; +} + +int main(){ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + root=new trie; + int n; + cout << "Input the number of words in the List" << endl; + cin >> n; + string word; + cout<<"Enter the words"<> word ; + insert(word); + } + cout << "Enter the number of words you want to check exist in the List" << endl; + int m; + cin >> m; + //the words to be checked + for(int i=0; i> word ; + if(check(word)) + cout<< "This word exist in the list" <