From cbef5275bb5ac2b34d4ec0ed986cb1129486e85a Mon Sep 17 00:00:00 2001 From: shubhamguptaji Date: Thu, 11 Oct 2018 09:50:16 +0530 Subject: [PATCH 1/2] Trie Tree --- Datastructures/TrieTree.cpp | 93 +++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Datastructures/TrieTree.cpp diff --git a/Datastructures/TrieTree.cpp b/Datastructures/TrieTree.cpp new file mode 100644 index 000000000..abf0da08b --- /dev/null +++ b/Datastructures/TrieTree.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +using namespace std; + +typedef struct trie +{ + struct trie *arr[26]; + bool isEndofWord; +} trie; + +trie *createNode() +{ + trie *nn = new trie(); + for (int i = 0; i < 26; i++) + nn->arr[i] = NULL; + nn->isEndofWord = false; + return nn; +} + +void insert(trie *root, char* str) +{ + for (int i = 0; i < strlen(str); i++) + { + int j = str[i] - 'a'; + if (root->arr[j]) + { + root = root->arr[j]; + } + else + { + root->arr[j] = createNode(); + root = root->arr[j]; + } + } + root->isEndofWord = true; +} + +bool search(trie *root, char* str, int index) +{ + if (index == strlen(str)) + { + if (!root->isEndofWord) + return false; + return true; + } + int j = str[index] - 'a'; + if (!root->arr[j]) + return false; + return search(root->arr[j], str, index + 1); +} + +bool deleteString (trie *root, char* str, int index) +{ + if (index == strlen(str)) + { + if (!root->isEndofWord) + return false; + root->isEndofWord = false; + for (int i = 0; i < 26; i++) + return false; + return true; + } + int j = str[index] - 'a'; + if (!root->arr[j]) + return false; + bool var = deleteString (root, str, index + 1); + if (var) + { + root->arr[j] = NULL; + if (root->isEndofWord) + return false; + else + { + int i; + for (i = 0; i < 26; i++) + if (root->arr[i]) + return false; + return true; + } + } +} + +int main() +{ + trie *root = createNode(); + insert(root, "hello"); + insert(root, "world"); + int a = search(root, "hello", 0); + int b = search(root, "word", 0); + printf("%d %d ", a, b); + return 0; +} \ No newline at end of file From 975cefb62b25d492f45ec217c116649a24b5515f Mon Sep 17 00:00:00 2001 From: shubhamguptaji Date: Thu, 11 Oct 2018 09:57:49 +0530 Subject: [PATCH 2/2] comments added --- Datastructures/TrieTree.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Datastructures/TrieTree.cpp b/Datastructures/TrieTree.cpp index abf0da08b..866087d87 100644 --- a/Datastructures/TrieTree.cpp +++ b/Datastructures/TrieTree.cpp @@ -2,13 +2,13 @@ #include #include using namespace std; - +// structure definition typedef struct trie { struct trie *arr[26]; bool isEndofWord; } trie; - +// create a new node for trie trie *createNode() { trie *nn = new trie(); @@ -18,6 +18,7 @@ trie *createNode() return nn; } +// insert string into the trie void insert(trie *root, char* str) { for (int i = 0; i < strlen(str); i++) @@ -36,6 +37,7 @@ void insert(trie *root, char* str) root->isEndofWord = true; } +// search a string exists inside the trie bool search(trie *root, char* str, int index) { if (index == strlen(str)) @@ -49,7 +51,9 @@ bool search(trie *root, char* str, int index) return false; return search(root->arr[j], str, index + 1); } - +/* removes the string if it is not a prefix of any other + string, if it is then just sets the endofword to false, else + removes the given string*/ bool deleteString (trie *root, char* str, int index) { if (index == strlen(str))