summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Jungeblut <paul.jungeblut@gmail.com>2016-10-05 22:23:54 +0200
committerPaul Jungeblut <paul.jungeblut@gmail.com>2016-10-05 22:23:54 +0200
commit2cb378d70cd847491680e7768798169ce3f93f00 (patch)
treeace541f5f00e1f8d37220d77eb0415cbd64a749b
parentaa669973a3ef1e372daf80b1e3b80b020fee0771 (diff)
Improve trie code.
-rw-r--r--string/trie.cpp21
-rw-r--r--tcr.pdfbin267523 -> 267632 bytes
2 files changed, 9 insertions, 12 deletions
diff --git a/string/trie.cpp b/string/trie.cpp
index 24a21b2..5ee7a87 100644
--- a/string/trie.cpp
+++ b/string/trie.cpp
@@ -5,21 +5,18 @@ struct node {
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++;
+void insert(node *root, string &txt, int s) { // Laufzeit: O(|txt|)
+ if(s == (int)txt.size()) root->c++;
else {
- int idx = (int)(*txt[s] - 'a');
- if(root->e[idx] == NULL) {
- root->e[idx] = new node();
- }
+ int idx = (int)(txt[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[s] - 'a');
- if(root->e[idx] != NULL) {
- return contains(root->e[idx], txt, s+1);
- } else return 0;
+int contains(node *root, string &txt, int s) { // Laufzeit: O(|txt|)
+ if(s == txt.size()) return root->c;
+ int idx = (int)(txt[s] - 'a');
+ if(root->e[idx] != NULL) return contains(root->e[idx], txt, s + 1);
+ else return 0;
}
diff --git a/tcr.pdf b/tcr.pdf
index d126384..f32988a 100644
--- a/tcr.pdf
+++ b/tcr.pdf
Binary files differ