One: Concept
Below we have and,as,at,cn,com these keywords, then how to build trie tree?
From the above figure, we can find some interesting features more or less.
First: The root node does not contain characters, and each child node outside of the root node contains a single character.
Second: From the root node to a node, the characters that pass through the path are concatenated, which is the corresponding string for that node.
Third: The common prefix for each word is saved as a character node.
Second: scope of Use
Now that we learn trie tree, we must know what this thing is for.
First: Word frequency statistics.
May be someone to say, the word frequency statistics simple ah, a hash or a heap can be finished, but the problem comes, if the memory is limited? can still be so
Do you play? So here we can use the trie tree to compress the space, because the public prefix is saved with a node.
Second: prefix matching
Take the above picture, if I want to get all the strings starting with "a", it is obvious that: And,as,at, if not trie tree,
What are you going to do about it? It is clear that the simple practice of Time complexity O (N2), then the trie tree is not the same, it can do h,h for you to retrieve the length of the word,
It can be said that this is the effect of the second kill.
Related Topics:
Implement Trie
Implement a trie with insert , search , and startsWith methods.
Can be implemented with HashMap.
Implement Trie hashmap version
Trie Tree is a 26-fork tree, then you can actually use an array, and then use each letter corresponding to index.
At the same time function can be written recursively
Index is required for recursion, then index needs to re-write an insert function and find function. Note that these two methods are defined inside the class!
(The class defines the function recursively, writes several times)
classTrienode {trienode[] children; BooleanIsword =false; //Initialize your data structure here. PublicTrienode () { This. Children =NewTrienode[26]; } PublicTrienode (CharMyChar) { This. Children =NewTrienode[26]; } Public voidInsert (String Word,intindex) { if(word.length () = =index) { This. Isword =true; return; } intpos = Word.charat (Index)-' a '; if(Children[pos] = =NULL) {Children[pos]=NewTrienode (); } children[pos].insert (Word, index+ 1); } PublicTrienode Search (String Word,intindex) { if(Index = =word.length ()) { return This; } intpos = Word.charat (Index)-' a '; if(Children[pos] = =NULL) { return NULL; } returnChildren[pos].search (Word, index + 1); }} Public classTrie {PrivateTrienode Root; PublicTrie () {root=NewTrienode (); } //inserts a word into the trie. Public voidInsert (String Word) {Root.insert (Word,0); } //Returns If the word is in the trie. Public BooleanSearch (String word) {Trienode res= Root.search (Word, 0); if(res = =NULL|| Res.isword = =false) { return false; } return true; } //Returns If there is any word in the trie//That's starts with the given prefix. Public BooleanstartsWith (String prefix) {return(Root.search (prefix, 0)! =NULL); }}trie Tree
Notes:this refers to the current object itself.
This reference should be added when you want to explicitly indicate a variable or function that uses the object's own in a class.
------Split Line-----
Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must is constructed from letters of sequentially adjacent cell, where "adjacent" cells is those horizontally or Vertically neighboring. The same letter cell is used more than once in a word.
For example,
Given words = and ["oath","pea","eat","rain"] board =
[ [' O ', ' a ', ' a ', ' n '], [' e ', ' t ', ' a ', ' e '], [' I ', ' h ', ' K ', ' R '], [' I ', ' f ', ' l ', ' V ']]
Return ["eat","oath"] .
Violent methods for a given words array to do DFS for each word, the optimization method is to create a dictionary tree on the basis of word search, putting the results found in the DFS process down to the result array.
Data structure Note part3 (dictionary tree && Scan line)