build-an-autocomplete-engine / lesson-09.md
Lesson 09 · Prefix traversal and completions

A prefix that is itself a word

A prefix can also be a stored word in its own right, and autocomplete should offer it as one of its own completions. Today you confirm the exact case where the prefix node is marked end-of-word.

The goal

Include the prefix itself in its completions when the prefix is a stored word.

Start here - the target
TO DO
Scenario: The prefix appears among its own completions
Givena trie with Insert("car"), Insert("card"), Insert("care")
WhenCompletions("car") is called
Thenit returns exactly ["car", "card", "care"]
Andcar appears first because the empty suffix (the prefix itself) sorts before any longer word
Background

When you call Completions("car") on a trie that stored car, the subtree walk starts at the car node - and the very first thing it does is check that node’s end flag. Because car was stored, end is true and the empty suffix is recorded, producing car itself. Then it descends into the d and e children for card and care.

The ordering falls out naturally: the empty suffix "" sorts before any non-empty one, so the prefix-word always leads its own completions - car, then card, then care. This matters for real autocomplete: if you have typed a complete word that is also the start of longer ones, the engine should still offer the word you have already typed as a valid choice, not silently drop it in favour of the longer completions.

Make it work
// No new code if lesson 8 is right: the walk starts AT the prefix node,
// and that node's own end flag is checked first (suffix == ""), so a prefix
// that is a word is emitted as prefix+"" before any child is visited.
// This lesson pins that behavior with a test.
CheckpointDONE
A prefix that is also a stored word is included in its own completion list. Commit and stop here.