Complexity Analysis
In the introduction, you can find that the complexity of searching and inserting a word is linear, but we have not completed this analysis. Lowering a layer in a tree during insertion and search can be completed within a constant time. At the same time, each time this program lowers a layer, it will remove a character from the string; we can conclude that each function accesses the L layer in the tree. Each function accesses the next layer in the tree, which is a constant time, then, you can insert and search for a word in the dictionary tree at O (l) time. The memory used in this dictionary tree is determined by the storage edge and the number of words with the same prefix.
Other types of dictionary trees
We use the dictionary tree to store words with lower-case letters, but the dictionary tree can also store many other things. We can use digits or bytes to replace lowercase letters. Each data structure can exist in this tree, not just strings. Use the dictionary tree to make your imagination fly. For example, if you want to find a word with a deleted letter in the dictionary. You can modify countwords to the following format:
Countwords (vertex, word, missingletters) <br/> K = firstcharacter (Word) <br/> If isempty (Word) <br/> return vertex. word <br/> else if notexists (edges [k]) and missingletters = 0 <br/> return 0 <br/> else if notexists (edges [k]) <br/> cutleftmostcharacter (Word) <br/> return countwords (vertex, word, missingLetters-1) <br/> here we cut a character but we don't go lower in the tree <br/> else <br/> we are adding the two possibilities: the first <br/> character has been deleted plus the first character is present <br/> r = countwords (vertex, word, missingLetters-1) <br/> cutleftmostcharacter (word) <br/> r = R + countwords (edges [K], word, missingletters) <br/> return r <br/>
This function may be more complex than the initial one, but it must judge all the substrings of a word faster.
Exercise
Wordfind SRM 232
Searchbox SRM 361
Cyclicwords SRM 358
Tagalogdictionary SRM 342
Joinedstring SRM 302
Cmpdwords SRM 268
Scruffle 2007 TCCC marathon online Round 1
PS: Because I am really not good at it, please point it out if the translation is inappropriate.