Skip to content

Instantly share code, notes, and snippets.

@kwyn
Created June 5, 2014 16:40
Show Gist options
  • Save kwyn/8f61037033b1b8801c0f to your computer and use it in GitHub Desktop.
Save kwyn/8f61037033b1b8801c0f to your computer and use it in GitHub Desktop.
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