[code fix] Fixed trie_tree for code quality and docs & clang-tidy error check (#1059)

* attempt to fix trie-tree code

* clang-tidy fixes

* remove performance-unnecessary-value-param as error - this has auto-fix

* make test() static

* added original author to the description

* added namespace

* doc updates

* fix true spelling

* extended to small & big caps alphabets - 52 characters

* clang-format and clang-tidy fixes for edc2247a

* move const parameter to after the parameters

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Krishna Vedala 2020-08-27 12:30:14 -04:00 committed by GitHub
parent f255e3f1fa
commit b4bbe9dc4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 193 additions and 75 deletions

View File

@ -1,6 +1,6 @@
--- ---
Checks: '-*,google-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.*,cppcoreguidelines-*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-*,openmp-*,performance-*,portability-*,modernize-*,-modernize-use-trailing-*' Checks: '-*,google-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.*,cppcoreguidelines-*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-*,openmp-*,performance-*,portability-*,modernize-*,-modernize-use-trailing-*'
WarningsAsErrors: '*,-google-readability-*,-google-explicit-constructor,-modernize-*,modernize-avoid-c-arrays,-performance-move-const-arg,-performance-noexcept-move-constructor,-cppcoreguidelines-init-variables,-cppcoreguidelines-pro-*,-cppcoreguidelines-owning-memory,-clang-analyzer-cplusplus.Move' WarningsAsErrors: '*,-google-readability-*,-google-explicit-constructor,-modernize-*,modernize-avoid-c-arrays,-performance-move-const-arg,-performance-noexcept-move-constructor,-performance-unnecessary-value-param,-cppcoreguidelines-init-variables,-cppcoreguidelines-pro-*,-cppcoreguidelines-owning-memory,-clang-analyzer-cplusplus.Move'
HeaderFilterRegex: '' HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false AnalyzeTemporaryDtors: false
FormatStyle: '{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }' FormatStyle: '{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }'

View File

@ -1,91 +1,209 @@
#include <stdbool.h> /**
#include <stdio.h> * @file
* @author [@Arctic2333](https://github.com/Arctic2333)
* @author [Krishna Vedala](https://github.com/kvedala)
* @brief Implementation of [Trie](https://en.wikipedia.org/wiki/Trie) data
* structure for English alphabets in small characters.
* @note the function ::data_structure::trie::deleteString might be erroneous
* @see trie_modern.cpp
*/
#include <array>
#include <cassert>
#include <iostream> #include <iostream>
#include <memory>
#include <string> #include <string>
#include <vector>
// structure definition /** \namespace data_structure
typedef struct trie { * \brief Data-structure algorithms
struct trie* arr[26]; */
bool isEndofWord; namespace data_structure {
} trie; /**
* @brief [Trie](https://en.wikipedia.org/wiki/Trie) implementation for
* small-case English alphabets `a-z`
*/
class trie {
private:
static constexpr uint8_t NUM_CHARS = 26; ///< Number of alphabets
/** @brief Recursive tree nodes as an array of shared-pointers */
std::array<std::shared_ptr<trie>, NUM_CHARS << 1> arr;
bool isEndofWord = false; ///< identifier if a node is terminal node
// create a new node for trie /**
trie* createNode() { * @brief Convert a character to integer for indexing
trie* nn = new trie(); *
for (int i = 0; i < 26; i++) nn->arr[i] = NULL; * @param ch character to index
nn->isEndofWord = false; * @return unsigned integer index
return nn; */
} uint8_t char_to_int(const char& ch) const {
if (ch >= 'A' && ch <= 'Z') {
return ch - 'A';
} else if (ch >= 'a' && ch <= 'z') {
return ch - 'a' + NUM_CHARS;
}
// insert string into the trie std::cerr << "Invalid character present. Exiting...";
void insert(trie* root, std::string str) { std::exit(EXIT_FAILURE);
for (int i = 0; i < str.length(); i++) { return 0;
int j = str[i] - 'a'; }
/** search a string exists inside a given root trie
* @param str string to search for
* @param index start index to search from
* @returns `tre` if found
* @returns `false` if not found
*/
bool search(const std::shared_ptr<trie>& root, const std::string& str,
int index) {
if (index == str.length()) {
if (!root->isEndofWord) {
return false;
}
return true;
}
int j = char_to_int(str[index]);
if (!root->arr[j]) {
return false;
}
return search(root->arr[j], str, index + 1);
}
public:
trie() = default; ///< Class default constructor
/** insert string into the trie
* @param str String to insert in the tree
*/
void insert(const std::string& str) {
std::shared_ptr<trie> root(nullptr);
for (const char& ch : str) {
int j = char_to_int(ch);
if (root) {
if (root->arr[j]) { if (root->arr[j]) {
root = root->arr[j]; root = root->arr[j];
} else { } else {
root->arr[j] = createNode(); std::shared_ptr<trie> temp(new trie());
root = root->arr[j]; root->arr[j] = temp;
root = temp;
}
} else if (arr[j]) {
root = arr[j];
} else {
std::shared_ptr<trie> temp(new trie());
arr[j] = temp;
root = temp;
} }
} }
root->isEndofWord = true; root->isEndofWord = true;
} }
// search a string exists inside the trie /** search a string exists inside the trie
bool search(trie* root, std::string str, int index) { * @param str string to search for
* @param index start index to search from
* @returns `true` if found
* @returns `false` if not found
*/
bool search(const std::string& str, int index) {
if (index == str.length()) { if (index == str.length()) {
if (!root->isEndofWord) if (!isEndofWord) {
return false; return false;
}
return true; return true;
} }
int j = str[index] - 'a'; int j = char_to_int(str[index]);
if (!root->arr[j]) if (!arr[j]) {
return false; return false;
return search(root->arr[j], str, index + 1); }
} return search(arr[j], str, index + 1);
}
/* /**
removes the string if it is not a prefix of any other * removes the string if it is not a prefix of any other
string, if it is then just sets the endofword to false, else * string, if it is then just sets the ::data_structure::trie::isEndofWord
removes the given string * to false, else removes the given string
*/ * @note the function ::data_structure::trie::deleteString might be
bool deleteString(trie* root, std::string str, int index) { * erroneous
* @todo review the function ::data_structure::trie::deleteString and the
* commented lines
* @param str string to remove
* @param index index to remove from
* @returns `true` if successful
* @returns `false` if unsuccessful
*/
bool deleteString(const std::string& str, int index) {
if (index == str.length()) { if (index == str.length()) {
if (!root->isEndofWord) if (!isEndofWord) {
return false; return false;
root->isEndofWord = false; }
for (int i = 0; i < 26; i++) return false; isEndofWord = false;
// following lines - possible source of error?
// for (int i = 0; i < NUM_CHARS; i++)
// if (!arr[i])
// return false;
return true; return true;
} }
int j = str[index] - 'a'; int j = char_to_int(str[index]);
if (!root->arr[j]) if (!arr[j]) {
return false; return false;
bool var = deleteString(root, str, index + 1); }
bool var = deleteString(str, index + 1);
if (var) { if (var) {
root->arr[j] = NULL; arr[j].reset();
if (root->isEndofWord) { if (isEndofWord) {
return false; return false;
} else { } else {
int i; int i = 0;
for (i = 0; i < 26; i++) for (i = 0; i < NUM_CHARS; i++) {
if (root->arr[i]) if (arr[i]) {
return false; return false;
}
}
return true; return true;
} }
} }
/* should not return here */ /* should not return here */
std::cout << __func__ << ":" << __LINE__ << "Should not reach this line\n"; std::cout << __func__ << ":" << __LINE__
<< "Should not reach this line\n";
return false; return false;
}
};
} // namespace data_structure
/**
* @brief Testing function
* @returns void
*/
static void test() {
data_structure::trie root;
root.insert("Hello");
root.insert("World");
assert(!root.search("hello", 0));
std::cout << "hello - " << root.search("hello", 0) << "\n";
assert(root.search("Hello", 0));
std::cout << "Hello - " << root.search("Hello", 0) << "\n";
assert(!root.search("Word", 0));
std::cout << "Word - " << root.search("Word", 0) << "\n";
assert(root.search("World", 0));
std::cout << "World - " << root.search("World", 0) << "\n";
// Following lines of code give erroneous output
// root.deleteString("hello", 0);
// assert(!root.search("hello", 0));
// std::cout << "hello - " << root.search("world", 0) << "\n";
} }
/**
* @brief Main function
* @return 0 on exit
*/
int main() { int main() {
trie* root = createNode(); test();
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; return 0;
} }