Trie prefix tree

트라이 접두사 트리

Share prefix nodes and follow one character at a time.

···
html
<div class="v"><header><b>PREFIX TRIE</b><span id="state"></span></header><main><div class="query">prefix <b id="prefix">c</b></div><div class="words" id="words"></div></main><footer>shared prefix narrows matches</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,4.5vmin,19px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.45} .query{text-align:center;height:35%;padding-top:8px;color:var(--muted)}.query b{color:var(--accent);font:700 1.3em ui-monospace,monospace}.words{display:flex;justify-content:center;gap:6px;align-items:center;height:56%}.word{padding:7px;border:1px solid var(--line);border-radius:6px;font:600 .9em ui-monospace,monospace}.word em{font-style:normal;color:var(--accent)}
js
let n=0;const prefixes=["c","ca","car"];function draw(){const p=prefixes[n];document.getElementById("prefix").textContent=p;document.getElementById("words").innerHTML=["car","cart","cat"].map(w=>"<div class=\"word "+(w.startsWith(p)?"":"muted")+"\"><em>"+w.slice(0,p.length)+"</em>"+w.slice(p.length)+"</div>").join("");document.getElementById("state").textContent="match "+[3,3,2][n];n=(n+1)%3}draw();setInterval(draw,1100)

A trie stores words as character paths. Words with the same prefix share their leading nodes.

Lookup follows one character at a time, and a terminal marker distinguishes a complete word. Autocomplete collects endings below a prefix node, but broad prefixes need result limits.

When to use

Use it for prefix search, autocomplete, and routing rules.

Open as page ↗