TreesTrees
Trie (Prefix Tree)
A tree keyed by characters where each root-to-node path spells a prefix, giving O(L) insert, lookup and prefix search independent of how many words are stored.
1/43Empty trie: only the root. Each edge holds one character, and a node with the "end" badge terminates a stored word.
Matched prefixCurrent nodeNew nodeMatch / word end
PseudocodeLearn Trie →
1node = root2for ch in word:3 if ch not in node.children: (insert) create child / (search) return false4 node = node.children[ch]5insert: node.end = true6search: return node.end7startsWith: return trueVariables
words0
Complexity
access —
search O(L)
insert O(L)
delete O(L)
Speed