Created
June 5, 2014 16:40
-
-
Save kwyn/8f61037033b1b8801c0f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var treeMaker = function(value){ | |
//tree code goes here! | |
var tree = Object.create(treeMaker.methods); | |
tree.value = value || null; | |
tree.children = []; | |
return tree; | |
}; | |
//methods go here! | |
treeMaker.methods = {}; | |
treeMaker.methods.addChild = function(value){ | |
var node = treeMaker(value); | |
this.children.push(node); | |
}; | |
treeMaker.methods.contains = function(value){ | |
var currentNode = this; | |
var queue = []; | |
while(currentNode){ | |
if(currentNode.value === value){ | |
return true; | |
} | |
for (var i = 0; i < currentNode.children.length; i++) { | |
queue.push(currentNode.children[i]); | |
} | |
currentNode = queue.shift(); | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment