TEIR
by Juan Alban Franco
HTML
<html>
<body>
<input type = textbox value = 'input' id = input>
<br>
<input type = button value = "Add" onclick = "add()">
<input type = button value = "Search" onclick = "search ()">
<br>
<br>
<p id = "out">
</p>
</body>
</html>
JavaScript
function Node(_content){
this.content = _content;
this.wordtest = 0;
this.prefx = 0;
this.children = {};
}
function Trie() {
this.root = new Node('')
}
Trie.prototype.add = function(_word){
if(!this.root){ return null;}
this.addnodes(this.root,_word);
};
Trie.prototype.addnodes = function (node,word){
if (!node|| !word){ return null;}
node.prefx++;
var letter = word.charAt(0);
var child = node.children[letter]
console.log(child);
if(!child){
child = new Node(letter);
node.children[letter] = child;
}
var r = word.substring(1);
if(!r){
child.isWord =1;
}
this.addnodes(child,r);
}
Trie.prototype.remove = function(word){
if(!this.root) {
return
}
if(this.contains(word)){
this.removenodes(this.root,word);
}
}
Trie.prototype.removenodes = function (node,word){
if (!node || !word){ return false;}
node.prefixes --;
var letter = word.charAt(0);
var child = node.children[letter];
if(child){
var r = word.substring(1);
if (r){
if(child.prefx ==1){
delete node.children[letter];
}
else{
this.removenodes(child,r); //recursive call to itterate trough word
}
}else{
child.isWord=0;
}
}
}
Trie.prototype.contains = function(word){
if(!this.root){
return false;
}
return this.T_Contains(this.root,word);
}
Trie.prototype.T_Contains = function(node,word){
if(!node || !word){return false;}
var letter = word.charAt(0);
var child = node.children[letter];
if(child){
var r = word.substring(1);
if(!r && child.isWord){
alert("found")
return true;
}
else {
return this.T_Contains(child,r);
}
}
else{
return false;
}
}
var tree = new Trie();
tree.add('in');
tree.add("in");
tree.add("into");
tree.add("inlet");
tree.add("inn");
tree.add("inner");
tree.add("innate");
tree.add("ink");
tree.add("I");
function add (){
var phrase = document.getElementById("input").value;
tree.add(phrase);
}
function search()...