Skip to content
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

Implement Trie Data Structure #78

Closed
wants to merge 1 commit into from

Conversation

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

Implement Trie Data Structure

This pull request introduces a new Trie data structure implementation to the marcosfede/algorithms repository. The Trie, also known as a prefix tree, is an efficient tree-like data structure used for storing and retrieving strings, making it particularly useful for tasks such as autocomplete, spell checking, and IP routing.

Features

  • Implemented a memory-efficient Trie data structure using a regular dictionary for children nodes.
  • Added three main methods:
    1. insert(word): Inserts a word into the trie.
    2. search(word): Searches for a complete word in the trie.
    3. starts_with(prefix): Checks if any word in the trie starts with the given prefix.

Implementation Details

  • Created two classes: TrieNode and Trie.
  • TrieNode represents each node in the Trie, containing:
    • A dictionary of children nodes.
    • A flag to indicate if it represents the end of a word.
  • The Trie is implemented to handle lowercase letters a-z.

Test Cases

A comprehensive test suite has been created to verify the functionality of the Trie implementation. The test cases cover:

  1. Inserting and searching for words.
  2. Checking for prefixes.
  3. Handling multiple words.
  4. Edge case: empty string.

Files Added

  1. /tree/trie/new_trie.py: Contains the Trie implementation.
  2. /tree/trie/test_new_trie.py: Contains the test cases for the Trie.

Potential Applications

This Trie implementation can be used in various applications, including:

  • Autocomplete systems
  • Spell checkers
  • IP routing tables
  • Text search and pattern matching algorithms

Next Steps

  • Consider adding more advanced Trie operations, such as deletion or finding all words with a given prefix.
  • Explore optimizations for space efficiency, such as path compression.

This implementation aims to provide a solid foundation for Trie-based algorithms and can be easily extended or modified for specific use cases.

Additional Information

This Devin run was requested by Federico.

Copy link
Author

Closing due to inactivity.

1 similar comment
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.

0 participants