Skip to content

Instantly share code, notes, and snippets.

@ionox0
Created April 17, 2014 19:29
Show Gist options
  • Save ionox0/11006495 to your computer and use it in GitHub Desktop.
Save ionox0/11006495 to your computer and use it in GitHub Desktop.
Breadth First Search
function BFS(node){
var list = [node];
var current = list.shift();
while(current){
console.log(current.data);
var i = 1;
while (current["link" + i]){
list.push(current["link" + i]);
i ++;
}
current = list.shift();
}
}
//test
var node1, node2, node3, node4, node5, node6, node7;
node4 = {
data: "d"
}
node5 = {
data: "e"
}
node6 = {
data: "f"
}
node7 = {
data: "g"
}
node2 = {
data: "b",
link1: node4,
link2: node5
}
node3 = {
data: "c",
link1: node6,
link2: node7
}
node1 = {
data: "a",
link1: node2,
link2: node3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment