A Trie (Prefix Tree) is an $N$-ary tree structure designed for fast character prefix string searching, powering search engine autocomplete systems.
Start at Root node. Check root.children['c']. If null, create new TrieNode for letter 'c'.
Root -> Node('c')Move to Node('c'). Check Node('c').children['a']. Create new TrieNode for letter 'a'.
Root -> Node('c') -> Node('a')Move to Node('a'). Create Node('t') and set isEnd = true to mark end of word 'cat'.
Root -> Node('c') -> Node('a') -> Node('t') (isEnd = true)To find 'cat', you follow letter branch 'c', then branch 'a', then branch 't'. Words sharing the same prefix share the same tree path.
~9 steps (Logarithmic)
O(log N) Tree Height Time
O(N) Tree Nodes Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Search Word / Prefix | O(L) | O(1) |
| Insert Word | O(L) | O(L) |
class TrieNode {
constructor() { this.children = {}; this.isEnd = false; }
}Suggests query completions as users type letters.
Validates dictionary words rapidly.
Validate your conceptual understanding, operation mechanics, and core concepts of Trie.
Trie search time depends ONLY on the string length (L), completely independent of how many millions of total words are stored!
Trie initialized. Type a word and click Insert to build the prefix tree.