Computer ScienceData StructuresMedium

Trie (data structure)

Also known as:Prefix treeDigital treeRadix tree

A trie (also called a prefix tree or digital tree) is a tree-like data structure used to store strings where each node represents a single character of a key, and paths from root to leaf spell out complete keys. The root represents an empty string, and each edge corresponds to a character. Tries are highly efficient for prefix-based search operations, making them ideal for autocomplete systems, spell checkers, and IP routing tables.

Trie vs Hash Table vs BST for String Operations

OperationTrieHash TableBalanced BST
SearchO(L)O(L) avgO(L log n)
InsertO(L)O(L) avgO(L log n)
DeleteO(L)O(L) avgO(L log n)
Prefix SearchO(L + k)O(n·L)O(L log n + k)
SpaceO(ALPHABET × n × L)O(n)O(n)

Interactive Tools

Visualgo — Trie

Open Tool

CS USF — Trie Visualization

Open Tool

Brilliant.org — Tries

Open Tool
A trie data structure storing words like "to", "tea", "ten", "in", and "inn"

Wikimedia Commons, CC BY-SA

Related Terms

The word "trie" was coined by Edward Fredkin in 1960, derived from the middle letters of "retrieval." Despite being spelled "trie," it is commonly pronounced "try" to distinguish it from "tree," reflecting its purpose as a retrieval data structure.

trieprefix-treestringssearchdata-structureautocomplete