3.4.14 · D1Trees

Foundations — Trie (prefix tree) — insert, search, startsWith, applications (autocomplete, spell check)

2,059 words9 min readBack to topic

Before you can read a single line of trie code, you need a small toolbox of ideas. The parent note quietly assumes all of them. Below we build each one from absolute zero: plain words → the picture → why the trie needs it. Read top to bottom; each idea stands on the one above it.


1. A character (char)

Picture a single tile from a Scrabble bag. The word car is three tiles in a row: c, a, r.

The set of allowed characters is called the alphabet, written with the Greek letter (sigma). For lowercase English, , so has 26 members. When the parent note writes " = alphabet size", it just means "how many different letters could appear" — here, 26.


2. A string and its length

Figure — Trie (prefix tree) — insert, search, startsWith, applications (autocomplete, spell check)

Look at the figure: the word card is drawn as four tiles left-to-right. The number under the row, , is simply the count of tiles.


3. What "" (Big-O) means

Figure — Trie (prefix tree) — insert, search, startsWith, applications (autocomplete, spell check)
  • ::: the number of words stored.
  • ::: the length of one word being queried.
  • ::: the alphabet size (26 for a–z).

4. Boolean — True / False

Picture a light switch: up (True) or down (False).


5. A map (dictionary / hash map)

Picture a wall of labelled mailboxes: the label is the key, what's inside is the value. You walk straight to the c box; you don't open every box.

If you already know the Hash Table, a map is exactly that: instant lookup by key. A trie is, in a sense, a hash table chained into a tree.


6. A tree, root, node, edge, child, leaf

This is the biggest idea. Build it slowly.

Figure — Trie (prefix tree) — insert, search, startsWith, applications (autocomplete, spell check)

In the figure, follow the colours: the root sits at top. An edge (arrow) carries a single character. Walk root → car and the path you traced spells the prefix car. Note the node after r has two children (d and e) — one parent, several kids, exactly what a map of children allows.


7. The empty string ""

Picture the row of tiles from figure 1 — now with no tiles at all. That "nothing yet" is where the root lives.


8. Prefix vs. full word

Figure — Trie (prefix tree) — insert, search, startsWith, applications (autocomplete, spell check)

The figure shows the card path. Teal circles mark prefixes (c, ca, car) — real spots on the road but no flag. The orange flag at d marks the full word card.

  • Prefix question (startsWith) ::: "does a road reach here?" → return True if the walk didn't break.
  • Word question (search) ::: "is a flag planted here?" → return node.isEnd.

9. Recursion & DFS (a preview)

Picture exploring a cave: follow one tunnel to its end, backtrack, take the next tunnel — never skipping any.


How these foundations feed the trie

character

string plus length L

prefix vs full word

boolean True False

isEnd flag

map key to value

children map

tree root node edge

empty string root

TRIE insert search startsWith

Big-O growth shape

O of L cost claim

DFS explore subtree

autocomplete

Read top-down: characters make strings, strings have prefixes; booleans make the isEnd flag; maps + trees make the children structure; together they build the trie, whose cost is understood through Big-O, and whose autocomplete leans on DFS.

Related deeper structures you'll meet later (do not worry about them now): the Radix Tree (Compressed Trie) squeezes single-child chains, the Suffix Tree stores endings instead of beginnings, Longest Prefix Match and Edit Distance power routing and spell-check, and a Binary Search Tree is the string structure a trie out-races.


Equipment checklist

Read each question, answer aloud, then reveal. If any trips you, re-read that numbered section.

A character is
one single symbol, e.g. the letter c — not a whole word.
A string of length means
an ordered list of characters, e.g. "card" has .
in plain words
the work grows as a straight line in the number of characters — one step per letter.
, , and stand for
number of words, length of one word, and alphabet size (26 for a–z).
A boolean is
a yes/no switch: only True or False.
A map (children) does what
stores key → value and lets you look up a value instantly by its key; here key = character, value = child node.
The root of a trie represents
the empty string "" — the "nothing spelled yet" starting point.
A leaf is
a node with no children; not the same as "a word ends here".
Why can't "leaf = word"
because car is a word yet has child d (for card), so it's internal, not a leaf — we need the isEnd flag.
A prefix vs. a full word
a prefix is any beginning-chunk (road exists); a full word additionally has isEnd = True (flag planted).
search vs. startsWith differ by
search asks "is a flag here?" (isEnd); startsWith asks "does the road reach here?" (True).
DFS means
go down one branch fully, backtrack, try the next — visiting every node of a subtree; used for autocomplete.