Created
April 17, 2014 19:29
-
-
Save ionox0/11006495 to your computer and use it in GitHub Desktop.
Breadth First Search
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
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