Write a tree
The dictionary tree can be implemented in many ways. Some of the words can be used to search for the word set in the dictionary. These words may be a little different from the words to be searched, other methods can be used to find a word that exactly matches the target word. In this example, the dictionary tree can only find exactly matched words and count the number of words with the same prefix. The implementation here uses pseudocode, because different encodings may use different programming languages.
We will write the following four functions:
- Addword. This function will add a separateWordTo the dictionary.
- Contpreffixes. This function calculates the number of words with the same prefix as a string in the dictionary.
- Contwords. This function will calculate the exact match between the dictionary and the given wordWord.
- Our dictionary tree supports only English letters.
We also need to write a struct. Some of its attributes will indicate the values stored on this node. Because we want to know the number of words matching a given string, each node must have an attribute to determine whether this node represents a word or a prefix (for simplicity, A complete word is also considered as a prefix) and how many words in this dictionary contain this prefix (which may be repeated in the dictionary ). This task can be solved with an integer property value.
Because we want to know how many words have a given prefix string, we need another integer property value to record how many words have the same prefix on this node. At the same time, each node must be able to access all of its potential son nodes (26 ). After knowing these details, our struct should have the following members:
Structure trie <br/> integer words; <br/> integer prefixes; <br/> reference edges [26];
We also need the following functions:
Initialize (vertex) <br/> addword (vertex, word); <br/> integer countprefixes (vertex, prefix); <br/> integer countwords (vertex, word );
First, we must use the following function to initialize all nodes:
Initialize (vertex) <br/> vertex. words = 0 <br/> vertex. prefixes = 0 <br/> for I = 0 to 26 <br/> edges [I] = noedge
The addword function consists of two parameters: the node to be inserted and the word to be added. The idea of this function is to use that stringWordAdded to NodeVertex, We will add thisWordRemove the leftmost character of the word from the corresponding node branch. If the branch does not exist, we should add it. All topcoder programming languages can simulate the operation of deleting a character within a constant time instead of creating an original string or moving other characters.
Addword (vertex, word) <br/> If isempty (Word) <br/> vertex. words = vertex. words + 1 <br/> else <br/> vertex. prefixes = vertex. prefixes + 1 <br/> K = firstcharacter (Word) <br/> If (notexists (edges [k]) <br/> edges [k] = createedge () <br/> initialize (edges [k]) <br/> cutleftmostcharacter (Word) <br/> addword (edges [K], word)
Countwords and countprefixes functions are similar. If we look for an empty string, we only need to return the number of words or prefixes associated with this node. If we are looking for a non-empty string, we should look for a corresponding subtree in the tree, but if this subtree does not exist, then we have to return 0.
Countwords (vertex, word) <br/> K = firstcharacter (Word) <br/> If isempty (Word) <br/> return vertex. words <br/> else if notexists (edges [k]) <br/> return 0 <br/> else <br/> cutleftmostcharacter (word) <br/> return countwords (edges [K], word );
Countprefixes (vertex, prefix) <br/> K = firstcharacter (prefix) <br/> If isempty (Word) <br/> return vertex. prefixes <br/> else if notexists (edges [k]) <br/> return 0 <br/> else <br/> cutleftmostcharacter (prefix) <br/> return countwords (edges [K], prefix)