Skip to content

Instantly share code, notes, and snippets.

@goatslacker
Created April 15, 2011 06:14
Show Gist options
  • Save goatslacker/921225 to your computer and use it in GitHub Desktop.
Save goatslacker/921225 to your computer and use it in GitHub Desktop.
Depth First Search JS
var Search = {
opened: [],
closed: [],
/*
BFS: function (tree) {
this.opened.push(tree);
while (this.opened.length > 0) {
this.walkBFS(this.opened[0]);
}
console.log(this.closed);
},
*/
DFS: function (tree) {
this.opened.push(tree);
while (this.opened.length > 0) {
var branch = this.opened[0];
var tmpArr = [];
this.closed.push(this.opened[0].value);
this.opened.shift();
for (var val in branch) {
if (val === 'left' || val === 'right') {
tmpArr.push(branch[val]);
}
}
this.opened = tmpArr.concat(this.opened);
}
console.log(this.closed);
},
/*
walkBFS: function (obj) {
if (this.opened.length > 0) {
this.opened.shift();
}
for (var branch in obj) {
if (obj[branch] !== null) {
this.opened.push(obj[branch]);
}
this.closed.push(branch);
}
}
*/
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment