0
0
DSA C++programming~10 mins

Longest Word in Dictionary Using Trie in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Longest Word in Dictionary Using Trie
Start: Insert words into Trie
Build Trie nodes for each char
Mark end of word nodes
DFS from root to find longest word
At each node: Check if word ends here
If yes, continue deeper
Update longest word if current path longer
Return longest word found
Insert all words into a Trie, then use depth-first search to find the longest word where all prefixes are valid words.
Execution Sample
DSA C++
class TrieNode {
public:
  TrieNode* children[26] = {nullptr};
  bool isEnd = false;
};
Defines a Trie node with 26 children pointers and a flag to mark end of a word.
Execution Table
StepOperationNodes in TriePointer ChangesVisual State
1Insert 'a'root -> a (isEnd=true)root->children['a' - 'a'] = new noderoot -> a*
2Insert 'app'root -> a* -> p -> p (isEnd=true)a->children['p' - 'a'] = new node, p->children['p' - 'a'] = new noderoot -> a* -> p -> p*
3Insert 'apple'root -> a* -> p -> p* -> l -> e (isEnd=true)p->children['l' - 'a'] = new node, l->children['e' - 'a'] = new noderoot -> a* -> p -> p* -> l -> e*
4Insert 'apply'root -> a* -> p -> p* -> l -> e* and l -> y (isEnd=true)l->children['y' - 'a'] = new noderoot -> a* -> p -> p* -> l -> e*, y*
5Insert 'banana'root -> a* ... and b -> a -> n -> a -> n -> a (isEnd=true)root->children['b' - 'a'] = new node, chain for 'banana'root -> a* ... b -> a -> n -> a -> n -> a*
6DFS start at rootAll nodes builtSet longestWord = ''No change
7Visit 'a' node (isEnd=true)At node 'a'longestWord = 'a'Current longest: 'a'
8Visit 'p' after 'a' (not end)At node 'p'Skip deeper (prefix not word)No update
9Visit second 'p' after 'ap' (isEnd=true)At node 'p'longestWord = 'app'Current longest: 'app'
10Visit 'l' after 'app' (not end)At node 'l'Skip deeperNo update
11Visit 'e' after 'appl' (isEnd=true)At node 'e'longestWord = 'apple'Current longest: 'apple'
12Visit 'y' after 'appl' (isEnd=true)At node 'y'longestWord = 'apply' (tie, lex order decides)Current longest: 'apple' or 'apply'
13Visit 'b' node (not end)At node 'b'Skip deeperNo update
14DFS completeLongest word foundReturn 'apple'Final longest word: 'apple'
💡 DFS ends after visiting all nodes; longest word with all prefixes present is 'apple'
Variable Tracker
VariableStartAfter Step 7After Step 9After Step 11After Step 12Final
longestWord'''a''app''apple''apple''apple'
currentNoderoot'a''p''e''y'null (DFS end)
Trie Size1 (root)247814 nodes total
Key Moments - 3 Insights
Why do we skip deeper when a node is not marked as end of a word during DFS?
Because the problem requires all prefixes to be valid words. If a node is not an end, its prefix is incomplete, so deeper paths can't form valid words. See execution_table rows 8 and 10.
How do we decide between words of the same length like 'apple' and 'apply'?
We choose the lexicographically smaller word. In the example, 'apple' is chosen over 'apply'. See execution_table row 12.
Why do we mark nodes with isEnd=true during insertion?
Marking nodes as end of word helps DFS know which prefixes are valid words, essential for checking the condition. See execution_table rows 1-5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the longestWord after step 9?
A'apple'
B'a'
C'app'
D''
💡 Hint
Check the 'longestWord' variable in variable_tracker after step 9.
At which step does the DFS skip deeper because the node is not an end of a word?
AStep 8
BStep 11
CStep 7
DStep 14
💡 Hint
Look at execution_table rows where 'Skip deeper' is mentioned in Pointer Changes.
If the word 'app' was not inserted, how would the longestWord after step 11 change?
A'apple'
B'a'
C''
D'apply'
💡 Hint
Without 'app' marked as end, prefixes for 'apple' fail; see key_moments about prefix checks.
Concept Snapshot
Trie stores words by characters in nodes.
Mark nodes where words end.
Use DFS to find longest word with all prefixes valid.
Skip paths where prefix node is not end.
Choose lex smaller if tie in length.
Return longest valid word found.
Full Transcript
We build a Trie by inserting each word character by character, marking nodes where words end. Then, we perform a depth-first search starting from the root. At each node, we check if it marks the end of a word. If yes, we continue deeper; if not, we skip that path because the problem requires all prefixes to be valid words. We keep track of the longest word found so far, updating it when we find a longer valid word or a lexicographically smaller word of the same length. Finally, we return the longest word found after visiting all nodes.