Skip to content

feat: Implement Fenwich Tree (Binary Indexed Tree) and Implement Additional Trie Operations #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from

Conversation

arvinder004
Copy link
Contributor

@arvinder004 arvinder004 commented Mar 17, 2025

The Fenwick Tree is a data structure that efficiently calculates prefix sums and performs single element updates on an array in logarithmic time complexity (O(log n)). This makes it a valuable tool for scenarios involving frequent updates and prefix sum queries.

The FenwickTree class has been implemented with the following key methods:

init(self, size_or_array): Initializes the Fenwick Tree, allowing initialization with a specified size or from an existing array.
update(self, index, delta): Updates the value at a given index in the conceptual original array by adding delta and updates the Fenwick Tree accordingly.
prefix_sum(self, index): Calculates and returns the prefix sum up to the given index (inclusive).
range_sum(self, start_index, end_index): Calculates the sum of elements within the specified range using prefix sum operations.
get_value(self, index): (Optional utility) Returns the current value at a given index based on prefix sum differences.

Also solves #617

This pull request introduces several new functionalities to the Trie data structure, significantly enhancing its utility and completeness.

Changes:

Count of Words (count_words()): Adds a word_count attribute to the Trie class to track the number of inserted words.

Implements the count_words() method to return this count.

Longest Common Prefix (longest_common_prefix()): Implements the longest_common_prefix() method to find the longest common prefix among all words in the Trie.
Autocomplete (autocomplete(prefix)): Implements the autocomplete(prefix) method to provide a list of words matching a given prefix.
Bulk Insert (bulk_insert(words)): Implements the bulk_insert(words) method to efficiently insert multiple words.
Clear Trie (clear()): Implements the clear() method to reset the Trie.
Check if Trie is Empty (is_empty()): Implements the is_empty() method to check if the Trie is empty.
Find All Words (find_all_words()): Implements the find_all_words() method to retrieve all words.
Find the Shortest Unique Prefix (shortest_unique_prefix(word)): Implements the shortest_unique_prefix(word) method to find the shortest unique prefix for a given word.
Find the Longest Word (longest_word()): Implements the longest_word() method to find the longest word within the trie.

Tests:
Adds comprehensive unit tests for all new methods using pytest.
Ensures proper handling of edge cases and various scenarios.
Docstrings:
Provides detailed docstrings for all new methods, explaining their purpose, parameters, and return values.

@arvinder004 arvinder004 changed the title feat: Implement Fenwich Tree (Binary Indexed Tree) feat: Implement Fenwich Tree (Binary Indexed Tree) and Implement Additional Trie Operations Mar 19, 2025
@czgdp1807 czgdp1807 closed this Mar 19, 2025
@arvinder004 arvinder004 deleted the arvinder branch March 19, 2025 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants