diff --git a/.github/workflows/awesome_workflow.yml b/.github/workflows/awesome_workflow.yml index 473d7940663..fcf7bada175 100644 --- a/.github/workflows/awesome_workflow.yml +++ b/.github/workflows/awesome_workflow.yml @@ -41,11 +41,22 @@ jobs: - name: Lint modified files shell: bash run: python3 scripts/file_linter.py - - name: Commit and push changes - run: | - git diff DIRECTORY.md - git commit -am "clang-format and clang-tidy fixes for ${GITHUB_SHA::8}" || true - git push origin HEAD:$GITHUB_REF || true + - name: Create Pull Request for automated fixes + uses: peter-evans/create-pull-request@v5 + with: + commit-message: 'chore: clang-format and clang-tidy fixes for ${{ github.sha }}' + title: 'chore: Automated formatting and linting fixes' + body: | + ## Automated Changes + This PR contains automated formatting and linting fixes generated by the CI workflow. + + - clang-format applied + - clang-tidy checks fixed + - filename formatting applied + branch: automated-fixes-${{ github.run_number }} + delete-branch: true + author: 'github-actions[bot] ' + committer: 'github-actions[bot] ' build: name: Compile checks diff --git a/data_structures/trie_tree.cpp b/data_structures/trie_tree.cpp index b7b4ce5fd5e..7999868296c 100644 --- a/data_structures/trie_tree.cpp +++ b/data_structures/trie_tree.cpp @@ -29,6 +29,16 @@ class trie { std::array, NUM_CHARS << 1> arr; bool isEndofWord = false; ///< identifier if a node is terminal node + /** @brief Check whether the current trie node has any children */ + bool hasChildren() const { + for (const auto& child : arr) { + if (child) { + return true; + } + } + return false; + } + /** * @brief Convert a character to integer for indexing * @@ -55,7 +65,7 @@ class trie { */ bool search(const std::shared_ptr& root, const std::string& str, int index) { - if (index == str.length()) { + if (index == (int)str.length()) { if (!root->isEndofWord) { return false; } @@ -105,7 +115,7 @@ class trie { * @returns `false` if not found */ bool search(const std::string& str, int index) { - if (index == str.length()) { + if (index == (int)str.length()) { if (!isEndofWord) { return false; } @@ -132,41 +142,27 @@ class trie { * @returns `false` if unsuccessful */ bool deleteString(const std::string& str, int index) { - if (index == str.length()) { + if (index == (int)str.length()) { if (!isEndofWord) { 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; } int j = char_to_int(str[index]); if (!arr[j]) { return false; } - bool var = deleteString(str, index + 1); - if (var) { + bool deleted = arr[j]->deleteString(str, index + 1); + if (!deleted) { + return false; + } + + if (!arr[j]->isEndofWord && !arr[j]->hasChildren()) { arr[j].reset(); - if (isEndofWord) { - return false; - } else { - int i = 0; - for (i = 0; i < NUM_CHARS; i++) { - if (arr[i]) { - return false; - } - } - return true; - } } - /* should not return here */ - std::cout << __func__ << ":" << __LINE__ - << "Should not reach this line\n"; - return false; + return true; } }; } // namespace data_structures @@ -192,10 +188,13 @@ static void test() { 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"; + root.insert("Hell"); + assert(root.search("Hell", 0)); + assert(root.search("Hello", 0)); + + assert(root.deleteString("Hello", 0)); + assert(!root.search("Hello", 0)); + assert(root.search("Hell", 0)); } /** diff --git a/search/recursive_binary_search.cpp b/search/recursive_binary_search.cpp new file mode 100644 index 00000000000..a47919c42da --- /dev/null +++ b/search/recursive_binary_search.cpp @@ -0,0 +1,53 @@ +/** + * @file recursive_binary_search.cpp + * @brief Recursive Binary Search implementation + */ + +#include +#include +#include + +namespace search { + +namespace recursive_binary_search { + +int binarySearch(const std::vector& arr, + int left, + int right, + int target) { + if (left > right) { + return -1; + } + + int mid = left + (right - left) / 2; + + if (arr[mid] == target) { + return mid; + } + + if (arr[mid] > target) { + return binarySearch(arr, left, mid - 1, target); + } + + return binarySearch(arr, mid + 1, right, target); +} + +} // namespace recursive_binary_search + +} // namespace search + +static void test() { + std::vector arr = {1, 2, 3, 4, 5, 6, 7}; + + int result = search::recursive_binary_search::binarySearch( + arr, 0, arr.size() - 1, 5); + + assert(result == 4); + + std::cout << "Test passed!\n"; +} + +int main() { + test(); + return 0; +} \ No newline at end of file