programming-examples/ruby/Algorithms/tree_breadth_first_search.rb
2019-11-15 12:59:38 +01:00

23 lines
355 B
Ruby

def bfs_tree(node)
queue = [node]
while queue.size != 0
n = queue.shift
puts n.value
n.children.each do |child|
queue.push(child)
end
end
end
def bfs_binary_tree(node)
queue = [node]
while queue.size != 0
n = queue.shift
puts n.value
queue.push(n.left) if n.left
queue.push(n.right) if n.right
end
end