Non-Linear Data StructuresIntermediate LevelStudy Time: 20 mins

Trie (Prefix Tree) Data Structure

A Trie (Prefix Tree) is an $N$-ary tree structure designed for fast character prefix string searching, powering search engine autocomplete systems.

Loading visualizer workspace...

Trie Word Insertion Step-by-Step

Sample Input:Insert Word: 'cat'
Step 1

Branch Character 'c'

Start at Root node. Check root.children['c']. If null, create new TrieNode for letter 'c'.

Root -> Node('c')
Each node represents a single character link in the prefix chain.
Step 2

Branch Character 'a'

Move to Node('c'). Check Node('c').children['a']. Create new TrieNode for letter 'a'.

Root -> Node('c') -> Node('a')
Prefix 'ca' is now shared across any words starting with 'ca' (e.g. 'cat', 'car', 'cap').
Step 3

Branch Character 't' and Set isEnd Flag

Move to Node('a'). Create Node('t') and set isEnd = true to mark end of word 'cat'.

Root -> Node('c') -> Node('a') -> Node('t') (isEnd = true)
Setting isEnd = true distinguishes complete words from partial prefixes.

Real-world analogy: Dictionary Index Paths

Prefix Branching
Hover over items for details

To find 'cat', you follow letter branch 'c', then branch 'a', then branch 't'. Words sharing the same prefix share the same tree path.

  • Prefix Branching= Each node contains children references for letters 'a' through 'z'.

Operations explained

Complexity analysis

Scenario Simulator: Dataset Size (N = 500)
Presets:
N = 10N = 10,000
Complexity Curve MapTrie (Prefix Tree): O(log N) Tree Height Time
Time (t)Size N (500)N = 500O(N²)O(N log N)O(N)O(log N)O(1)Trie (Prefix Tre
Time Steps (N=500)

~9 steps (Logarithmic)

O(log N) Tree Height Time

Space Footprint

O(N) Tree Nodes Space

Memory growth rate as N expands.

Performance Rank
Optimal Tier

Relative efficiency rating for large N.

OperationTime ComplexitySpace Complexity
Search Word / PrefixO(L)O(1)
Insert WordO(L)O(L)

Code implementations

class TrieNode {
  constructor() { this.children = {}; this.isEnd = false; }
}

Real-world applications

Case 01

Search Autocomplete

Suggests query completions as users type letters.

Case 02

Spell Checking

Validates dictionary words rapidly.

Interview questions

Topic Knowledge Test & Assessment

10 Questions Assessment

Trie Knowledge & Skill Test

Validate your conceptual understanding, operation mechanics, and core concepts of Trie.