Created
May 27, 2014 20:40
-
-
Save amckemie/304c3817ad15dc11a352 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Defines the node object | |
var Node = function(options) { | |
var options = options || {}; | |
this.value = options.value; | |
this.parent = options.parent; | |
this.children = options.children || {}; | |
this.stop = options.stop; | |
}; | |
var Trie = function() { | |
this.head = new Node(); | |
// Inserts string into the Trie. | |
this.insert = function(string) { | |
var currentNode = this.head; | |
var word = string.split(''); | |
for(var i = 0; i < word.length; i++){ | |
var letter = word[i]; | |
if(currentNode.children[letter] === undefined){ | |
var options = {parent: currentNode, value: word[i], children: {}, stop: false}; | |
currentNode.children[word[i]] = new Node(options); | |
} | |
currentNode = currentNode.children[word[i]]; | |
} | |
currentNode.stop = true; | |
console.log(this.head); | |
}; | |
// Recursive function. Helper function for the insert function. | |
this._insert = function() { | |
}; | |
// Returns true if string is in the trie. Returns false otherwise. | |
this.includes = function(string) { | |
if(!string){ | |
return false; | |
} | |
currentNode = this.head | |
for(var i=0; i < string.length; i++){ | |
if(currentNode.children[string[i]] === undefined){ | |
return false; | |
} | |
else { | |
currentNode = currentNode.children[string[i]]; | |
} | |
} | |
if(currentNode.stop === true){ | |
return true; | |
} | |
else { | |
return false; | |
} | |
}; | |
// Recursive function. Returns true if string is in the trie. Returns false | |
// otherwise. | |
this._includes = function() { | |
}; | |
// Search for all strings that have 'prefix' as the prefix. | |
// | |
// Returns Array of Strings. | |
this.search = function(prefix) { | |
}; | |
// Recursive function. Helper function for the search function. | |
this._search = function() { | |
}; | |
// Find the node that correspond to the last character in the string. | |
// | |
// Returns Node. | |
this.findLastNode = function(string) { | |
}; | |
// Recursive function. Helper function for the findLastNode function. | |
this._findLastNode = function() { | |
}; | |
// Given a node, return all the strings that are part of this node. | |
// | |
// Returns Array of Strings. | |
this.iterate = function(node) { | |
if (node === undefined){ | |
return []; | |
} | |
var result = []; | |
var actualResult = []; | |
var str = node.value; | |
result.push(this._iterate(str, node)); | |
actualResult = actualResult.concat.apply(actualResult, result); | |
return actualResult; | |
}; | |
// Recursive helper function for .iterate | |
this._iterate = function(str, node) { | |
var result = []; | |
var children = node.children; | |
var keys = Object.keys(children); | |
if(keys.length === 0){ | |
result.push(str); | |
return result; | |
} | |
for(var i = 0; i < keys.length; i++){ | |
var local = str + children[keys[i]].value; | |
if (children[keys[i]].stop){ | |
result.push(local); | |
} | |
else { | |
result.push( this._iterate(local, children[keys[i]]) ); | |
} | |
} | |
}; | |
// You may find this function useful for implementing iterate(). | |
// | |
// Returns true if object is empty. False otherwise. | |
this.isEmpty = function (object) { | |
for(var i in object) { | |
return false; | |
} | |
return true; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment