BeginnerCoder

Lesson 22 — Trie (expert)

Trie: a tree of prefixes

A trie (say "try") stores words letter by letter in nested dicts: shared prefixes share the same path. "car" and "cat" share c → a, then split. A special key "#" marks where a full word ends. Searching walks one letter at a time; if a letter is missing, the word is not there.

root = {"c": {"a": {"r": {"#": True}, "t": {"#": True}}}}

Why it matters: autocomplete and spell-checkers use tries because "all words starting with ca" is just one walk down the tree.

Want it another way?
Task

Finish the walk in walk(text): step into the child node for ch, or return None if ch is missing.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Data Structures

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.