A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
Trie() Initializes the trie object.
void insert(String word) Inserts the string word into the trie.
boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.
word and prefix consist only of lowercase English letters.
At most 3 * 104 calls in total will be made to insert, search, and startsWith.
ac
classTrie {privateTrieNode root; /** Initialize your data structure here. */publicTrie() { root =newTrieNode(); } /** Inserts a word into the trie. */publicvoidinsert(String word) {root.insert(word,0); } /** Returns if the word is in the trie. */publicbooleansearch(String word) {TrieNode node =root.find(word,0);return node !=null&&node.hasWord; } /** Returns if there is any word in the trie that starts with the given prefix. */publicbooleanstartsWith(String prefix) {TrieNode node =root.find(prefix,0);return node !=null; }}classTrieNode {privateTrieNode[] children;publicboolean hasWord;publicTrieNode() { children =newTrieNode[26]; hasWord =false; }publicvoidinsert(String word,int index) {if (index ==word.length()) {this.hasWord=true;return; }int pos =word.charAt(index) -'a';if (children[pos] ==null) { children[pos] =newTrieNode(); } children[pos].insert(word, index +1); }publicTrieNodefind(String word,int index) {if (index ==word.length()) returnthis;int pos =word.charAt(index) -'a';if (children[pos] ==null) returnnull;return children[pos].find(word, index +1); }}/** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */
more intuitive:
classTrie {TrieNode root; /** Initialize your data structure here. */publicTrie() { root =newTrieNode(); } /** Inserts a word into the trie. */publicvoidinsert(String word) {TrieNode r = root;for (int i =0; i <word.length(); i++) {int pos =word.charAt(i) -'a';if (r.next[pos] ==null) r.next[pos] =newTrieNode(); r =r.next[pos]; }r.isWord=true; } /** Returns if the word is in the trie. */publicbooleansearch(String word) {TrieNode r = root;for (int i =0; i <word.length(); i++) {int pos =word.charAt(i) -'a';if (r.next[pos] ==null) returnfalse; r =r.next[pos]; }returnr.isWord; } /** Returns if there is any word in the trie that starts with the given prefix. */publicbooleanstartsWith(String prefix) {TrieNode r = root;for (int i =0; i <prefix.length(); i++) {int pos =prefix.charAt(i) -'a';if (r.next[pos] ==null) returnfalse; r =r.next[pos]; }// prefix is wordif (r.isWord) returntrue;// iterate next[]for (TrieNode t :r.next) {if (t !=null) returntrue; }returnfalse; }}classTrieNode {TrieNode[] next;boolean isWord;publicTrieNode() { next =newTrieNode[26]; }}/** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */