use std::collections::HashMap;
struct TrieNode {
children: HashMap<char, TrieNode>,
is_end_of_word: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: HashMap::new(),
is_end_of_word: false,
}
}
}
struct Trie {
root: TrieNode,
}
impl Trie {
fn new() -> Self {
Trie {
root: TrieNode::new(),
}
}
fn insert(&mut self, word: &str) {
let mut node = &mut self.root;
for ch in word.chars() {
node = node.children.entry(ch).or_insert(TrieNode::new());
}
node.is_end_of_word = true;
}
fn search(&self, word: &str) -> bool {
let mut node = &self.root;
for ch in word.chars() {
match node.children.get(&ch) {
Some(next_node) => node = next_node,
None => return false,
}
}
node.is_end_of_word
}
}
fn main() {
let mut trie = Trie::new();
trie.insert("cat");
trie.insert("car");
trie.insert("dog");
println!("{}", trie.search("car"));
}The code inserts the words "cat", "car", and "dog" into the Trie. Then it searches for "car". Since "car" was inserted, the search returns true.
use std::collections::HashMap;
struct TrieNode {
children: HashMap<char, TrieNode>,
is_end_of_word: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: HashMap::new(),
is_end_of_word: false,
}
}
}
struct Trie {
root: TrieNode,
}
impl Trie {
fn new() -> Self {
Trie {
root: TrieNode::new(),
}
}
fn insert(&mut self, word: &str) {
let mut node = &mut self.root;
for ch in word.chars() {
node = node.children.entry(ch).or_insert(TrieNode::new());
}
node.is_end_of_word = true;
}
fn search(&self, word: &str) -> bool {
let mut node = &self.root;
for ch in word.chars() {
match node.children.get(&ch) {
Some(next_node) => node = next_node,
None => return false,
}
}
node.is_end_of_word
}
}
fn main() {
let mut trie = Trie::new();
trie.insert("cat");
trie.insert("car");
println!("{}", trie.search("cap"));
}The word "cap" was not inserted into the Trie. The search will fail at the character 'p' because it does not exist in the children of the node for 'a'. Hence, the search returns false.
use std::collections::HashMap;
struct TrieNode {
children: HashMap<char, TrieNode>,
is_end_of_word: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: HashMap::new(),
is_end_of_word: false,
}
}
}
struct Trie {
root: TrieNode,
}
impl Trie {
fn new() -> Self {
Trie {
root: TrieNode::new(),
}
}
fn insert(&mut self, word: &str) {
let mut node = &mut self.root;
for ch in word.chars() {
node = node.children.entry(ch).or_insert(TrieNode::new());
}
node.is_end_of_word = true;
}
fn search(&self, word: &str) -> bool {
let mut node = &self.root;
for ch in word.chars() {
if node.children.contains_key(&ch) {
node = node.children.get(&ch).unwrap();
} else {
return false;
}
}
false
}
}
fn main() {
let mut trie = Trie::new();
trie.insert("hello");
println!("{}", trie.search("hello"));
}The search method returns false at the end regardless of whether the word exists. It should return node.is_end_of_word to confirm the word ends here.
Searching a word in a Trie depends only on the length of the word, not the total number of words stored. Each character leads to a child node lookup, so complexity is O(n).
The word "app" was inserted, so searching "app" returns true. "appl" is only a prefix, not a complete inserted word, so searching "appl" returns false.
