summaryrefslogtreecommitdiff
path: root/string/trie.cpp
diff options
context:
space:
mode:
authorpjungeblut <paul.jungeblut@gmail.com>2014-11-23 23:01:04 +0100
committerpjungeblut <paul.jungeblut@gmail.com>2014-11-23 23:01:04 +0100
commit3bf9e44bf552ef5ceef2a4eef87907cc1a8db09b (patch)
tree0348c99d32361c5787a41740c0d1f5156a5bd031 /string/trie.cpp
parent4cc304e57566d149582d974cdaf4a7f724c6b5c1 (diff)
parent213662f659ed8b0a95da110ae6eb5e91e2ecae71 (diff)
gebaut
Diffstat (limited to 'string/trie.cpp')
-rw-r--r--string/trie.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/string/trie.cpp b/string/trie.cpp
new file mode 100644
index 0000000..f4b979c
--- /dev/null
+++ b/string/trie.cpp
@@ -0,0 +1,25 @@
+//nur fuer kleinbuchstaben!
+struct node {
+ node *(e)[26];
+ int c = 0;//anzahl der woerter die an dem node enden.
+ node() { for(int i = 0; i < 26; i++) e[i] = NULL; }
+};
+
+void insert(node *root, string *txt, int s) {
+ if(s >= txt->length()) root->c++;
+ else {
+ int idx = (int)((*txt).at(s) - 'a');
+ if(root->e[idx] == NULL) {
+ root->e[idx] = new node();
+ }
+ insert(root->e[idx], txt, s+1);
+ }
+}
+
+int contains(node *root, string *txt, int s) {
+ if(s >= txt->length()) return root->c;
+ int idx = (int)((*txt).at(s) - 'a');
+ if(root->e[idx] != NULL) {
+ return contains(root->e[idx], txt, s+1);
+ } else return 0;
+}