Implement Trie (Prefix Tree) Data Structure in PyDataStructs #615
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
References to Other Issues or PRs or Relevant Literature
Fixes #614
This PR introduces a Trie (Prefix Tree) data structure to the PyDataStructs library. Tries are fundamental data structures widely used in applications such as autocomplete, spell checkers, and IP routing.
Brief Description of What is Fixed or Changed
This PR adds:
Trie Implementation (pydatastructs/trees/trie.py)
TrieNode class for individual nodes
Trie class with essential methods:
insert(word): Inserts a word into the Trie
search(word): Checks if a word exists in the Trie
starts_with(prefix): Determines if any word starts with a given prefix
delete(word): Removes a word from the Trie
_get_node(prefix): A helper method for efficient prefix search
Tests (tests/trees/test_trie.py)
Unit tests covering all functionalities
Edge cases (empty strings, deletion of non-existent words)
Validations against expected Trie behavior
Project Structure Updates
Updated pydatastructs/trees/init.py to expose Trie
Added tests/init.py if missing
Other Comments
The Trie follows the coding style of PyDataStructs
The implementation is well-documented with NumPy-style docstrings and examples
Tests can be run using:
python -m pytest --doctest-modules --cov=./ --cov-report=html
Code coverage has been verified
Looking forward to feedback from the maintainers! @Kishan-Ved