Skip to content

Instantly share code, notes, and snippets.

@jhamon
Created September 21, 2013 07:14
Show Gist options
  • Save jhamon/6648061 to your computer and use it in GitHub Desktop.
Save jhamon/6648061 to your computer and use it in GitHub Desktop.
This doesn't work because the block can't be passed recursively.
class TreeNode
# ... omitted.
def dfs(target_value = nil, &blk)
# A (broken) first attempt at depth-first search
# with arbitrary search criteria defined by the
# block received.
return self if !blk.nil && blk.call(@value)
return self if @value == target_value
found_node = nil
@children.each do |child|
found_node = child.dfs(target_value, blk) # Argument error.
return found_node unless found_node.nil?
end
found_node
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment