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.
Search for the word’s first character among the root’s children.
For instance, with “AGRA” being the initial insertion, create a ‘A’ child for the root, followed by its descendants ‘G’, ‘R’, and ‘A’.
Proceed to the corresponding child node in the Trie and the next letter in the word if such a node is present.
For example, when inserting “AYODHYA” after ‘AGRA’, start at the root and locate the existing ‘A’ node. Moving to the ‘A’ node, search for ‘Y’ among its children. Absence of ‘Y’ implies it needs to be created as a new child of ‘A’.
On encountering a null child and additional unsaved characters in the word, sequentially append them as children. Thus ‘Y’ would be linked under ‘A’, ‘O’ under ‘Y’, and so forth.
classTrie {
staticclassTrieNode {
privateboolean isEnd;
private Map<Character, TrieNode> children =new HashMap<>();
}
public TrieNode root;
/** Initialize your data structure here. */publicTrie() {
this.root=new TrieNode();
}
/** Inserts a word into the trie. */publicvoidinsert(String word) {
TrieNode curr =this.root;
for (char c: word.toCharArray()) {
curr.children.putIfAbsent(c, new TrieNode());
curr = curr.children.get(c);
}
curr.isEnd=true;
}
/** Returns if the word is in the trie. */publicbooleansearch(String word) {
return exists(word);
}
privatebooleanexists(String word) {
TrieNode curr = root;
for (char c: word.toCharArray()) {
if (!curr.children.containsKey(c)) {
returnfalse;
}
curr = curr.children.get(c);
}
return curr.isEnd;
}
/** Returns if there is any word in the trie that starts with the given prefix. */publicbooleanstartsWith(String prefix) {
TrieNode curr = root;
for (char c: prefix.toCharArray()) {
if (!curr.children.containsKey(c)) {
returnfalse;
}
curr = curr.children.get(c);
}
returntrue;
}
}
classTrie {
staticclassTrieNode {
privateboolean isEnd;
private TrieNode[] children =new TrieNode[26];
}
public TrieNode root;
/** Initialize your data structure here. */publicTrie() {
this.root=new TrieNode();
}
/** Inserts a word into the trie. */publicvoidinsert(String word) {
TrieNode curr =this.root;
for (char c: word.toCharArray()) {
int idx = c -'a';
if (curr.children[idx]==null) {
curr.children[idx]=new TrieNode();
}
curr = curr.children[idx];
}
curr.isEnd=true;
}
/** Returns if the word is in the trie. */publicbooleansearch(String word) {
return exists(word);
}
privatebooleanexists(String word) {
TrieNode curr = root;
for (char c: word.toCharArray()) {
int idx = c -'a';
curr = curr.children[idx];
if (curr ==null) {
returnfalse;
}
}
return curr.isEnd;
}
/** Returns if there is any word in the trie that starts with the given prefix. */publicbooleanstartsWith(String prefix) {
TrieNode curr = root;
for (char c: prefix.toCharArray()) {
int idx = c -'a';
curr = curr.children[idx];
if (curr ==null) {
returnfalse;
}
}
returntrue;
}
}