Trie Animation
A glowing tree that grows letter by letter, with shared prefixes, snapping lookups and autocomplete: a Trie ready for your lessons, slides and posts
.zip · HTML / CSS / JS · v1.0.0
Free — no account, no email. Always the newest version.
Live preview
About
A trie is easiest to understand when you watch it grow. This animation runs the real insert,
search, startsWith, countWithPrefix, delete, autocomplete, nodeCount and size from the
free algorithm page, step by step: every letter is a glowing node in a tree that grows to the right
like a line of text, words that start the same way share their first nodes, and a star marks
where a word ends. A cursor walks the path of each call, a lookup snaps off at the letter that is
missing, a prefix query lights up the whole subtree, and delete cuts a branch the moment nothing
lives below it. The letters sit on a tape above the tree, the 26 child slots of the node under the
cursor sit below it, the line of code that is running lights up beside it, and a pair of bars sets
the letters you typed against the nodes that were built.
- Every public method of the listing, line for line, with the private helpers (
#find,#childOrCreate,#collect,#countNodes) shown when they run - Shared prefixes you can see: nodes an insert walks over are marked as reused, only new letters build nodes, and a badge on every node counts the words going through it
- Lookups that snap off where a letter is missing, prefix queries that light the subtree and list its words, autocomplete that fills its pills a to z and stops at the limit
deletethat subtracts along the path, cuts dead branches and only clears the star of a word that starts another- Three seeded scenarios, autocomplete, shared prefixes and prune a branch, or your own operations typed in; every operation in the script is a button that jumps to it
- Counters for nodes created, visited and shared, and the letters typed against the nodes built
- A full player with a scrubber and 0.5×–4× speed, built to look good on a slide or in a recording
- Every colour, radius, font and timing is a CSS custom property; light and dark follow your page
- Nothing to install: one CSS file and one script, or one React component
Features · 14
- Animates every public method of the free page's Trie, exactly as the listing writes it: insert, search, startsWith, countWithPrefix, delete, autocomplete, nodeCount and size
- A tree that grows to the right like a line of text: insert builds a node for each new letter and walks over the ones that already exist, so a shared prefix is visibly reused instead of built twice
- A star marks the node where a word ends, and a badge on every node shows its passCount, the number of words going through it
- A cursor walks the path of every call, with the listing's own variables (node, child, start) gliding along as chips and the letters on a tape above the tree
- A failed lookup snaps off where the letter is missing: the node shakes, the tape marks the letter and the slot strip shows the empty slot
- A prefix query lights the whole subtree and lists the words under it; autocomplete collects them a to z, one pill at a time, and stops the moment it reaches the limit
- delete subtracts along the path and cuts a branch the instant its count reaches zero, while a word that is the start of another one only loses its star
- The 26 child slots of the node under the cursor, so you can see the array behind the tree and why a traversal is already in alphabetical order
- A script of operations you can press to jump to: three seeded scenarios (autocomplete, shared prefixes, prune a branch), or type your own like insert car; insert card; auto ca 3
- Counters for nodes created, visited and shared, and two bars that set the letters typed against the nodes built: the gap is what sharing saved
- The code panel shows the method that is running, public or private, with the call stack above it and the lines of the step lit
- Player: play, pause, step back and forward, reset, scrub, 0.5×–4× speed, optional loop
- Plain HTML/CSS/JS and React (Next.js ready) builds, light and dark
- Zero dependencies, about 24 KB gzipped (script and CSS together)
How to use it
Link the stylesheet and the script once, then add an empty element with
data-trie-animation wherever you want a player:
<link rel="stylesheet" href="trie-animation.css" />
<script src="trie-animation.js" defer></script>
<div class="trie-animation" data-trie-animation></div>
<div
class="trie-animation"
data-trie-animation
data-trie-animation-scenario="prune"
data-trie-animation-speed="2"
></div>
<div
class="trie-animation"
data-trie-animation
data-trie-animation-operations="insert car; insert card; insert dog; count ca; auto ca 3"
></div>Options as data attributes, all starting with data-trie-animation-:
| Attribute | Default | What it does |
|---|---|---|
-scenario |
autocomplete |
autocomplete, prefixes or prune: the list of operations to run |
-operations |
the scenario's | Your own list, separated by ;, , or new lines (up to 24, and up to 80 inserted letters): insert w, search w, starts p, count p, delete w, auto p limit, nodes and size. Words and prefixes are lowercase a–z, up to 12 letters; a lone - is the empty prefix |
-seed |
7 |
Which list the scenario builds: the same seed always gives the same one |
-speed |
1 |
0.25 to 4 |
-autoplay |
true |
Plays on its own once loaded (never with reduced motion) |
-loop |
false |
Starts again after a pause on the summary |
-code |
true |
false hides the code panel |
-inputs |
true |
false hides the rows of inputs (scenario and operations) |
The operations are the methods of the free page's Trie: starts is startsWith, count is
countWithPrefix, auto is autocomplete and nodes is nodeCount. The box also reads the
method names themselves, in any case.
Elements added after the page loads: TrieAnimation.init(container); it returns a function that
undoes it. For full control, TrieAnimation.createTrieAnimation(element, options) returns a
handle with setScenario, setOperations(text) (it returns false and says why on the page when
the list cannot be read), shuffle, player (play, pause, seek…), trace() and destroy.
Copy TrieAnimation.tsx, trie-animation.css and the core folder into your project:
import { TrieAnimation } from "./TrieAnimation";
<TrieAnimation />
<TrieAnimation scenario="prune" autoplay={false} />
<TrieAnimation operations="insert car; insert card; auto ca 3" loop inputs={false} onFrame={(frame) => console.log(frame.caption)} />The props are the options above (scenario, operations, seed, speed, autoplay, loop,
code, inputs), plus onFrame(frame, index), called with every frame drawn. Changing a prop
starts the list again. Every other prop goes to the root <div> (className, id, aria-*…).
React 18 or 19.
The file starts with "use client", so use it from any page or server component. With the App
Router nothing else is needed. With the Pages Router, move the CSS import into pages/_app.
Set these on .trie-animation (or a class of your own):
| Property | What it changes |
|---|---|
--trie-tone-0 … --trie-tone-7 |
The eight depth colours (cyan, violet, pink, amber, mint, sky, orange, lime): the letters of a word go through them in order, so the same prefix always has the same colours |
--trie-lit, --trie-star |
The walk a call makes, and the star of a node where a word ends |
--trie-born, --trie-reuse, --trie-miss |
A node that was just built, a node an insert walked over instead, and a missing letter or a branch being cut |
--trie-orb, --trie-veil |
The ink every node is mixed with, and the colour its glow fades into |
--trie-bg, --trie-surface, --trie-surface-strong, --trie-blur |
The background and the frosted glass |
--trie-edge, --trie-shadow, --trie-code-bg |
Borders, shadows, the code panel |
--trie-text, --trie-muted, --trie-on-accent, --trie-error, --trie-ring |
Text, secondary text, text on the coloured chips and buttons, the error message, focus ring |
--trie-font, --trie-mono |
The fonts for words and for code and numbers |
--trie-radius |
Corners of the player |
--trie-duration, --trie-ease |
How fast and how softly nodes grow, glide and fly |
Colours use light-dark(): they follow color-scheme on your page. With
prefers-reduced-motion, nothing plays on its own and frames change instantly; every control
still works.
Ask about this product
Not sure it fits your project? Ask — the answer comes from this page.
Changelog
1.0.0First releaseLatest
Plain HTML/CSS/JS and React builds of a step-by-step Trie animation: a tree that grows letter by letter with shared prefixes reused, end-of-word stars, a cursor that walks every insert, search, prefix count, delete and autocomplete, a lookup that snaps off at the missing letter, branches that are cut when their count hits zero, three seeded scenarios or your own script, live code, counters, player controls, light and dark.
- Fixes and new features land behind the same download, which always serves the latest version.
Reviews
No reviews yet
Used it? Be the first to say how it went.
Write a review
How did it work for you? Reviews appear once we have read them.
You might also like
- AnimationData structures
LRU Cache Animation
A hash map beside a doubly linked list: hits fly to the front, the oldest node falls away. The O(1) LRU cache, ready for your lessons, slides and posts
Free - AnimationData structures
Union-Find Animation
A glowing forest where groups merge and paths flatten: Union-Find with union by size and path compression, ready for your lessons, slides and posts
Free




