mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Pseudocode + big O for BFS (#166)
This commit is contained in:
parent
953eaf8970
commit
b6ac765c99
@ -8,6 +8,32 @@ nodes first, before moving to the next level neighbors.
|
||||
|
||||
![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/5/5d/Breadth-First-Search-Algorithm.gif)
|
||||
|
||||
## Pseudocode
|
||||
|
||||
BFS(root)
|
||||
Pre: root is the node of the BST
|
||||
Post: the nodes in the BST have been visited in breadth first order
|
||||
q ← queue
|
||||
while root = ø
|
||||
yield root.value
|
||||
if root.left = ø
|
||||
q.enqueue(root.left)
|
||||
end if
|
||||
if root.right = ø
|
||||
q.enqueue(root.right)
|
||||
end if
|
||||
if !q.isEmpty()
|
||||
root ← q.dequeue()
|
||||
else
|
||||
root ← ø
|
||||
end if
|
||||
end while
|
||||
end BFS
|
||||
|
||||
## Space and Time Complexity:
|
||||
|
||||
O(b<sup>d + 1</sup>)
|
||||
|
||||
## References
|
||||
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Breadth-first_search)
|
||||
|
Loading…
Reference in New Issue
Block a user