pytrie is a pure Python implementation of the trie (prefix tree) data structure.
A trie is an tree data structure that is used to store a mapping where the keys are sequences, usually strings over an alphabet. In addition to implementing the mapping interface, tries facilitate finding the items for a given prefix, and vice versa, finding the items whose keys are prefixes of a given key K. As a common special case, finding the longest-prefix item is also supported.
Algorithmically, tries are more efficient than binary search trees (BSTs) both in lookup time and memory when they contain many keys sharing relatively few prefixes. Unlike hash tables, trie keys don’t need to be hashable. In the current implementation, a key can be any finite iterable with hashable elements.
>>> from pytrie import SortedStringTrie as trie
>>> t = trie(an=0, ant=1, all=2, allot=3, alloy=4, aloe=5, are=6, be=7)
>>> t
{'all': 2, 'allot': 3, 'alloy': 4, 'aloe': 5, 'an': 0, 'ant': 1, 'are': 6, 'be': 7}
>>> t.keys(prefix='al')
['all', 'allot', 'alloy', 'aloe']
>>> t.items(prefix='an')
[('an', 0), ('ant', 1)]
>>> t.longest_prefix('antonym')
'ant'
>>> t.longest_prefix_item('allstar')
('all', 2)
>>> t.longest_prefix_value('area', default='N/A')
6
>>> t.longest_prefix('alsa')
Traceback (most recent call last):
...
KeyError
>>> t.longest_prefix_value('alsa', default=-1)
-1
>>> list(t.iter_prefixes('allotment'))
['all', 'allot']
>>> list(t.iter_prefix_items('antonym'))
[('an', 0), ('ant', 1)]
Bases: UserDict.DictMixin, object
Base trie class.
As with regular dicts, keys are not necessarily returned sorted. Use SortedTrie if sorting is required.
Create a new trie.
Parameters are the same with dict().
Create a new trie with keys from iterable and values set to value.
Parameters are the same with dict.fromkeys().
Callable for forming a key from its parts.
alias of tuple
Bases: pytrie.Trie
A more appropriate for string keys Trie.
Bases: pytrie.Trie
A Trie that returns its keys (and associated values/items) sorted.
Note
This implementation does not keep the keys sorted internally; instead it sorts them every time a method returning a list or iterator (e.g. keys()) is called. In cases where a trie is relatively stable (few inserts/deletes) and is iterated often, it is probably more efficient to use a NodeFactory based on a sorted dict such as sorteddict.
Bases: pytrie.SortedTrie, pytrie.StringTrie
A Trie that is both a StringTrie and a SortedTrie.
The following methods are specific to tries; they are not part of the mapping API.
Return the longest key in this trie that is a prefix of key.
Return the value associated with the longest key in this trie that is a prefix of key.
Return the item ((key,value) tuple) associated with the longest key in this trie that is a prefix of key.
The following methods extend the respective mapping API methods with an optional prefix parameter. If not None, only keys (or associated values/items) that start with prefix are returned.
Return a list of this trie’s keys.
Parameter: | prefix – If not None, return only the keys prefixed by prefix. |
---|
Return a list of this trie’s values.
Parameter: | prefix – If not None, return only the values associated with keys prefixed by prefix. |
---|
Return a list of this trie’s items ((key,value) tuples).
Parameter: | prefix – If not None, return only the items associated with keys prefixed by prefix. |
---|
Return an iterator over this trie’s keys.
Parameter: | prefix – If not None, yield only the keys prefixed by prefix. |
---|
Return an iterator over this trie’s values.
Parameter: | prefix – If not None, yield only the values associated with keys prefixed by prefix. |
---|
Return an iterator over this trie’s items ((key,value) tuples).
Parameter: | prefix – If not None, yield only the items associated with keys prefixed by prefix. |
---|
The following methods have the standard mapping signature and semantics.
Tries are implemented as trees of Node instances. You don’t need to worry about them unless unless you want to extend or replace Node with a new node factory and bind it to Trie.NodeFactory.
Bases: object
Trie node class.
Subclasses may extend it to replace ChildrenFactory with a different mapping class (e.g. sorteddict).
Variable value: | The value of the key corresponding to this node or NULL if there is no such key. |
---|---|
Variable children: | |
A {key-part : child-node} mapping. |
A callable for creating a new children mapping.
alias of dict