-
Notifications
You must be signed in to change notification settings - Fork 142
Implement Trie Data Structure #88
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
Conversation
…ons. Add test cases to verify functionality.
def test_insert_and_search(self): | ||
words = ["apple", "app", "apricot", "banana"] | ||
for word in words: | ||
self.trie.insert(word) | ||
|
||
for word in words: | ||
self.assertTrue(self.trie.search(word)) | ||
|
||
self.assertFalse(self.trie.search("grape")) | ||
self.assertFalse(self.trie.search("ap")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test more
def test_delete(self): | ||
words = ["apple", "app", "apricot", "banana"] | ||
for word in words: | ||
self.trie.insert(word) | ||
|
||
self.assertTrue(self.trie.delete("apple")) | ||
self.assertFalse(self.trie.search("apple")) | ||
self.assertTrue(self.trie.search("app")) | ||
|
||
self.assertTrue(self.trie.delete("banana")) | ||
self.assertFalse(self.trie.search("banana")) | ||
|
||
self.assertFalse(self.trie.delete("grape")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test a little more
def test_empty_string(self): | ||
self.trie.insert("") | ||
self.assertTrue(self.trie.search("")) | ||
self.assertTrue(self.trie.delete("")) | ||
self.assertFalse(self.trie.search("")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
finally!
hey devin, can you clean up this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file level comment
test |
Closing due to inactivity. |
Trie Data Structure Implementation
This pull request (#88) implements a Trie data structure in Python for the
marcosfede/algorithms
repository. The Trie is a tree-like data structure used for efficient storage and retrieval of strings, particularly useful for tasks such as autocomplete and spell-checking.Features
Implemented in
trie/trie.py
:TrieNode
class for representing nodes in the TrieTrie
class with the following methods:insert(word)
: Insert a word into the Triesearch(word, is_prefix=False)
: Search for a word or prefix in the Triedelete(word)
: Delete a word from the TrieTime complexity:
Space complexity: O(n * m), where n is the number of words and m is the average length of words
Implementation Details
delete
operation uses an iterative approach to handle long words efficientlysearch
method includes anis_prefix
parameter to support prefix searchingTesting
Comprehensive unit tests implemented in
trie/test_trie.py
cover:New test cases added for more comprehensive coverage:
test_unicode_characters
: Ensures correct handling of non-ASCII characterstest_large_dataset
: Verifies performance with a large number of wordstest_prefix_matching
: Checks prefix search functionalitytest_edge_cases
: Tests boundary conditions and edge scenariosThese additional tests ensure the robustness and reliability of the Trie implementation across various use cases.
Improvements
Next Steps
This implementation provides a solid foundation for Trie-based algorithms and can be easily extended for various applications.
PR: #88
URL: #88
Requested by: Federico
Link to Devin run: https://staging.itsdev.in/devin/21a67400cd63476a80b6a041168e9615