📖 Scenario: Imagine you are building a simple search feature that suggests words as you type. To do this efficiently, you use a Trie, a tree-like data structure that stores words letter by letter.
🎯 Goal: You will create a Trie and write code to insert words into it. This helps you understand how words are stored step-by-step in the Trie.
📋 What You'll Learn
Create a TrieNode struct with a children map and an end-of-word flag
Create a Trie struct with a root node
Write an insert method to add words letter by letter
Print the Trie structure after inserting words
💡 Why This Matters
🌍 Real World
Tries are used in search engines, autocomplete features, and spell checkers to quickly find words or prefixes.
💼 Career
Understanding Tries helps in roles involving text processing, search optimization, and building efficient data retrieval systems.
Progress0 / 4 steps
1
Create the TrieNode struct
Create a struct called TrieNode with two fields: children which is a HashMap<char, TrieNode> and is_end_of_word which is a bool. Also implement a new function that returns an empty TrieNode with is_end_of_word set to false.
DSA Rust
Hint
Use HashMap<char, TrieNode> for children and set is_end_of_word to false in new.
2
Create the Trie struct with root node
Create a struct called Trie with one field root of type TrieNode. Implement a new function for Trie that initializes root with a new TrieNode.
DSA Rust
Hint
Define Trie with a root field and initialize it with TrieNode::new().
3
Implement the insert method for Trie
Inside the impl Trie block, write a method called insert that takes &mut self and a word: &str. For each character in word.chars(), check if it exists in the current node's children. If not, insert a new TrieNode. Move to the child node for the character. After all characters, set is_end_of_word to true for the last node.
DSA Rust
Hint
Use entry(ch).or_insert_with(TrieNode::new) to add missing children nodes.
4
Insert words and print Trie structure
Create a main function. Inside it, create a mutable Trie called trie. Insert the words "cat", "car", and "dog" into trie using the insert method. Then print the root node's children keys as a vector to show the first letters stored in the Trie.
DSA Rust
Hint
Print the keys of trie.root.children as a vector to see the first letters stored.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Trie data structure?
easy
A. To search for a word in the Trie
B. To delete a word from the Trie
C. To add a new word by creating nodes for each character
D. To count the number of words stored in the Trie
Solution
Step 1: Understand the insert operation
The insert operation adds a new word by creating nodes for each character if they don't exist.
To add a new word by creating nodes for each character -> Option C
Quick Check:
Insert = Add word nodes [OK]
Hint: Insert means adding characters as nodes step-by-step [OK]
Common Mistakes:
Confusing insert with search or delete
Thinking insert counts words
Assuming insert modifies existing words
2. Which of the following is the correct Rust syntax to insert a word into a Trie using a method named insert?
easy
A. trie->insert("word");
B. trie.insert("word");
C. insert.trie("word");
D. trie.insert[word];
Solution
Step 1: Recall Rust method call syntax
In Rust, methods are called with dot notation and parentheses for arguments, like object.method(args).
Step 2: Check each option
trie.insert("word"); uses correct dot and parentheses syntax. trie.insert[word]; uses brackets incorrectly. insert.trie("word"); reverses object and method. trie->insert("word"); uses arrow syntax not used in Rust.
Final Answer:
trie.insert("word"); -> Option B
Quick Check:
Rust method call = dot + parentheses [OK]
Hint: Use dot and parentheses for method calls in Rust [OK]
Common Mistakes:
Using brackets instead of parentheses
Reversing method and object order
Using arrow syntax like C++
3. Given the following Rust code snippet for inserting words into a Trie, what will be the printed output after inserting "cat" and "car"?
To fix, children should store Box<TrieNode> or Rc<TrieNode> to have a pointer and known size.
Final Answer:
HashMap cannot store TrieNode directly due to recursive type size -> Option D
Quick Check:
Recursive struct needs indirection like Box [OK]
Hint: Recursive structs need Box or pointer types in Rust [OK]
Common Mistakes:
Ignoring recursive type size error
Thinking get_mut() causes error
Misplacing is_end assignment
5. You want to insert multiple words into a Trie and then find all words starting with a given prefix. Which modification to the Trie insert operation helps support efficient prefix search in Rust?
hard
A. Store a count of words passing through each node during insert
B. Only mark is_end at the root node during insert
C. Insert words in reverse order to optimize prefix search
D. Use a vector instead of HashMap for children to speed up insert
Solution
Step 1: Understand prefix search requirement
To find all words with a prefix, the Trie must quickly identify nodes representing that prefix.
Step 2: Use counts to optimize prefix queries
Storing counts of words passing through nodes during insert helps quickly know how many words share that prefix.
Final Answer:
Store a count of words passing through each node during insert -> Option A
Quick Check:
Prefix search needs counts at nodes [OK]
Hint: Count words at nodes to speed prefix queries [OK]
Common Mistakes:
Marking is_end only at root loses word ends
Inserting words reversed breaks normal prefix logic
Using vector loses fast char lookup
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.