From 78f500e8648d687a898ef55821de279def8558ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Thu, 19 Dec 2019 17:37:30 +0800 Subject: [PATCH] Repair warning: ISO C + + forbids converting a string constant to 'char *' [- wwrite strings] (#683) * Add header file * Add header file and regulate code style * Add header file and regulate code style * Do not use namespace using-directives. Use using-declarations instead. * Arctic2333 Add header file and regulate code style . Do not use namespace using-directives. Use using-declarations instead. * Repair warning: ISO C + + forbids converting a string constant to 'char *' [- wwrite strings] * Repair warning: ISO C + + forbids converting a string constant to 'char *' [- wwrite strings] * rename * RENAME * Repair warning: ISO C + + forbids converting a string constant to 'char *' [- wwrite strings] * char * -> string * Update and rename trietree.cpp to trie_tree.cpp * Delete TrieTree.cpp * char hello[] = "hello"; --- data_structure/TrieTree.cpp | 97 ------------------------------------ data_structure/trie_tree.cpp | 91 +++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 97 deletions(-) delete mode 100644 data_structure/TrieTree.cpp create mode 100644 data_structure/trie_tree.cpp diff --git a/data_structure/TrieTree.cpp b/data_structure/TrieTree.cpp deleted file mode 100644 index 2368a76aa..000000000 --- a/data_structure/TrieTree.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include -#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(); - for (int i = 0; i < 26; i++) - nn->arr[i] = NULL; - nn->isEndofWord = false; - return nn; -} - -// insert string into the trie -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; -} - -// search a string exists inside the trie -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); -} -/* 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)) - { - 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 diff --git a/data_structure/trie_tree.cpp b/data_structure/trie_tree.cpp new file mode 100644 index 000000000..56cd25427 --- /dev/null +++ b/data_structure/trie_tree.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include + +// structure definition +typedef struct trie { + struct trie * arr[26]; + bool isEndofWord; +} +trie; + +// create a new node for trie +trie * createNode() { + trie * nn = new trie(); + for (int i = 0; i < 26; i++) + nn -> arr[i] = NULL; + nn -> isEndofWord = false; + return nn; +} + +// insert string into the trie +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; +} + +// search a string exists inside the trie +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); +} + +/* 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)) { + 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(); + 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); + printf("%d %d ", a, b); + return 0; +}