https://leetcode.com/problems/implement-trie-prefix-tree/
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume this all inputs is consist of lowercase letters a-z
.
Problem Solving Ideas:
Https://en.wikipedia.org/wiki/Trie
First review, what is called Trie tree. Each node of the trie tree is typically used to hold characters, the root node is empty, and the pointer position of each parent node below the child node is determined by the character value represented by the descendant node. Therefore, in the trie tree, there is usually no value in this field.
Thus, for a node in the trie tree, all its child nodes have the same string prefix. As you can see, the trie tree is not a binary tree because, for any node, it may have 26 child nodes, or more (in case of not qualifying to lowercase letters, or other characters).
Trie trees are often used for string searches, or for string input prompts when searching.
In this subject, the search method is used to find the string that has been inserted into the trie tree. It is important to note that not only the path to the leaf node is the inserted string word, it is also possible to insert ABCD First and then insert ABC. So, for each node, a Boolean value is required to indicate whether the node is a word or just a prefix.
You can see that the search in the trie tree, or a string, is always started from root. There is no word from any node to any node.
Trie tree can be implemented with trienode[] or hashmap. The problem with the former is that there may be a lot of oh sparse matrix.
The following is an array of implementation, with the above ideas, the code is still relatively clear.
classTrienode {//Initialize your data structure here. BooleanIsword; Trienode[] Next; PublicTrienode () {Next=NewTrienode[26]; Isword=false; }} Public classTrie {PrivateTrienode Root; PublicTrie () {root=NewTrienode (); } //inserts a word into the trie. Public voidInsert (String word) {Trienode cur=Root; for(inti = 0; I < word.length (); i++) { if(Cur.next[word.charat (i)-' a '] = =NULL) {Trienode next=NewTrienode (); Cur.next[word.charat (i)-' a '] =Next; } cur= Cur.next[word.charat (i)-' a ']; } Cur.isword=true; } //Returns If the word is in the trie. Public BooleanSearch (String word) {Trienode cur=Root; for(inti = 0; I < word.length (); i++) {cur= Cur.next[word.charat (i)-' a ']; if(cur = =NULL) { return false; } } returnCur.isword; } //Returns If there is any word in the trie//That's starts with the given prefix. Public BooleanstartsWith (String prefix) {trienode cur=Root; for(inti = 0; I < prefix.length (); i++) {cur= Cur.next[prefix.charat (i)-' a ']; if(cur = =NULL) { return false; } } return true; }}//Your Trie object would be instantiated and called as such://Trie Trie = new Trie ();//Trie.insert ("somestring");//trie.search ("key");
As with HashMap, the code is as follows
classTrienode {//Initialize your data structure here. BooleanIsword; Map<character, trienode>Next; PublicTrienode () {Next=NewHashmap<character, trienode>(); Isword=false; }} Public classTrie {PrivateTrienode Root; PublicTrie () {root=NewTrienode (); } //inserts a word into the trie. Public voidInsert (String word) {Trienode cur=Root; for(inti = 0; I < word.length (); i++) { if(Cur.next.get (Word.charat (i)) = =NULL) {Trienode next=NewTrienode (); Cur.next.put (Word.charat (i), next); } cur=Cur.next.get (Word.charat (i)); } Cur.isword=true; } //Returns If the word is in the trie. Public BooleanSearch (String word) {Trienode cur=Root; for(inti = 0; I < word.length (); i++) {cur=Cur.next.get (Word.charat (i)); if(cur = =NULL) { return false; } } returnCur.isword; } //Returns If there is any word in the trie//That's starts with the given prefix. Public BooleanstartsWith (String prefix) {trienode cur=Root; for(inti = 0; I < prefix.length (); i++) {cur=Cur.next.get (Prefix.charat (i)); if(cur = =NULL) { return false; } } return true; }}//Your Trie object would be instantiated and called as such://Trie Trie = new Trie ();//Trie.insert ("somestring");//trie.search ("key");
Implement Trie (Prefix Tree)