Code with text, representing a trie data structure.

A tree structure built for storing and searching text.

What a trie is

A trie is a tree-like data structure used to store a set of strings, organizing them by their characters. The name comes from the word 'retrieval,' reflecting its purpose. In a trie, the path from the top down to a point spells out a string, and strings that share a beginning share the same early path. This structure makes a trie especially good at working with text and prefixes.

How a trie is organized

In a trie, each position represents a character, and following a path through the structure builds up a string one character at a time. Strings that start with the same sequence of characters share the same initial portion of the tree, branching apart only where they begin to differ. This character-by-character organization is the core idea that distinguishes a trie from other ways of storing strings.

Why shared prefixes help

Because strings with a common beginning share the same path in a trie, the structure naturally groups related strings together. This makes it efficient to find all strings that start with a given prefix, since you simply follow the path for that prefix and then explore from there. The sharing of prefixes also means common beginnings are not stored repeatedly, which can be efficient.

What tries are used for

Tries are well suited to tasks that involve searching text by prefix. A common example is autocomplete, where typing the start of a word should quickly suggest matching options. Tries are also used in dictionaries and other situations where fast lookups and prefix matching on strings are important. Their structure makes these operations natural and efficient.

Tries compared to other structures

A trie offers a different approach from structures like hash tables, which also store and look up data. While a hash table is excellent for checking whether an exact key exists, a trie shines when prefixes matter, letting you find all entries that begin a certain way. Choosing a trie makes sense when prefix-based searching is a central need, playing to the structure's strengths.

Why it matters

Tries provide an efficient way to store and search strings, especially by prefix. Understanding what a trie is - a tree that organizes strings character by character so shared prefixes share a path - clarifies how features like autocomplete can work quickly. For anyone learning about data structures, the trie is a useful concept to know.

See also: What is a binary tree? Explained, What is a data structure? Explained.

Sources

Published date reflects the original event date (2023-07-11). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.

Skillo Staff

Skillo Staff

All stories by: Skillo Staff

Leave a Reply