Skip to content
Multiple

Trie-Based String Search

Building and searching prefix trees.

Time Complexity
O(M)
Space Complexity
O(M * alphabet)

Interactive Visualization

Playback Speed
Execution State
STEP 1 / 16
Inserting Words: CAR, CAT, CART
R
Process Description

Inserting word: "CAR"

Technical Guide

A Trie (Prefix Tree) is an ordered tree used to store a dynamic set of strings. Unlike a hash table, it allows for prefix-based searches (e.g., 'find all words starting with CAT'). Each node represents a single character, and the path from the root to a node represents a prefix. It is highly efficient for dictionaries and autocomplete systems.

Logical Phases

  • 1
    Starting at the root for every new word.
  • 2
    Moving down the tree if the character node exists.
  • 3
    Creating a new node if the character is missing.
  • 4
    Marking the final node as the 'end of word'.

Implementation

curr = curr.children[char] ||= new Node();

Expert Pitfalls

Standard Tries can be very sparse and memory-intensive; use Radix Tries for optimization.