Created
September 21, 2013 07:14
-
-
Save jhamon/6648061 to your computer and use it in GitHub Desktop.
This doesn't work because the block can't be passed recursively.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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