SHARE
TWEET

Untitled

a guest Nov 14th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bfs(tree, value) {
  2.     var queue = []
  3.    
  4.     queue.push(tree[0])
  5.    
  6.     while (queue.length !== 0) {
  7.         for (let i = 0; i < queue.length; i++) {
  8.            
  9.             var node = queue.shift()
  10.  
  11.             if (node.value === value) {
  12.                 return node
  13.             }
  14.             if (node.left) {
  15.                 queue.push(tree[node.left])
  16.             }
  17.             if (node.right) {
  18.                 queue.push(tree[node.right])
  19.             }
  20.         }
  21.     }
  22.     console.log('Unable to find node')
  23. }
RAW Paste Data
Top