Data Structures · Visualization

Trie: Introduction

Trie

A trie (pronounced 'try', from reTRIEval) is a tree built specifically for storing strings, where each edge represents a single character and each path from the root spells out a prefix. Time: O(L) for insert/search/prefix-check, where L is the word's length. Space: O(total characters stored across all words).

Big-O: Lookup O(word length), independent of the number of words.

Trie: Introduction

Trie

A trie (pronounced 'try', from reTRIEval) is a tree built specifically for storing strings, where each edge represents a single character and each path from the root spells out a prefix. Words that share a common beginning share the same path in the tree, which makes tries extremely efficient for prefix-based lookups like autocomplete. Each node also has a flag marking whether a complete word ends there.

How Trie: Introduction Works

  1. Start with a root node representing an empty string.
  2. For each word, walk down the tree one character at a time, following (or creating) a child node for each character.
  3. Mark the final node of a word as 'end of word'.
  4. Words sharing a prefix automatically share the same path from the root.

Trie: Introduction Worked Example

Input: Insert 'cat' and 'car' into an empty trie

  1. root -> c -> a -> t (mark 't' as end of word)
  2. Insert 'car': root -> c -> a already exist, reuse them
  3. root -> c -> a -> r (new branch, mark 'r' as end of word)

Output: root - c - a - t (end) \ r (end)

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Trie: Introduction
A tree where each path from the root spells out a word one character per edge — words sharing a prefix share the same starting path.
root
root
c
a
t
r
t
d
o
g
n
e
Active path End of word
Trie: Introduction
Press ▶ Run to watch it step by step.
Python · Tries in Code
class TrieNode:
    def __init__(self):
        self.children = {}   # char -> node
        self.is_end = False

# Each node is a map of children plus an
# end-of-word flag. Prefixes are SHARED.
# example: intro

Trie: Introduction Time & Space Complexity

TimeO(L) for insert/search/prefix-check, where L is the word's length
SpaceO(total characters stored across all words)

When to Use Trie: Introduction

  • Autocomplete and search-as-you-type suggestions
  • Spell checkers
  • IP routing tables (longest prefix matching)
  • Word games like Boggle or Scrabble helpers

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.