This is a JSFiddle assignment and must be completed at jsfiddle.net and include
ID: 3577640 • 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
/* TRIE implementation in Javascript */
//Class MyTrie.
MyTrie = function()
{
//Initialize words.
this.words = 0;
//Initialize prefixes.
this.prefixes = 0;
//Initialize children.
this.children = [];
};
MyTrie.prototype =
{
//Function addToTrie.
addToTrie: function(myStr, position)
{
if(myStr.length == 0)
{
//Blank string can't be added
return;
}
var MyT = this,
idx,
tChild;
if(position === undefined)
{
position = 0;
}
if(position === myStr.length)
{
MyT.words ++;
return;
}
MyT.prefixes ++;
idx = myStr[position];
if(MyT.children[idx] === undefined)
{
MyT.children[idx] = new MyTrie();
}
tChild = MyT.children[idx];
tChild.addToTrie(myStr, position + 1);
},
//Function delete
removeFromTrie: function(myStr, position)
{
if(myStr.length == 0)
{
return;
}
var MyT = this,
idx,
tChild;
if(position === undefined)
{
position = 0;
}
if(MyT === undefined)
{
return;
}
if(position === myStr.length)
{
MyT.words --;
return;
}
MyT.prefixes --;
idx = myStr[position];
tChild = MyT.children[idx];
tChild.removeFromTrie(myStr, position + 1);
},
//Method updateTrie.
updateTrie: function(stringOld, stringNew)
{
if(stringOld.length == 0 || stringNew.length == 0)
{
return;
}
this.removeFromTrie(stringOld);
this.addToTrie(stringNew);
},
//Method countTWord.
countTWord: function(myStr, position)
{
if(myStr.length == 0)
{
return 0;
}
var MyT = this, idx, tChild, myReturn = 0;
if(position === undefined)
{
position = 0;
}
if(position === myStr.length)
{
return MyT.words;
}
idx = myStr[position];
tChild = MyT.children[idx];
if(tChild !== undefined)
{
myReturn = tChild.countTWord(myStr, position + 1);
}
return myReturn;
},
//Method countTPrefix.
countTPrefix: function(myStr, position)
{
if(myStr.length == 0)
{
return 0;
}
var MyT = this, idx, tChild, myReturn = 0;
if(position === undefined)
{
position = 0;
}
if(position === myStr.length)
{
return MyT.prefixes;
}
var idx = myStr[position];
tChild = MyT.children[idx];
if(tChild !== undefined)
{
myReturn = tChild.countTPrefix(myStr, position + 1);
}
return myReturn;
},
//Method checkSpelling.
checkSpelling: function(myStr)
{
if(myStr.length == 0)
{
return false;
}
if(this.countTWord(myStr) > 0)
{
return true;
}
else
{
return false;
}
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.