Skip to content

Instantly share code, notes, and snippets.

@git-ashish
Last active January 2, 2016 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save git-ashish/8368381 to your computer and use it in GitHub Desktop.
Save git-ashish/8368381 to your computer and use it in GitHub Desktop.
Breadth First Search
/**
* Breadth First Search to traverse through all data points
*
* Structure of json :
*
* { "name": "Alpha",
* "children": [
* {
* "name": "Beta",
* "children": [{
* //...
* }]
* }]
* }
*
*/
function bfs(json) {
var remainingNodes = json.children,
nextNodes = []
;
while (remainingNodes.length) {
nextNodes = [];
remainingNodes.forEach(function(node) {
if (node.children) {
node.children.forEach(function(n) {
nextNodes.push(n);
});
};
//console.log(node);
});
remainingNodes = nextNodes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment