prefix_tree

PyPI - Downloads PyPI License

📦 prefix_tree

A lightweight, pure-Python prefix tree (trie) implementation for fast in-memory prefix search, autocomplete, and filtering based on metadata. Useful for building autocomplete engines, suggestion systems, and efficient word lookups.


✨ Features


📅 Installation

pip install prefix-tree

🚀 Usage Example

from prefix_tree import Trie

# Create a new trie
trie = Trie()

# Insert words with associated metadata
trie.insert("hello", {"name": "hello", "amount": 10, "gender": "t", "type": "t"})
trie.insert("help", {"name": "help", "amount": 5, "gender": "f", "type": "f"})
trie.insert("hell", {"name": "hell", "amount": 7, "gender": "t", "type": "f"})

# Search by prefix and sort by amount (descending)
results = trie.get_by_prefix_sort_desc_by("hel", "amount")
print(results)
# Output:
# [{'name': 'hello', 'amount': 10, ...}, {'name': 'hell', 'amount': 7, ...}, {'name': 'help', 'amount': 5, ...}]

# Search by prefix and filter by query
filtered = trie.get_by_prefix_and_query("hel", {"gender": "t"})
print(filtered)
# Output:
# [{'name': 'hello', 'amount': 10, ...}, {'name': 'hell', 'amount': 7, ...}]

🔧 Build & Upload to PyPI

python3 -m build
twine upload dist/*

🧠 Use Cases



📚 Keywords

autocomplete, trie, prefix search, in-memory database, suggestions, python trie, word search, autocompletion, fast lookup, filtering


📝 License

MIT License (see LICENSE for details)


✨ Author

Created by ilia iakhin