|
Structures, Machines
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. more...
Home
Asia
Australia
Br. Comm. Other
Canada
Europe
Latin America
Middle East
Publications & Supplies
Topical & Specialty
Animation, Cartoons
Cinderellas & Fakes
Nature
Other
People, Sports
Structures, Machines
Transport, Space
World Errors, Freaks,...
UK (Great Britain)
United States
Worldwide
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." Due to this etymology it is pronounced ("tree"), although some encourage the use of ("try") or the use of ("tree-ay") in order to distinguish it from the more general tree.
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.
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.;
Applications
As replacement of other data structures
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:
Read more at Wikipedia.org
• [List your site here Free!]
|
|