From 4394d2bfbc868cabd0d8d96f404f0e6715343221 Mon Sep 17 00:00:00 2001 From: AlinGeorgescu <33759291+AlinGeorgescu@users.noreply.github.com> Date: Mon, 1 Oct 2018 13:01:59 +0300 Subject: [PATCH 1/2] Added Trie.cpp --- CPP/data-structures/Trie.cpp | 212 +++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 CPP/data-structures/Trie.cpp diff --git a/CPP/data-structures/Trie.cpp b/CPP/data-structures/Trie.cpp new file mode 100644 index 0000000..9a9b21e --- /dev/null +++ b/CPP/data-structures/Trie.cpp @@ -0,0 +1,212 @@ +#include +#include +#include + +#define ALPHABET_SIZE 26 + +using std::vector; +using std::string; +using std::cout; +using std::endl; + +template +class Trie { + private: + int count, suffix; + vector *> children; + T value; + bool isEndOfWord; + public: + Trie() {} + + Trie(int capacity, T value) + : count(0), + suffix(0), + children(capacity, NULL), + value(value) { } + + Trie(int capacity) + : count(0), + suffix(0), + children(capacity, NULL) { } + + ~Trie() {} + void insert(string key, T value) { + if (!key.size()) { + this->value = value; + isEndOfWord = true; + return; + } + + int x = 0; + if (!search(key, x)) + ++suffix; + + int nextNode = key[0] - 'a'; + if (!children[nextNode]) { + children[nextNode] = new Trie(ALPHABET_SIZE); + ++count; + } + + key = key.erase(0, 1); + children[nextNode]->insert(key, value); + } + bool search(string key, T &val) { + if (!key.size()) { + val = value; + return isEndOfWord; + } + + int nextNode = key[0] - 'a'; + + if (!children[nextNode]) + return false; + + key = key.erase(0, 1); + return children[nextNode]->search(key, val); + } + + bool hasChildren() { + for (int i = 0; i < ALPHABET_SIZE; ++i) { + if (children[i] != NULL) + return true; + } + + return false; + } + + bool remove(string key) { + if (!key.size()) { + isEndOfWord = false; + value = 0; + if (!hasChildren() && !isEndOfWord) + return true; + } + + int nextNode = key[0] - 'a'; + + if (children[nextNode]) { + key = key.erase(0, 1); + --suffix; + + if (children[nextNode]->remove(key)) { + delete children[nextNode]; + --count; + if (!hasChildren() && !isEndOfWord) + return true; + } + } + + return false; + } + + int numWordsWithPrefix(string prefix) { + if (!prefix.size()) { + return suffix; + } + + int nextNode = prefix[0] - 'a'; + + if (!children[nextNode]) + return 0; + + prefix = prefix.erase(0, 1); + return children[nextNode]->numWordsWithPrefix(prefix); + } + +}; + +int main() { + Trie *trie = new Trie(ALPHABET_SIZE); + string s; + int val; + + s = "arta"; + trie -> insert(s, 1); + s = "artilerie"; + trie -> insert(s, 2); + s = "artist"; + trie -> insert(s, 3); + s = "artistic"; + trie -> insert(s, 4); + s = "arma"; + trie -> insert(s, 5); + s = "pusca"; + trie -> insert(s, 6); + + if (!trie -> search(s, val)) { + cout << s << " should be in Trie\n"; + } + cout << "Value of " << s << " : " << val << endl; + s = "artistic"; + if (!trie -> search(s, val)) { + cout << s << " should be in Trie\n"; + } + cout << "Value of " << s << " : " << val << endl; + s = "artist"; + if (!trie -> search(s, val)) { + cout << s << " should be in Trie\n"; + } + cout << "Value of " << s << " : " << val << endl; + s = "artilerie"; + if (!trie -> search(s, val)) { + cout << s << " should be in Trie\n"; + } + cout << "Value of " << s << " : " << val << endl; + s = "arma"; + if (!trie -> search(s, val)) { + cout << s << " should be in Trie\n"; + } + cout << "Value of " << s << " : " << val << endl; + s = "arta"; + if (!trie -> search(s, val)) { + cout << s << " should be in Trie\n"; + } + cout << "Value of " << s << " : " << val << endl; + s = "artis"; + if (trie -> search(s, val)) { + cout << s << " should not be in Trie\n"; + } + s = "pusca"; + trie -> remove(s); + if (trie -> search(s, val)) { + cout << s << " should not be in Trie after removal\n"; + } + s = "artilerie"; + trie -> remove(s); + if (trie -> search(s, val)) { + cout << s << " should not be in Trie after removal\n"; + } + s = "arta"; + if (!trie -> search(s, val)) { + cout << s << " should still be in Trie\n"; + } + s = "artist"; + trie -> remove(s); + if (trie -> search(s, val)) { + cout << s << " should not be in Trie after removal\n"; + } + s = "artistic"; + if (!trie -> search(s, val)) { + cout << s << " should still be in Trie\n"; + } + + s = "artist"; + trie -> insert(s, 3); + s = "artilerie"; + trie -> insert(s, 3); + s = "pusca"; + trie -> insert(s, 3); + s = ""; + cout << trie->numWordsWithPrefix(s) << endl; + s = "a"; + cout << trie->numWordsWithPrefix(s) << endl; + s = "art"; + cout << trie->numWordsWithPrefix(s) << endl; + s = "arti"; + cout << trie->numWordsWithPrefix(s) << endl; + s = "articulatie"; + cout << trie->numWordsWithPrefix(s) << endl; + + return 0; +} From 00e8327c9a8ac07c8d922166bbf007322d8338d1 Mon Sep 17 00:00:00 2001 From: AlinGeorgescu <33759291+AlinGeorgescu@users.noreply.github.com> Date: Mon, 1 Oct 2018 13:03:35 +0300 Subject: [PATCH 2/2] Update CONTRIBUTORS.md --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c6d76e6..97bc2a1 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,3 +1,4 @@ # Add yourself in this file if you are a contributor for Hacktoberfest 2018 - Radu Stochitoiu +- Alin Georgescu