Lesson 13 / 42
Trie
A prefix tree: one node per character, sharing common prefixes.
Words as paths
Each word is a path from the root, one edge per character. cat and car share the c-a path then branch. Lookup, insert, and prefix checks are O(L) where L is the word length — independent of how many words are stored.
Insert & search
A node is just a map of child characters plus an end-of-word flag.
class Trie:
def __init__(self):
self.root = {}
def insert(self, word):
node = self.root
for c in word:
node = node.setdefault(c, {})
node['