-
-
Save git-ashish/8368381 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
/** | |
* 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