Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created December 19, 2014 02:30
Show Gist options
  • Save ccorcos/ea10ef07d25107cd5e79 to your computer and use it in GitHub Desktop.
Save ccorcos/ea10ef07d25107cd5e79 to your computer and use it in GitHub Desktop.
breadth first search find coffeescript
match = (obj, properties) ->
for k,v of properties
if not k of obj
return false
else if v instanceof Object
if not match obj[k], v
return false
else if obj[k] isnt v
return false
return true
bfsFind = (collection, edgeKey, properties) ->
queue = collection
while queue.length > 0
obj = queue.shift()
if match obj, properties
return obj
else if edgeKey of obj
for child in obj[edgeKey]
collection.push(child)
return undefined
bfsFindAll = (collection, edgeKey, properties) ->
found = []
queue = collection
while queue.length > 0
obj = queue.shift()
if match obj, properties
found.push(obj)
else if edgeKey of obj
for child in obj[edgeKey]
collection.push(child)
return found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment