From b5a074f1886463db682fd8281e31900aa838a34f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 19 Dec 2019 12:48:43 +0100 Subject: [PATCH] trie_tree.cpp: #include --> (#684) * trie_tree.cpp: #include --> * std::string str --- data_structure/trie_tree.cpp | 37 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/data_structure/trie_tree.cpp b/data_structure/trie_tree.cpp index 56cd25427..2b84099e8 100644 --- a/data_structure/trie_tree.cpp +++ b/data_structure/trie_tree.cpp @@ -1,14 +1,14 @@ #include -#include #include + #include +#include // structure definition typedef struct trie { struct trie * arr[26]; bool isEndofWord; -} -trie; +} trie; // create a new node for trie trie * createNode() { @@ -20,8 +20,8 @@ trie * createNode() { } // insert string into the trie -void insert(trie * root, char * str) { - for (int i = 0; i < strlen(str); i++) { +void insert(trie * root, std::string str) { + for (int i = 0; i < str.length(); i++) { int j = str[i] - 'a'; if (root -> arr[j]) { root = root -> arr[j]; @@ -34,8 +34,8 @@ void insert(trie * root, char * str) { } // search a string exists inside the trie -bool search(trie * root, char * str, int index) { - if (index == strlen(str)) { +bool search(trie * root, std::string str, int index) { + if (index == str.length()) { if (!root -> isEndofWord) return false; return true; @@ -46,11 +46,13 @@ bool search(trie * root, char * str, int index) { 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)) { +/* +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, std::string str, int index) { + if (index == str.length()) { if (!root -> isEndofWord) return false; root -> isEndofWord = false; @@ -79,13 +81,10 @@ bool deleteString(trie * root, char * str, int index) { int main() { trie * root = createNode(); - char hello[] = "hello"; - char world[] = "world"; - char word[] = "word"; - insert(root, hello); - insert(root, world); - int a = search(root, hello, 0); - int b = search(root, word, 0); + 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; }