This is a JSFiddle assignment and must be completed at jsfiddle.net and include
ID: 3579157 • Letter: T
Question
This is a JSFiddle assignment and must be completed at jsfiddle.net and include both the HTML and Java sections
Implement a TRIE that has both the functions AddToTRIE and CheckSpelling.
Once you have created the TRIE add the words (hard code) I, in, into, inlet, inn, inner, innate, ink. These are good words to use to test your TRIE
Create an interface that has one text box for entry and 2 buttons - Add To Dictionary and Spell Check. They should ad the word to the TRIE (feedback - word added) and check the word against the TRIE and return in the word was in or not in the dictionary.
Explanation / Answer
class Trie Node {
char c;
Hash Map<Character, Trie Node> children = new Hash Map<Character, Trie Node>();
boolean is Leaf;
public Trie Node() {}
public Trie Node(char c){
this.c = c;
}
}
public class Trie {
private Trie Node root;
public Trie() {
root = new Trie Node();
}
// Inserts a word into the trie.
public void insert(String word) {
Hash Map<Character, Trie Node> children = root.children;
for(int i=0; i<word.length(); i++){
char c = word.charAt(i);
Trie Node t;
if(children.contains Key(c)){
t = children.get(c);
}else{
t = new Trie Node(c);
children.put(c, t);
}
children = t.children;
//set leaf node
if(i==word.length()-1)
t.is Leaf = true;
}
}
// Returns if the word is in the trie.
public boolean search(String word) {
Trie Node t = search Node(word);
if(t != null && t.is Leaf)
return true;
else
return false;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean start sWith(String prefix) {
if(search Node(prefix) == null)
return false;
else
return true;
}
public Trie Node search Node(String str){
Map<Character, Trie Node> children = root.children;
Trie Node t = null;
for(int i=0; i<str.length(); i++){
char c = str.charAt(i);
if(children.contains Key(c)){
t = children.get(c);
children = t.children;
}else{
return null;
}
}
return t;
}
}
class Trie Node {
char c;
Hash Map<Character, Trie Node> children = new Hash Map<Character, Trie Node>();
boolean is Leaf;
public Trie Node() {}
public Trie Node(char c){
this.c = c;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.