In computer science, a trie, also called digital tree or prefix tree, is a kind of search tree—an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are not necessarily associated with every node. Rather, values tend to be associated with leaves, and with some inner nodes that correspond to keys of interest.
In the example shown, keys are listed in the nodes and values below them. Each complete English word has an arbitrary integer value associated with it. When the trie is traversed from the root towards a particular key, the associated value is retrieved by concatenating the letters on the edges traversed.
A trie can be seen as a tree-shaped deterministic finite automaton. A deterministic finite automaton is built from a set of states Q and a set of input symbols Σ, where Σ is the alphabet of the trie. The automaton has a start state s, a set of final states F, and a transition function that maps each state and input symbol to a new state. In a trie, the set of final states is generally the set of all strings.
The transition function for a trie is defined as follows: given a state s and an input symbol a, the new state is obtained by descending from the state s along the edge labeled with the symbol a. If there is no edge labeled with the symbol a, then the new state is the failure state. In a trie, the failure state is generally the root.
A trie can be seen as a special case of an N-ary tree in which each node has at most N children, where N is the number of symbols in the alphabet. The path from the root to any leaf describes a prefix of the string associated with that leaf.
A trie can be implemented as an N-ary tree, a radix tree, a digital search tree, a digital tree, or a Patricia tree.
An N-ary tree is a rooted tree in which each node has no more than N children. A trie with N symbols in its alphabet can be seen as an N-ary tree in which each node has at most N children.
In the example shown, the keys are listed in the nodes and the values are below them. Each complete English word has an arbitrary integer value associated with it. When the trie is traversed from the root towards a particular key, the associated value is retrieved by concatenating the letters on the edges traversed.
A trie can be seen as a special case of an N-ary tree in which each node has at most N children, where N is the number of symbols in the alphabet. The path from the root to any leaf describes a prefix of the string associated with that leaf.
A radix tree or compressed tree is a kind of search tree in which the keys are strings. The tree is compressed by storing only the differences between successive keys. For example, if the keys are the words "Hello", "World", and "Hell", the root node will have the letters "H" and "W", and the first child of the root will have the letter "o". The second child of the root will have the letters "r" and "l".
In the example shown, the keys are listed in the nodes and the values are below them. Each complete English word has an arbitrary integer value associated with it. When the trie is traversed from the root towards a particular key, the associated value is retrieved by concatenating the letters on the edges traversed.
A radix tree can be seen as a special case of an N-ary tree in which each node has at most N children, where N is the number of symbols in the alphabet. The path from the root to any leaf describes a prefix of the string associated with that leaf.
A digital search tree or digital tree is a kind of search tree in which the keys are strings. The tree is compressed by storing only the differences between successive keys. For example, if the keys are the words "Hello", "World", and "Hell", the root node will have the letters "H" and "W", and the first child of the root will have the letter "o". The second child of the root will have the letters "r" and "l".
In the example shown, the keys are listed in the nodes and the values are below them. Each complete English word has an arbitrary integer value associated with it. When the trie is traversed from the root towards a particular key, the associated value is retrieved by concatenating the letters on the edges traversed.
A digital tree can be seen as a special case of an N-ary tree in which each node has at most N children, where N is the number of symbols in the alphabet. The path from the root to any leaf describes a prefix of the string associated with that leaf.
A Patricia tree or radix tree is a kind of search tree in which the keys are strings. The tree is compressed by storing only the differences between successive keys. For example, if the keys are the words "Hello", "World", and "Hell", the root node will have the letters "H" and "W", and the first child of the root will have the letter "o". The second child of the root will have the letters "r" and "l".
In the example shown, the keys are listed in the nodes and the values are below them. Each complete English word has an arbitrary integer value associated with it. When the trie is traversed from the root towards a particular key, the associated value is retrieved by concatenating the letters on the edges traversed.
A Patricia tree can be seen as a special case of an N-ary tree in which each node has at most N children, where N is the number of symbols in the alphabet. The path from the root to any leaf describes a prefix of the string associated with that leaf.
Tries are used in many applications, such as:
Compression: Tries can be used to compress strings by storing strings that have a common prefix in the same node. For example, the string "aaaabbbbccccdddd" can be compressed to "4a4b4c4d".
Spelling correction: Tries can be used to store a dictionary of words and then used to check if a given word is spelled correctly.
IP address lookup: Tries can be used to store a list of IP addresses and then used to lookup the IP address of a given host.
DNA sequence analysis: Tries can be used to store DNA sequences and then used to find the matching DNA sequence for a given query.