Skip to the workspace
Pebellslearn by building
Trees, Heaps & Tries  ›  Lesson

Tries & Search-as-you-type

Tries 25 minA tree of characters where titles that start alike share a path

dsa / Tries & Search-as-you-type

Explore each scene at your own pace.

01 · Complete a title before the next keystroke

Complete a title before the next keystroke

Tries & Search-as-you-type

Definition
A trie is a tree whose edges are characters, so each word is a path from the root and words sharing a start share that part of the path; finding a prefix takes one step per character, however many words are stored.
In plain words
It is like autocomplete on a phone: as you type each letter you follow one more branch, and everything beneath where you stop is a possible word. Words starting alike walk the same road at first.

In short: a trie (said "try", from retrieval) is a tree whose edges are characters. Every title is a path down from the root, and titles that begin the same way share the start of that path — so a prefix is found in as many steps as it has characters, no matter how many titles the trie holds. Each node holds a dict of children and an is_end flag marking whether a title ends exactly there; without that flag, every prefix of every real title would wrongly pass as a title too. search(word) is node is not None and node.is_end; starts_with(prefix) only needs node is not None. Both cost O(m) for m characters.

Two four-level chains n-e-o-n, only "neon" inserted: the left, checking only "does the path exist", wrongly calls "neo" a word at the o node; the right checks the is_end flag, which is only true at the final n, so "neo" correctly fails

Complete a title before the next keystroke

Type "ne" into Tunebox's search box and suggestions should appear before the next key is pressed. Checking title.startswith("ne") against every title works for a few hundred songs, but with millions, every keystroke rescans the lot. A trie's node holds a dict of children and the end flag:

python
watch it move · tree

A trie holding glow, neo, neon and net, where ● marks a node at which a title ends, and typing a prefix walks one edge per character.

Every lookup starts at the root, which all four titles share. Binary tree, 10 nodes, height 5. At node root.

1 / 5

(Each node here has at most two children only because this example is small; a trie node can have one child per possible character.)

Scene 1 of 9
Project context and further resources
You're building a piece ofTunebox — discovery
This piece — suggest_titles(): Completes a title from its first few letters.
Scenario As a listener types into Tunebox's search box, suggestions update on every keystroke. The titles live in a trie, so each keystroke walks only as far as the letters typed.
suggest_titles.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
suggest_titles(titles, prefix, limit) → listUp to limit titles starting with prefix, alphabetically.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.