From b6ac765c996c1de10da5ed89013684925cffca5d Mon Sep 17 00:00:00 2001 From: Moshe Date: Tue, 14 Aug 2018 08:21:34 -0400 Subject: [PATCH] Pseudocode + big O for BFS (#166) --- .../tree/breadth-first-search/README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/algorithms/tree/breadth-first-search/README.md b/src/algorithms/tree/breadth-first-search/README.md index d9a07615..4c897cae 100644 --- a/src/algorithms/tree/breadth-first-search/README.md +++ b/src/algorithms/tree/breadth-first-search/README.md @@ -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(bd + 1) + ## References - [Wikipedia](https://en.wikipedia.org/wiki/Breadth-first_search)