From cb60762c391e5038fb31067f82d79145f8c61c79 Mon Sep 17 00:00:00 2001 From: Ellika Mishra <55554508+ellikamishra@users.noreply.github.com> Date: Thu, 21 Oct 2021 18:15:10 +0530 Subject: [PATCH] chore(CPlusPlus): trie datastructure for searching added (#578) --- algorithms/CPlusPlus/README.md | 4 + algorithms/CPlusPlus/Trie/trie_search.cpp | 118 ++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 algorithms/CPlusPlus/Trie/trie_search.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 3ef1eabc..9289df6a 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -126,6 +126,10 @@ - [Binary Tree Implementation](Trees/binary-tree-implementation.cpp) - [Iterative Segment Tree](Trees/IterativeSegmentTree.cpp) +## Trie + +- [Trie for searching](Trie/trie_search.cpp) + # Maths - [Kaprekar Number](Maths/Kaprekar-number.cpp) diff --git a/algorithms/CPlusPlus/Trie/trie_search.cpp b/algorithms/CPlusPlus/Trie/trie_search.cpp new file mode 100644 index 00000000..064ce2d0 --- /dev/null +++ b/algorithms/CPlusPlus/Trie/trie_search.cpp @@ -0,0 +1,118 @@ + + +/* +Trie is ds implementation of tree with one root and further 26 children at each layer and specifying if +it is end of tree. Better then hashmap in cases where we need to find words with same prefix,collisions +etc and used for word search spell check. + +*/ + +#include + +using namespace std; + +struct TrieNode *root; +struct TrieNode +{ + + struct TrieNode *children[26]; + + bool isEnd; +}; + +struct TrieNode *getNode() +{ + + struct TrieNode *pNode = new TrieNode; + pNode->isEnd = false; + for (int i = 0; i < 26; i++) + { + + pNode->children[i] = NULL; + } + + return pNode; +} +class Trie +{ +public: + /** Initialize your data structure here. */ + TrieNode *root; + Trie() + { + + root = new TrieNode(); + } + + /** Inserts a word into the trie. */ + void insert(string word) + { + + struct TrieNode *pcrawl = root; + for (int i = 0; i < word.length(); i++) + { + + if (!pcrawl->children[word[i] - 'a']) + pcrawl->children[word[i] - 'a'] = getNode(); + + pcrawl = pcrawl->children[word[i] - 'a']; + } + + pcrawl->isEnd = true; + } + + /** Returns if the word is in the trie. */ + bool search(string word) + { + struct TrieNode *pcrawl = root; + for (int i = 0; i < word.length(); i++) + { + + if (!pcrawl->children[word[i] - 'a']) + return false; + + pcrawl = pcrawl->children[word[i] - 'a']; + } + + return pcrawl->isEnd; + } +}; + +int main() +{ + + string keys[] = {"the", "a", "there", + "answer", "any", "by", + "bye", "their", "where", "when"}; + int n = sizeof(keys) / sizeof(keys[0]); + + Trie t; + t.root = getNode(); + // Construct trie + for (int i = 0; i < n; i++) + { + t.insert(keys[i]); + } + + // Search for different keys + cout << "'them' present in keys:"; + t.search("them") ? cout << "Yes\n" : cout << "No\n"; + cout << "'when' present in keys:"; + t.search("when") ? cout << "Yes\n" : cout << "No\n"; + return 0; +} + +/* + +Time Complexity- +O(key_length) +Space Complexity- +O(ALPHABET_SIZE * key_length * N), N is no. of keys in trie + +SAMPLE INPUT-OUTPUT + +'them' present in keys:No +'when' present in keys:Yes + + +*/ \ No newline at end of file