Assignment 13 - Trie Spellchecking
by sheila massey
HTML
<h1>TRIE Spellchecking</h1>
<input id = uInput type = text size = 18>
<button id = add onclick = addWord()>Add To Dictionary</button>
<button id = check onclick = check()>Spell check</button><br><br>
<p id='output'></p>
JavaScript
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
document.getElementById('uInput').focus();
function Node(data)
{
this.data = data;
this.isWord = false;
this.children = [];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Trie() {
this.root = new Node();
}
Trie.prototype.add = function(word)
{
if ( this.root == null)
{
return;
}
this._addNode(this.root, word);
};
////////////////////////////////////////////////
Trie.prototype._addNode = function(node, word)
{
if (node == false || word == false)
{
return null;
}
///////////////////////////////////////////////////
var letter = word.charAt(0);
var child = node.children[letter];
if (child == null)
{
child = new Node(letter);
node.children[letter] = child;
}
//////////////////////////////////////////
var remainder = word.substring(1);
if (remainder == 0) {
child.isWord = true;
}
this._addNode(child, remainder);
Trie.prototype.contains = function(word) {
if (this.root == null) {
return;
}
return this._contains(this.root, word);
};
Trie.prototype._contains = function(node, word) {
if (node == null || word == null) {
return false;
}
//////////////////////////////
var letter = word.charAt(0);
var child = node.children[letter];
if (child) {
var remainder = word.substring(1);
if (remainder == false && child.isWord) {
return true;
} else {
return this._contains(child, remainder);
}
} else {
return false;
}
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var trie = new Trie();
function addWord()
{
var i = document.getElementById('uInput').value;
trie.add(i);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function check() {
var s =...