Skip to content

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

Closed
wants to merge 6 commits into from

Conversation

staging-devin-ai-integration[bot]
Copy link

@staging-devin-ai-integration staging-devin-ai-integration bot commented Aug 26, 2024

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 Trie
    • Trie class with the following methods:
      • insert(word): Insert a word into the Trie
      • search(word, is_prefix=False): Search for a word or prefix in the Trie
      • delete(word): Delete a word from the Trie
  • Time complexity:

    • Insert: O(m), where m is the length of the word
    • Search: O(m), where m is the length of the word
    • Delete: O(m), where m is the length of the word
  • Space complexity: O(n * m), where n is the number of words and m is the average length of words

Implementation Details

  • The delete operation uses an iterative approach to handle long words efficiently
  • Empty string cases are handled appropriately
  • The search method includes an is_prefix parameter to support prefix searching

Testing

Comprehensive unit tests implemented in trie/test_trie.py cover:

  • Basic insertion, search, and deletion operations
  • Edge cases (empty string, long words, single-character words)
  • Common prefixes and their handling
  • Prefix searching functionality
  • Case sensitivity and special characters
  • Unicode character support
  • Large datasets (10,000 words)
  • Complex operations and mixed scenarios

New test cases added for more comprehensive coverage:

  • test_unicode_characters: Ensures correct handling of non-ASCII characters
  • test_large_dataset: Verifies performance with a large number of words
  • test_prefix_matching: Checks prefix search functionality
  • test_edge_cases: Tests boundary conditions and edge scenarios

These additional tests ensure the robustness and reliability of the Trie implementation across various use cases.

Improvements

  • Implemented prefix search functionality
  • Enhanced test coverage for edge cases and large datasets
  • Improved code organization and documentation
  • Cleaned up commit history through rebasing

Next Steps

  • Consider adding more advanced Trie operations (e.g., autocomplete, wildcard search)
  • Optimize memory usage for large datasets
  • Implement serialization/deserialization for persistent storage

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

…ons. Add test cases to verify functionality.
Comment on lines +8 to +17
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"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test more

Comment on lines +19 to +31
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"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test a little more

Comment on lines +33 to +37
def test_empty_string(self):
self.trie.insert("")
self.assertTrue(self.trie.search(""))
self.assertTrue(self.trie.delete(""))
self.assertFalse(self.trie.search(""))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more..

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally!

@marcosfede
Copy link
Owner

hey devin, can you clean up this PR?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file level comment

@marcosfede
Copy link
Owner

test

Copy link
Author

Closing due to inactivity.

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.

1 participant