From 4fc4e6e25b943efcd362c85294a2fd4767f1313f Mon Sep 17 00:00:00 2001 From: ashwath462 <79273144+ashwath462@users.noreply.github.com> Date: Sun, 16 Oct 2022 06:02:12 +0530 Subject: [PATCH] chore(CPlusCPlus): add trie algorithms (#1006) --- algorithms/CPlusPlus/README.md | 2 + algorithms/CPlusPlus/Trie/trie_delete.cpp | 44 +++++++++++++++++++ .../Trie/trie_insert_search_startWith.cpp | 44 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 algorithms/CPlusPlus/Trie/trie_delete.cpp create mode 100644 algorithms/CPlusPlus/Trie/trie_insert_search_startWith.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index a5cca706..8b3d72ed 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -157,6 +157,8 @@ ## Trie - [Trie for searching](Trie/trie_search.cpp) +- [Trie for insert search and prefix_search](Trie/trie_insert_search_startWith.cpp) +- [Trie for delete](Trie/trie_delete.cpp) # Maths diff --git a/algorithms/CPlusPlus/Trie/trie_delete.cpp b/algorithms/CPlusPlus/Trie/trie_delete.cpp new file mode 100644 index 00000000..86ea5f9b --- /dev/null +++ b/algorithms/CPlusPlus/Trie/trie_delete.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +typedef long long ll; +#define inf 1e9; +#define inf2 2e18; + + + +struct custom_hash { + static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31);} + size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } +}; + + + +struct TrieNode{ TrieNode* child[26]; bool isEnd; + TrieNode(){ isEnd = false; for(int i = 0; i<26; i++){ child[i] = NULL; } } +}; +struct TrieNode* rootTrie; +void addTrie(string& s){ TrieNode* curr = rootTrie; for(int i = 0; ichild[n] == NULL){curr->child[n] = new TrieNode();} curr = curr->child[n]; } curr->isEnd = true; } +bool searchTrie(string& s){TrieNode* curr = rootTrie;for(int i = 0; ichild[n]) return false;curr = curr->child[n];}return curr->isEnd;} +bool startsWithTrie(string s) {int n = s.length();TrieNode* curr = rootTrie;for(int i =0 ; ichild[k] == NULL) return false;curr = curr->child[k];}return true;} +bool isEmpty(TrieNode* rootTrie){ for (int i = 0; i < ALPHABET_SIZE; i++){if(rootTrie->child[i])return false;}return true;} +void remove(TrieNode* rootTrie, string key, int depth = 0){if (!rootTrie)return NULL;if (depth == key.size()) {if (rootTrie->isEnd)rootTrie->isEnd = false;if (isEmpty(rootTrie)) {delete (rootTrie);rootTrie = NULL;}return rootTrie;}int index = key[depth] - 'a';rootTrie->child[index] = remove(rootTrie->child[index], key, depth + 1);if (isEmpty(rootTrie) && rootTrie->isEnd == false) {delete (rootTrie);rootTrie = NULL;}return rootTrie;} + + +int main(){ +//Jai Shree Ram + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + string keys[] = { "the", "a", "there", "answer", "any", "by", "bye", "their", "hero", "heroplane" }; + int n = sizeof(keys) / sizeof(keys[0]); + + for (int i = 0; i < n; i++) + insert(rootTrie, keys[i]); + + search(rootTrie, "the") ? cout << "Yes\n" : cout << "No\n"; + search(rootTrie, "these") ? cout << "Yes\n" : cout << "No\n"; + + remove(rootTrie, "heroplane"); + search(rootTrie, "hero") ? cout << "Yes\n" : cout << "No\n"; + return 0; +} \ No newline at end of file diff --git a/algorithms/CPlusPlus/Trie/trie_insert_search_startWith.cpp b/algorithms/CPlusPlus/Trie/trie_insert_search_startWith.cpp new file mode 100644 index 00000000..8c73a57c --- /dev/null +++ b/algorithms/CPlusPlus/Trie/trie_insert_search_startWith.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +typedef long long ll; +#define inf 1e9; +#define inf2 2e18; + + + +struct custom_hash { + static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31);} + size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } +}; + + + +struct TrieNode{ TrieNode* child[26]; bool isEnd; + TrieNode(){ isEnd = false; for(int i = 0; i<26; i++){ child[i] = NULL; } } +}; +struct TrieNode* rootTrie; +void addTrie(string& s){ TrieNode* curr = rootTrie; for(int i = 0; ichild[n] == NULL){curr->child[n] = new TrieNode();} curr = curr->child[n]; } curr->isEnd = true; } +bool searchTrie(string& s){TrieNode* curr = rootTrie;for(int i = 0; ichild[n]) return false;curr = curr->child[n];}return curr->isEnd;} +bool startsWithTrie(string s) {int n = s.length();TrieNode* curr = rootTrie;for(int i =0 ; ichild[k] == NULL) return false;curr = curr->child[k];}return true;} + + + +int main(){ +//Jai Shree Ram + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + string keys[] = {"the", "a", "there", "answer", "any", "by", "bye", "their" }; + int n = sizeof(keys)/sizeof(keys[0]); + struct TrieNode *root = getNode(); + + for (int i = 0; i < n; i++) insert(root, keys[i]); + + char output[][32] = {"Not present in trie", "Present in trie"}; + + cout<<"the"<<" --- "<