Skip to content

Commit b6ac765

Browse files
moshejstrekhleb
authored andcommittedAug 14, 2018
Pseudocode + big O for BFS (trekhleb#166)
1 parent 953eaf8 commit b6ac765

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 

‎src/algorithms/tree/breadth-first-search/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ nodes first, before moving to the next level neighbors.
88

99
![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/5/5d/Breadth-First-Search-Algorithm.gif)
1010

11+
## Pseudocode
12+
13+
BFS(root)
14+
Pre: root is the node of the BST
15+
Post: the nodes in the BST have been visited in breadth first order
16+
q ← queue
17+
while root = ø
18+
yield root.value
19+
if root.left = ø
20+
q.enqueue(root.left)
21+
end if
22+
if root.right = ø
23+
q.enqueue(root.right)
24+
end if
25+
if !q.isEmpty()
26+
root ← q.dequeue()
27+
else
28+
root ← ø
29+
end if
30+
end while
31+
end BFS
32+
33+
## Space and Time Complexity:
34+
35+
O(b<sup>d + 1</sup>)
36+
1137
## References
1238

1339
- [Wikipedia](https://en.wikipedia.org/wiki/Breadth-first_search)

0 commit comments

Comments
 (0)