Created
June 14, 2019 01:33
-
-
Save StevenKulesza/dfd2a8e6493c9d422a32450ddfe3b5d7 to your computer and use it in GitHub Desktop.
breadth first search
This file contains hidden or 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(tree, value) { | |
| var queue = [] | |
| queue.push(tree[0]) | |
| while (queue.length !== 0) { | |
| for (let i = 0; i < queue.length; i++) { | |
| var node = queue.shift() | |
| if (node.value === value) { | |
| return node | |
| } | |
| if (node.left) { | |
| queue.push(tree[node.left]) | |
| } | |
| if (node.right) { | |
| queue.push(tree[node.right]) | |
| } | |
| } | |
| } | |
| return null | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment