The dictionary tree (Trie) can hold the corresponding relationship of some string--value. Basically, it's the same as Java's HashMap function, all key-value mappings, except that the Trie key can only be a string.
The strength of Trie lies in its time complexity. Its insertion and query time complexity are O (k), where k is the length of the key, regardless of how many elements are stored in the Trie. The hash table is called O (1), but in the calculation of the hash will certainly be O (k), and there are collisions and other problems; The disadvantage of Trie is the high space consumption.
As for the implementation of the Trie tree, you can use the array, you can also use the pointer dynamic allocation, I do the problem in order to facilitate the use of arrays, static allocation of space.
Trie tree, also known as the word search tree or key tree, is a tree-shaped structure, is a hash tree variant. Typical applications are used to count and sort large numbers of strings (but not limited to strings), so they are often used by search engine systems for text frequency statistics. It has the advantage of minimizing unnecessary string comparisons and querying efficiencies over hash tables.
The core idea of trie is space change time. Use the common prefix of a string to reduce the cost of query time to achieve the purpose of increasing efficiency.
Each word in the trie tree is stored by the character by character method, and the same prefix word shares the prefix node.
As you can see, each path consists of a single word. The tree above To/tea/ted/ten/inn these words.
The basic properties of the trie tree can be summed up as:
(1) The root node does not contain characters, and the root node unexpectedly contains only one character per node.
(2) from the root node to a node, the characters that pass through the path are concatenated to the corresponding string for that node.
(3) All child nodes of each node contain different strings.
Properties
(1) The root node does not contain characters, and each node outside the root node contains only one character.
(2) from the root node to a node, the characters that pass through the path are concatenated to the corresponding string for that node.
(3) All child nodes of each node contain different strings.
The basic idea (take the letter tree as an example):
1. Insert procedure
For a word, starting from the root, walk down the node in the tree corresponding to each letter of the word, until the word is traversed, and the last node is marked red, indicating that the word has been inserted into the trie tree.
2. Inquiry process
Similarly, from the root begins to traverse the trie tree in alphabetical order of the word, once it is found that a node tag does not exist or the word traversal is complete and the last node is not marked red, then the word does not exist, if the last node is marked red, indicates that the word exists.
Application
(1) Word frequency statistics
Space-saving than using hash directly
(2) Search tips
A word that prompts you when you enter a prefix
(3) as an auxiliary structure
Auxiliary structures such as suffix trees, ac automata, etc.
Realize
Although Python does not have pointers, you can use nested dictionaries to implement tree structures. For non-ASCII words, unify the Unicode encoding to insert and search.
#coding =utf-8 class Trie: root = {} END = '/' def add (self, word): #从根节点遍历单词, char by Char, if not present, adds Finally add a Word end flag node = Self.root for c in Word: node=node.setdefault (c,{}) node[self. END] = None def find (self, word): node = self.root for c in Word: if C isn't in node: return false< C19/>node = Node[c] return self. END in Node