Skip to content

Instantly share code, notes, and snippets.

@ccnixon
Forked from ionox0/breadthFirstSearch.js
Created October 10, 2015 15:37
Show Gist options
  • Save ccnixon/7534795bd4e6aa106566 to your computer and use it in GitHub Desktop.
Save ccnixon/7534795bd4e6aa106566 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