Created
April 15, 2011 06:14
-
-
Save goatslacker/921225 to your computer and use it in GitHub Desktop.
Depth First Search JS
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
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