programming-examples/ruby/Algorithms/tree_breadth_first_search.rb

23 lines
355 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
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