<div class="trans">
<h1 id="title">Assignment 13</h1>
</div>
<div class="trans" id="intro">
<h4>A "Trie-powered" Dictionary can be used to spellcheck very efficiently.<br/>Enter a word to check if it exists and add it if it doesn't.<br/>The sort order is: Numbers => CAPS => lower case</h4>
</div>
<div class="trans"><h2><div id="checkTrue"></div></h2></div>
<div class="trans"><h2><div class="blink" id="checkFalse"></div></h2></div>
<div class="input_elements">
<input type="tb1" id="input" onkeyup="checkInput()" onkeypress="handle(event)"><br/>
<div class="col1">
<input type="button" class="button" value="Check word" id="bt2" onClick="check();">
<input type="button" class="button" value="Add to dictionary" id="bt1" onClick="add();">
</div>
</div>
<div>
</div>
<div class="col2" id="array"></div>
<div class="col2" id="trie"></div>
//Assignment 13
//hard coded samples stored in an array and later fed into trie
var dictionary = ['I', 'in', 'into', 'inlet', 'inn', 'inner', 'innate', 'ink'];
//The enter key handler
function handle(e) {
var key = e.keyCode || e.which;
if (key == 13) {
input = document.getElementById("input").value;
check(input);
}
}
/*Excellent example found at https://github.com/rgantt/jsterbate/blob/master/lib/trie.js
Specific implentation to assignment and addition of check() and add() functions was done by me. I also commented the code to demonstrate my understanding of this data structure.
*/
//function expression for the trie includes the max number of letters in the first level
var Trie = (function () {
var ALPHABET_SIZE = 26;
//TrieNodes are the single elements like leafs to a tree. Each node has a key and a value
var TrieNode = function (key, value) {
this.key = key;
this.value = value;
//children are stored in arrays
this.children = [];
for (var i = 0; i < ALPHABET_SIZE; i++) {
this.children[i] = null;
}
//adding the current letter into the array
this.putChild = function (node) {
return (this.children[node.key.charCodeAt(0)] = node);
};
//getting the current child
this.getChild = function(key) {
return this.children[key.charCodeAt(0)];
};
};
//here is where it gets interresting: the child is evaluated
var add = function (node, key, value) {
//target represents the current letter
var target = node.getChild(key.charAt(0));
// if it's null a new node is created
if (target == null) {
target = node.putChild(new TrieNode(key.charAt(0), null));
}
// if it exists its value is returned
if (key.length == 1) {
return (target.value = value);
}
// the next letter is sliced off
return add(target, key.slice(1),...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.