Skip to content

Instantly share code, notes, and snippets.

@jharris-code
Last active January 14, 2019 19:56
Show Gist options
  • Save jharris-code/e57704ce936a30b2fd2da16989adcffd to your computer and use it in GitHub Desktop.
Save jharris-code/e57704ce936a30b2fd2da16989adcffd to your computer and use it in GitHub Desktop.
Breadth First Search
//Assumes:
//a Queue data structure is available with push(), pop(), isEmpty() methods.
//each node in the Queue have left and right properties.
//Time Complexity: O(N)
//Space Complexity: O(N)
const bfs = (root, value) => {
let q = new Queue();
q.push(root);
while(!q.isEmpty()) {
let n = q.pop();
if(n.value === value){
return n;
} else {
if(n.left) {
q.push(n.left);
}
if(n.right) {
q.push(n.right);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment