Trie
From Wikipedia, the free encyclopedia
In computer science, a trie, or prefix tree, is an ordered tree data structure that is used to store an 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 shows what key it is associated with. All the descendants of any one node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are normally not associated with every node, only with leaves and some inner nodes that happen to correspond to keys of interest.
The term trie comes from "retrieval." Despite this etymology, it is pronounced [traI] ("try")[1] and not [tri] ("tree"), as the pronunciation of "retrieval" suggests.
In the example shown, keys are listed in the nodes and values below them. Each complete English word has an integer value associated with it. A trie can be seen as a deterministic finite automaton, although the symbol on each edge is often implicit in the order of the branches.
It is not necessary for keys to be explicitly stored in nodes. (In the figure, words are shown only to illustrate how the trie works.)
Though it is most common, tries need not be keyed by character strings. The same algorithms can easily be adapted to serve similar functions of ordered lists of any construct, e.g., permutations on a list of digits, permutations on a list of shapes, etc.
Contents |
[edit] Advantages and drawbacks, relative to binary search tree
The following are the main advantages of tries over binary search trees (BSTs):
- Looking up keys is faster. Looking up a key of length m takes worst case O(m) time. A BST takes O(log n) time, where n is the number of elements in the tree, because lookups depend on the depth of the tree, which is logarithmic in the number of keys if the tree is balanced. But in the worst case, both are as bad/good as O(log n), because log(n) will approach m in the worst case. Also, the simple operations tries use during lookup, such as array indexing using a character, are fast on real machines.
- Tries can require less space when they contain a large number of short strings, because the keys are not stored explicitly and nodes are shared between keys with common initial subsequences.
- Tries help with longest-prefix matching, where we wish to find the key sharing the longest possible prefix of characters all unique.
[edit] Applications
[edit] As replacement of other data structures
This section does not cite any references or sources. (September 2007) Please help improve this section by adding citations to reliable sources. Unverifiable material may be challenged and removed. |
As mentioned, a trie has a number of advantages over binary search trees (see Bentley & Sedgewick reference detailing a number of them). A trie can also be used to replace a hash table, over which it has the following advantages:
- Looking up data in a trie is faster in the worst case, O(m) time, compared to an imperfect hash table. An imperfect hash table can have key collisions. A key collision is the hash function mapping of different keys to the same position in a hash table. The worst-case lookup speed in an imperfect hash table is O(N) time, but far more typically is O(1), with O(m) time spent evaluating the hash.
- There are no collisions of different keys in a trie.
- Buckets in a trie which are analogous to hash table buckets that store key collisions are only necessary if a single key is associated with more than one value.
- There is no need to provide a hash function or to change hash functions as more keys are added to a trie.
- A trie can provide an alphabetical ordering of the entries by key.
Tries do have some drawbacks as well:
- Tries can be slower in some cases than hash tables for looking up data, especially if the data is directly accessed on a hard disk drive or some other secondary storage device where the random access time is high compared to main memory.
- It is not easy to represent all keys as strings, such as floating point numbers, which can have multiple string representations for the same floating point number, e.g. 1, 1.0, 1.00, +1.0, etc.
- Tries are frequently less space-efficient than hash tables.
[edit] Dictionary representation
A common application of a trie is storing a dictionary, such as one found on a mobile telephone. Such applications take advantage of a trie's ability to quickly search for, insert, and delete entries; however, if storing dictionary words is all that is required (i.e. storage of information auxiliary to each word is not required), a minimal acyclic deterministic finite automaton would use less space than a trie.
Tries are also well suited for implementing approximate matching algorithms, including those used in spell checking software.
[edit] Algorithms
The following pseudo-code represents the general algorithm for determining whether a given string is in a trie. Note that children
is map of a node's children; and we say that a "terminal" node is one which contains a valid word.
def isKeyInTrie(node, key):
if len(key) == 0: # base case: key is an empty string
return node.isTerminal() # we have a match if the node is terminal
c = key[0] # first character in key
if not node.children.hasKey(c): # no child node?
return False # key is not empty, so there is no match
child = node.children[c] # get child node
tail = key[1:] # the rest of the key after first character
return isKeyInTrie(child, tail) # recurse
[edit] Sorting
Lexicographic sorting of a set of keys can be accomplished with a simple trie-based algorithm as follows:
- Insert all keys in a trie.
- Output all keys in the trie by means of pre-order traversal, which results in output that is in lexicographically increasing order, or post-order traversal, which results in output that is in lexicographically decreasing order. Pre-order traversal and post-order traversal are kinds of depth-first traversal. In-order traversal is another kind of depth-first traversal that is more appropriate for outputting the values that are in a binary search tree rather than a trie.
This algorithm is a form of radix sort.
A trie forms the fundamental data structure of Burstsort; currently (2007) the fastest known, memory/cache-based, string sorting algorithm.
A parallel algorithm for sorting N keys based on tries is O(1) if there are N processors and the length of the keys have a constant upper bound. There is the potential that the keys might collide by having common prefixes or by being identical to one another, reducing or eliminating the speed advantage of having multiple processors operating in parallel.
[edit] Full text search
A special kind of trie, called a suffix tree, can be used to index all suffixes in a text in order to carry out fast full text searches.
[edit] See also
[edit] External links
- NIST's Dictionary of Algorithms and Data Structures: Trie
- Tries by Lloyd Allison
- Using Tries Topcoder tutorial
- An Implementation of Double-Array Trie
- de la Briandais Tree
- Discussing a trie implementation in Lisp
[edit] References
- ^ Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Section 6.3: Digital Searching, pp.492–512.
- de la Briandais, R. (1959), “File Searching Using Variable Length Keys”, Proceedings of the Western Joint Computer Conference: 295–298
- Fredkin, Edward (Sept. 1960), “Trie Memory”, Communications of the ACM 3 (9): 490-499, DOI 10.1145/367390.367400
- Cache-Efficient String Sorting Using Copying
- Bentley, Jon & Sedgewick, Bob (1998), “Ternary Search Trees”, Dr. Dobbs Journal, April 01, 1998, <http://www.ddj.com/windows/184410528>. Retrieved on 14 April 2008