| Crates.io | prefix-tree-rs |
| lib.rs | prefix-tree-rs |
| version | 0.1.1 |
| created_at | 2024-12-18 14:35:02.594154+00 |
| updated_at | 2024-12-18 15:57:23.505231+00 |
| description | A Trie (prefix tree) implementation |
| homepage | https://github.com/fushji/prefix-tree |
| repository | https://github.com/fushji/prefix-tree |
| max_upload_size | |
| id | 1487811 |
| size | 10,283 |
A lightweight and efficient implementation of a Trie (prefix tree) data structure in Rust. This library provides a way to store and retrieve strings based on their prefixes, making it ideal for applications like autocomplete, spell checkers, and IP routing tables.
Add this to your Cargo.toml:
[dependencies]
prefix_tree_rs = "0.1.1"
use prefix_tree_rs::Trie;
fn main() {
// Create a new Trie
let mut trie = Trie::new();
// Insert some words
trie.insert("hello");
trie.insert("world");
trie.insert("helm");
// Search for words
assert!(trie.search("hello")); // true
assert!(!trie.search("hell")); // false
// Check prefixes
assert!(trie.starts_with("he")); // true
assert!(!trie.starts_with("wo")); // true
}
Trie::new()Creates a new, empty Trie.
Trie::insert(&mut self, word: &str)Inserts a word into the Trie.
Trie::search(&self, word: &str) -> boolChecks if a word exists in the Trie.
Trie::starts_with(&self, prefix: &str) -> boolChecks if there is any word in the Trie that starts with the given prefix.
The Trie is implemented using two main structures:
Trie: The main structure containing the root nodeTrieNode: Individual nodes containing:
Time Complexity:
Space Complexity:
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.