📖 Scenario: Imagine you are building a simple phone contact search system. You want to quickly check if a contact name exists in your phone book.
🎯 Goal: Build a Trie data structure and implement a search operation to check if a given contact name exists.
📋 What You'll Learn
Create a TrieNode class with an array of 26 children pointers and a boolean to mark end of word
Create a Trie class with insert and search methods
Insert given contact names into the Trie
Search for a specific contact name using the Trie search method
Print true if the contact exists, otherwise false
💡 Why This Matters
🌍 Real World
Tries are used in phone contact search, autocomplete, and spell checking to quickly find words or prefixes.
💼 Career
Understanding Trie search operations is important for roles involving text processing, search engines, and efficient data retrieval.
Progress0 / 4 steps
1
Create TrieNode class and Trie class with insert method
Create a class called TrieNode with a public boolean isEndOfWord set to false and an array of 26 TrieNode* called children initialized to nullptr. Then create a class called Trie with a public TrieNode* called root initialized to a new TrieNode. Add a public method insert that takes a string word and inserts it into the Trie.
DSA C++
Hint
Define the TrieNode with 26 children pointers and a boolean flag. In Trie, initialize root and implement insert by traversing characters and creating nodes.
2
Add search method to Trie class
Add a public method search to the Trie class that takes a string word and returns true if the word exists in the Trie, otherwise false. Use a for loop with variable c to traverse the Trie nodes.
DSA C++
Hint
Traverse the Trie for each character. If a child node is missing, return false. After the loop, return if current node marks end of word.
3
Insert contact names into the Trie
Create a Trie object called trie. Insert the following contact names into the Trie using the insert method: "alice", "bob", "carol", "dave".
DSA C++
Hint
Create a Trie object and call insert for each contact name exactly as given.
4
Search for a contact and print the result
Use the search method of the trie object to check if the contact name "carol" exists. Print true if it exists, otherwise print false.
DSA C++
Hint
Call trie.search("carol") and print true if found, else false.
Practice
(1/5)
1. What does the search operation in a Trie data structure do?
easy
A. Checks if a given word exists in the Trie
B. Deletes a word from the Trie
C. Inserts a new word into the Trie
D. Counts the total words stored in the Trie
Solution
Step 1: Understand the purpose of search in Trie
The search operation is designed to check if a specific word is present in the Trie structure.
Step 2: Differentiate from other operations
Insertion adds words, deletion removes words, and counting totals words. Search only checks existence.
Final Answer:
Checks if a given word exists in the Trie -> Option A
Quick Check:
Trie search = check word presence [OK]
Hint: Search checks presence of word, not insertion or deletion [OK]
Common Mistakes:
Confusing search with insert operation
Thinking search deletes words
Assuming search counts words
2. Which of the following is the correct way to check if a character node exists in a Trie during search in C++?
easy
A. if (node->children[ch])
B. if (node->children[ch] == nullptr)
C. if (node->children[ch] != nullptr)
D. if (node->children.find(ch) != node->children.end())
Solution
Step 1: Understand Trie children representation
Trie children are usually stored in a map or unordered_map. To check existence, use find() method.
Step 2: Identify correct syntax for existence check
Using find(ch) != end() confirms if character node exists without creating new nodes.
Final Answer:
if (node->children.find(ch) != node->children.end()) -> Option D
Quick Check:
Use find() to check key existence in map [OK]
Hint: Use find() != end() to check map key existence safely [OK]
Common Mistakes:
Using node->children[ch] != nullptr causes insertion
Accessing children[ch] directly creates new node
Checking children[ch] == nullptr can cause insertion
3. Consider the following C++ Trie search code snippet: